Embedded Programming

Arduino programming language

In this week assignment we had to develop a program that: uses most of the techniques shown during the lecture, uses a button or another human input, that it’s possible to verify and that is working via serial communication and a led. I decided to develop a program that works as a trafic light.

Data Types

Below you can see all different data types that you can use. A data type is a data storage format that can contain a specific type or range of values.

Explaining the data types:

boolean is a binary variable that can have one of two possible values, 0 (false) or 1 (true)

'char' means character and converts a number into the corresponding symbol of the ASCII Table.

'unsigned' values cannot be a negative number, only positive.

'int' means integer which refers to a whole number that can be positiv or negativ, and 'long' is the same as an integer but has a size of 64 bit.

'float' or 'double' are used for numbers with a decimal point.

• An 'array' is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. For example: int myArray[10]={9,3,2,4,3,2,7,8,9,11}; the 10 indicates how many items are stored inside the array, and between the curly brackets are all the items listed. If you want to acces the 9th element you write myArray[9], because the numbering begins with element 0. If you do not use the curly brackets to list the items you want, the array will create random items.

Operators

Below you can find all the different operators which you can use in the Arduino Software.

Control structure

Conditional Statements:

Use the if statement to specify a block of code to be executed, if a specified condition is true. If the codition is false use the else statement. Use else if to specify a new condition to test, if the first condition is false. The switch case statement is used to perform different actions based on different conditions.

Loops:

Loops can execute a block of code a number of times. There are different kind of loops. In the following I will explain the difference between the for and the while loop.

The for loop goes through a block of code a number of times. The while loop instead goes through a block of code while a specified condition is true.

Sketch structure

In the Arduino sketch you always have a void setup() function first followed by the void loop() funtion. The void setup() function is called when a sketch starts and will be displayed only once after each powerup of the Arduino board. The void loop() function works like a loop and will be executed repeatedly. Before the void setup, you can define any kind of data type or pin, that will be used in the subsequent code. Or you can also define them inside the void setup() function. Moreover you can also incorporate libraries into your code.

Digital and Analog fuctions

The pinMode() function configures the specified pin to behave either as an input or an output. The digitalRead() function reads the value from a specified digital pin, either HIGH or LOW. The digitalWrite() function writes a HIGH or a LOW value to a digital pin.

The analogRead() function reads the value from the specified analog pin. The analogWrite() function writes an analog value (PWM wave) to a pin. It can be used to light a LED at varying brightnesses.

Serial communication

You can activate the serial protocol with Serial.begin() inside the brackets you specify the data transmission rate at bits per second (baud). The higher the value, the faster the communication. Serial.print() prints data to the serial port

Programming with the Arduino Software

Below you can see the code of my trafic light system. Everytime you push the button, the emergency light will turn off and the trafic light will turn on. Moreover it writes a debug message to the serial connection.

#include <SoftwareSerial.h> // to use the Serial with an ATtiny45 you need to import this library
SoftwareSerial mySerial(2, 0); // These are RX, TX

//constants won't change. They're used here to set pin numbers:
const int RED = 2;        // I connected the red LED on the ATtiny45 Pin 7
const int YELLOW = 3;    // I connected the yellow LED on the ATtiny45 Pin 2
const int GREEN = 1;    // I connected the green LED on the ATtiny45 Pin 6
const int BUTTON = 4;  // I connected the Button on the ATtiny45 Pin 3 
// variable for reading the pushbutton status
int buttonState = 0; 

// RED LED on / off:
#define RedOn()       digitalWrite(RED, HIGH)
#define RedOff()      digitalWrite(RED, LOW)
// YELLOW LED on / off:
#define YellowOn()    digitalWrite(YELLOW, HIGH)
#define YellowOff()   digitalWrite(YELLOW, LOW)
// GREEN LED on / off:
#define GreenOn()     digitalWrite(GREEN, HIGH)
#define GreenOff()    digitalWrite(GREEN, LOW)
// Timer Redphase:
#define DelayOne()    delay(5000)
// Timer transition RED to GREEN:
#define DelayTwo()    delay(2000)
// Timer Greenphase:
#define DelayThree()  delay(5000)
// Timer transition GREEN to RED:
#define DelayFour()   delay(2000)

void setup() {
   // initialize digital LED pins as an output
  pinMode(RED, OUTPUT);
  pinMode(YELLOW, OUTPUT);
  pinMode(GREEN, OUTPUT);
  // initialize digital BUTTON pins as an input
  pinMode(BUTTON, INPUT);
  //the data transmission rate for the serail
  mySerial.begin(4800);
  // jump to the initialize function
  initialize();
}

// First all LEDs off:
void initialize() {
  RedOff();
  YellowOff();
  GreenOff(); 
}

// Redphase:
void phaseOne() {
  RedOn();
  YellowOff();
  GreenOff();
  DelayOne();
}

// Yellowphase:
void phaseTwo() {
  RedOn();
  YellowOn();
  GreenOff();
  DelayTwo();
}

// Greenphase:
void phaseThree() {
  RedOff();
  YellowOff();
  GreenOn();
  DelayThree();
}

// Yellowphase:
void phaseFour() {
  RedOff();
  YellowOn();
  GreenOff();
  DelayFour();
}


void loop() {
  // read the state of the button pin and save it in the buttonState value:
 buttonState = digitalRead(BUTTON); 
 //print the button state into serial, state 0 = LOW, state 1 = HIGH
  mySerial.println(digitalRead(BUTTON));
  // check if the button is not pressed. If it is not, the buttonState is LOW:
   if ( buttonState == LOW) {
   	// if the statement above is true, turn the trafic light on and repeat it for 3 times
    for ( int i=0; i < 3; i++) {
		// it should print tree times the sentence below 
         mySerial.println("Tarfic light will blick for 3 times!");
         phaseThree();
         phaseFour();
         phaseOne();
         phaseTwo(); 
               }
            }
   //  if the button is HIGH turn emergency light on. 
    else { 
	// print the sentence below as long as the button is pressed
      mySerial.println("Emergency light is on");
         RedOff();
         GreenOff();
         YellowOn();
	// pauses the program for 500 milliseconds
         delay(500);
         YellowOff();
	// pauses the program for 500 milliseconds
         delay(500);
     
    }
}
					

Before starting to connect my FabLab board to the arduino, I uploaded the ArduinoISP code onto the Arduino Uno. To communicate through the Arduino Software with the ATtiny45 board, I had to check the pinout of the ATtiny45. As you can see below pin 7 on the ATtiny45 is addressed by using pin number 2 in the Arduino Software.

Now I started to connect the Arduion with my board to upload the code.

To start the serial connection I had to connected the SCK and MOSI pins, which I initialize in the code to the software serial, with an USB TTL serial cable to my computer.

Furthermore I opened the arduino serial monitor and selected the correct baud rate of 4800 baud. When the emergency light was blinking it pushed continually the message "Emergency light is on". Morover it always showed the button state. After pressing the button on the board the debug message "Tarfic light will blick for 3 times!" appeared.