Embedded programming

  1. Assignment
  2. Connecting the board without Arduino
  3. Code functionality
  4. Download

Assignment

The Assignment for this week was the following:

Develop program that:
  1. Uses most of the techniques shown during the lecture
  2. Uses a button or another human input
  3. 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.

Connecting the board without Arduino

If you dont want to use the arduino uno to load your code on your board you can do the following:

  1. Connect the Arduino to your PC
  2. Under tools set the following settings
    • Board: Arduino Uno
    • Port --> the Port where you see your arduino
    • Programmer: Arduino as ISP
  3. After that you can click on lood bootloader.
  4. Now you have to connect your board with the arduino. After that load the bootloader on your board. If you succesfully done it you can disconnect the arduino and you dont have to use it ever again (atleast for this board ;) ).
  5. The next step is to get a Ftdi cable and to connect it to your board.
    • Black is for the ground
    • Red is for the VCC
    • Orange is for the TXD
    • Yellow is for the RXD
    • Green is for the Reset
  6. After that you can connect your board with your laptop. If your computer does not recognize the USB-Cable you have to download the following drivers: Drivers Download
  7. If the USB gets recognized now you can change the tool settings to the following:
    • Board: Avr-developers.com pinouts 16MHz using Optiboot
    • Port --> the Port which pops up after you insert the USB cable
    • Programmer: Arduino as ISP
  8. After those steps you can start programming. You can upload the code via the upload button.

Code functionality

My Code for this Assignment looks like this:

          
    #define LED_PIN 12
    #define LED_PIN2 13
    #define BUTTON_PIN PB0

    bool switchLED = true;
    bool buttonText = true;
    bool noInput = true;
    char launchText[20] = "Succesfully launched";
            

    void setup() {
        // put your setup code here, to run once:
        pinMode(LED_PIN,OUTPUT);
        pinMode(LED_PIN2,OUTPUT);
        pinMode(BUTTON_PIN,INPUT);
        
        Serial.begin(115200);

        for(int i=0; i < 20; i++) {
            Serial.write(launchText[i]);
        }
        Serial.println("");
    }

    void loop() {
        // put your main code here, to run repeatedly;
        if(Serial.available()) {
            char input = Serial.read();

            if(input == 'x'){
                noInput = false;
                Serial.println("Stopped LED");
            }

            if(input == 'c'){
                noInput = true;
                Serial.println("Started LED");
            } 
        }

        if(noInput){
            if( digitalRead(BUTTON_PIN) ) {
                switchLED = !switchLED;
                delay(200);

                if(switchLED){
                    Serial.println("LED_PIN active");
                }else{
                    Serial.println("LED_PIN2 active");
                }
            }
                
            if(switchLED){
                digitalWrite(LED_PIN, HIGH);
                digitalWrite(LED_PIN2, LOW);
                delay(200);    
            }else{
                digitalWrite(LED_PIN, LOW);
                digitalWrite(LED_PIN2, HIGH);
                delay(200);
            }    
        }else{
            digitalWrite(LED_PIN, LOW);
            digitalWrite(LED_PIN2, LOW);
            if( digitalRead(BUTTON_PIN) ) {
                buttonText = !buttonText;
                delay(200);

                if(buttonText){
                    Serial.println("Type C first to activate the previous button function");
                }else{
                    Serial.println("Type C first to activate the previous button function");
                } 
            }
        }
    }   


My Programm functions like this when you upload the code to your board you will get a message on your serial monitor which says Succesfully launched. After that one LED is light up and the other not. If you press the button the LED which is currently on will go off and the other LED will go on. If the user types an x in the serial monitor then both LED's will go off and you will see the message "Stopped LED". If you press the button now you will get a message to "Type c first to activate the previous button function". If you type in c now you will get the following message "Started LED" and one led is on again. Now the button has the same functionality as in the beginning until you type in x again.

I will split the whole code into tiny snippets and will describe their functionality. At the start of the code i define and initialise some variables i will need later on in the program.

  
    #define LED_PIN 12
    #define LED_PIN2 13
    #define BUTTON_PIN PB0

    bool switchLED = true;
    bool buttonText = true;
    bool noInput = true;
    char launchText[20] = "Succesfully launched";


In the setup function which is called once when you load the program on your board i do the following things. First i set the pinMode of my LED's and my button. Since the LED's are output devices i specify them as output. The button is an input device so i specify it as an input device. With Serial.begin i start a serial communication. I used a for-loop to display a text when the programm is sucesfully launched. Im going through the launchText Array which i declared at the top of the programm.


    void setup() {
        // put your setup code here, to run once:
        pinMode(LED_PIN,OUTPUT);
        pinMode(LED_PIN2,OUTPUT);
        pinMode(BUTTON_PIN,INPUT);
        
        Serial.begin(115200);

        for(int i=0; i < 20; i++) {
            Serial.write(launchText[i]);
        }
        Serial.println("");
    }


The launch text is showed in the serial monitor screen and looks like this:

The code snippets shown below are all in the loop function. This if statement checks if the user typed something in the serial monitor. According to a specific input a boolean will be set to true or false.


    // put your main code here, to run repeatedly;
    if(Serial.available()) {
        char input = Serial.read();

        if(input == 'x'){
            noInput = false;
            Serial.println("Stopped LED");
        }

        if(input == 'c'){
            noInput = true;
            Serial.println("Started LED");
        } 
    }


If the user did not input an x into the serial monitor then the button will function as a LED switch. If you press the button one LED goes off and the other on.

But if the user wrote an x as an input then the button functions as information button so the user knows he has to type in c to get the first functionality of the button. If the user types in c now then userInput is true again and the button will switch the LED's on and off again.


    if(noInput){
        if( digitalRead(BUTTON_PIN) ) {
            switchLED = !switchLED;
            delay(200);

            if(switchLED){
                Serial.println("LED_PIN active");
            }else{
                Serial.println("LED_PIN2 active");
            }
        }
            
        if(switchLED){
            digitalWrite(LED_PIN, HIGH);
            digitalWrite(LED_PIN2, LOW);
            delay(200);    
        }else{
            digitalWrite(LED_PIN, LOW);
            digitalWrite(LED_PIN2, HIGH);
            delay(200);
        }    
    }else{
        digitalWrite(LED_PIN, LOW);
        digitalWrite(LED_PIN2, LOW);
        if( digitalRead(BUTTON_PIN) ) {
            buttonText = !buttonText;
            delay(200);

            if(buttonText){
                Serial.println("Type C first to activate the previous button function");
            }else{
                Serial.println("Type C first to activate the previous button function");
            } 
        }
    }


Below you can see some gif's with the functionality of the code. You already saw the first message when the programm is succesfully launched. As i said at the start the button functions as a switch. If you press the button you can see a message on the serial monitor so you know which LED is currently on.

If the user types in x now both LED's will go off and the button will show a message everytime you press it.

Unfortunately i dont have a gif from the message output but you can comprehend if you look at the code.

Download

In this week we just created the code so there is nothing to download. You can just copy the arduino code above and insert it into your arduinoIDE.