Lesson 7 | Embedded Programming

Assignment for the week:

- develop program that:

     - uses most of the techniques shown during the lecture

     - uses a button or another human input

     - it's possible to verify that is working via serial communication and a led

- upload the program into one of your Fab Lab made boards

- create a page to document your progress of week 7, what did you do, in which order, what are your experiences (problems, solutions, etc.),
   add a video to show how the program you did is working, display the code into the page and add the download of it

Used Software:

    - Arduino IDE

Downloads:

    Software:
            - Arduino IDE

    Sourcecode:
             Download

             This zip.File include the .ino file with the sourcecode for my program.


Idea:

The next step after designing and manufacturing a board is programming the microcontroller independently. We use Arduino-C as the programming language. This is a modification of the language C and should simplify the introduction to programming. But for example the access to the registers was removed. The ATMEGA could also be programmed in "pure" C or Assembler. As development environment we use the Arduino IDE.

In the lecture we learned the basics of the language. We should apply this in today's lesson. Due to the fact that I had already worked with the Arduino before, I knew the basics of the language. Later I will show and comment snippets from my code again and again.

The full code

Below you can see my code.

/*#-#-#-#-#( Import needed libraries )#-#-#-#-#*/

/*#-#-#-#-#( Declare Constants and Pin Numbers )#-#-#-#-#*/
#define ledPin 8
#define boardButton 9

/*#-#-#-#-#( Declare objects )#-#-#-#-#*/

/*#-#-#-#-#( Declare Variables )#-#-#-#-#*/
int waitingTime = 500;
boolean buttonState = false;

//Functions
    void LedBlink (int amount)
{
  for(int i; i<=amount;i++){
    digitalWrite(ledPin, HIGH);
    delay(waitingTime);
    digitalWrite(ledPin, LOW);
    delay(waitingTime);
  }
}

void setup() {
//pinMode setzen
    pinMode(ledPin, OUTPUT);
    pinMode(boardButton, INPUT);

//Serial Communication start
  Serial.begin(9600);
  Serial.print("Hello");
}

void loop() {
 if (boardButton == HIGH && buttonState == false){
    buttonState = true;
    Serial.println("I'm listen to you");
    Serial.println("Enter a number and the LED will blink for you");
    do{
      char readValue =Serial.read();
      LedBlink(readValue);
      if(boardButton == LOW){
        buttonState = false;
        Serial.println("Now I'm not listening to you anymore.");
      }
    }
    while(buttonState == true);
  }
}

Define

We can address the pins of a microcontroller either by their pin numbers (e.g. pin 15) or by their names (e.g. PB0). But with "#define" we can assign more memorable names to the pins. In this case the pin PB0, to which the Led is connected, becomes the pin "ledPin". This way I can remember it better and it comes to less mix-ups.

#define ledPin 8
#define boardButton 9 

Variable

In variables we can store values like numbers, letters, text strings or truth values. In order to be able to use a variable, we have to define it before using it. First you have to name the type, then give the variable a name and then assign a value if needed. The value can also be assigned later. An overview of all data types can be found here. In order to be able to change the waiting times quickly and easily globally, I also create a Boolean to save the state of the switch.

int waitingTime = 500;
boolean buttonState = false;

Setup

The setup is run once when starting the microcontroller, not afterwards. Only when the power is disconnected and the microcontroller is supplied with power again, the setup is run through again. In the setup I set the two pins for the Led and for the button in their respective mode. The button is an input device and has to be declared as input. The Led is an output device and has to be declared as output. I also start the serial communication with 9600 Baud/s (signal changes per second) and send the string "Hello" via the serial communication.

void setup() {
//pinMode setzen
    pinMode(ledPin, OUTPUT);
    pinMode(boardButton, INPUT);

//Serial Communication start
  Serial.begin(9600);
  Serial.print("Hello");
}

Loop

The loop is repeated again and again. This means, if the commands in the loop have all run through, the microcontroller jumps back to the beginning of the loop and processes it again. In the IF query I check if the switch was pressed (boardButton == HIGH) and if the state of the button is wrong (buttonState == false). If both conditions apply, the code is executed inside the curly braces. First the function ON without parameters is called. Then the state of the buttonState variable is set to true. Then the two strings are sent via serial communication. Then the microcontroller jumps into a do-while loop. This is repeated until the condition is fulfilled. First the input of the serial communication is read in via Serial.read() and stored in the variable readValue. Then the function LedBlink with the value of readValue is called. The function causes the Led to flash as often as the value from readValue is. This loop is repeated until the button is pressed again.

void loop() {
 if (boardButton == HIGH && buttonState == false){
    buttonState = true;
    Serial.println("I'm listen to you");
    Serial.println("Enter a number and the LED will blink for you");
    do{
      char readValue =Serial.read();
      LedBlink(readValue);
      if(boardButton == LOW){
        buttonState = false;
        Serial.println("Now I'm not listening to you anymore.");
      }
    }
    while(buttonState == true);
  }
}

Function

A function helps to keep the code slim and helps to keep the program clear. Parts of the codes can be packed into a function, which is repeated. A function cannot return a value (void) or one or more variables.

void LedBlink (int amount)
{
  for(int i; i<=amount;i++){
    digitalWrite(ledPin, HIGH);
    delay(waitingTime);
    digitalWrite(ledPin, LOW);
    delay(waitingTime);
  }
}

Uploading the Code to the Board

Since we successfully burned the bootloader onto the board last week, we can transfer the code to the board quickly and easily. We use a USB cable with a built-in FTDI chip. Alternatively you can use a simple FTDI programmer board.
Programmer Board
VCC 5V Pin
Ground / GND Ground / GND
RX TX
TX RX
DTR Reset Pin on the left side

After we have wired the board, we can transfer the sketch directly from the Arduino IDE. You have only to choose the right board (in this case Arduino Uno) and the right port and then hit the upload button in the toolbar. How this works in detail you can read in lesson 6.

The serial monitor with some lines of input and output




Issues:

- SMD component are very hard to solder. I almost destroyed my first board.