Embedded Programming


Navigation
  1. Setting up the pcb with the bootloader
  2. Connecting the pcb with an FTDI cable
  3. Arduino IDE

Setting up the pcb with the bootloader

To program the pcb we are now using an FTDI cable.
But first we have to use the Arduino one more time.

Simply follow the steps from the last two assignments by using the Arduino as ISP and uploading the bootloader.
(Upload the ArduinoISP code to the Arduino > Burn the bootloader to the pcb)


Connecting the pcb with an FTDI cable

(These steps are only for the 1284p now)
Disconnect the Arduino with the pcb from your PC. Now you need to install an FTDI driver on your PC if you do not already have one.
Afterwards connect the cable with the board.
Look into the datasheet, eagle boardlayout and the FTDI pinout to do so.

Keep in mind that you now have to connect TXD and RXD.
I connected GND, VCC, TXD, RXD and RESET.

If that does not work try to swap the TXD and RXD pin connection or use the other reset pin.

Programming with the ArduinoIDE

This is my finished code.
I will explain the main parts in detail.

                    
    #define LED_PIN_ONE 11
    #define LED_PIN_TWO PB2
    #define BUTTON_PIN PB4
    
    boolean ledswitch = true;
    char message[19] = "Program has started";
    
    void setup() {
        // put your setup code here, to run once:
        pinMode(LED_PIN_ONE, OUTPUT);
        pinMode(LED_PIN_TWO, OUTPUT);
        pinMode(BUTTON_PIN, INPUT);
    
        Serial.begin(115200);
    
    
        for ( int i = 0; i < 19; i++) {
            Serial.write(message[i]);
        }
        Serial.write("\n");
    }
    
    void loop() {
        // put your main code here, to run repeatedly:
    
        if ( digitalRead(BUTTON_PIN) ) {
            ledswitch = !ledswitch;
            Serial.write("LED Switched\n");
            delay(100);
        }
    
        if (ledswitch) {
            digitalWrite(LED_PIN_ONE, HIGH);
            digitalWrite(LED_PIN_TWO, LOW);
            delay(100);
        } else {
            digitalWrite(LED_PIN_ONE, LOW);
            digitalWrite(LED_PIN_TWO, HIGH);
            delay(100);
        }
    
    
        if (Serial.available()) {
    
            char readValue = Serial.read();
            boolean button = true;
        
            if (readValue  == '1') {
                button = true;
                Serial.println("Entered Mode 1");
        
                while (button) {
                    digitalWrite(LED_PIN_ONE, HIGH);
                    digitalWrite(LED_PIN_TWO, HIGH);
            
                    if ( digitalRead(BUTTON_PIN) ) {
                        button = false;
                        Serial.println("Button pressed. Mode 1 left.");
                        delay(100);
                    }
                }
            }
        
            if (readValue  == '2') {
                button = true;
                Serial.println("Entered Mode 2");
        
                while (button) {
                    digitalWrite(LED_PIN_ONE, HIGH);
                    digitalWrite(LED_PIN_TWO, HIGH);
            
                    delay(100);
            
                    digitalWrite(LED_PIN_ONE, LOW);
                    digitalWrite(LED_PIN_TWO, LOW);
            
                    delay(100);
                    if ( digitalRead(BUTTON_PIN) ) {
                        button = false;
                        Serial.println("Button pressed. Mode 2 left.");
                        delay(100);
                    }
                }
            }
    
            if (readValue  == '3') {
                bool set = false;
                Serial.println("Entered Mode 3");
                Serial.println("Write a number:");

                serialFlush();
                while (!set) {
                    if (Serial.available()) {
                        int times = Serial.parseInt();
                        Serial.println(times);
                        for (int j = 1; j <= times; j++) {
                            digitalWrite(LED_PIN_ONE, HIGH);
                            digitalWrite(LED_PIN_TWO, HIGH);
                
                            delay(100);
                
                            digitalWrite(LED_PIN_ONE, LOW);
                            digitalWrite(LED_PIN_TWO, LOW);
                
                            delay(100);
                        }
                        set = true;
                        Serial.println("Mode 3 left.");
                    }
                }

                digitalWrite(LED_PIN_ONE, LOW);
                digitalWrite(LED_PIN_TWO, LOW);
            }
        }
    }
    
    void serialFlush() {
        while (Serial.available() > 0) {
        char t = Serial.read();
        }
    }
                    
                

In the setup I printed in the serial monitor a message which I defined using a character array.

                    
    char message[19] = "Program has started";
                    
                
                    
    for ( int i = 0; i < 19; i++) {
        Serial.write(message[i]);
    }
    Serial.write("\n");
                    
                

This code switches a boolean to true or false when the button is pressed.
The LEDs will switch every time too.
I used a delay do not let the button switch more than once in 100 ms.

                    
    if ( digitalRead(BUTTON_PIN) ) {
        ledswitch = !ledswitch;
        Serial.write("LED Switched\n");
        delay(100);
    }

    if (ledswitch) {
        digitalWrite(LED_PIN_ONE, HIGH);
        digitalWrite(LED_PIN_TWO, LOW);
        delay(100);
    } else {
        digitalWrite(LED_PIN_ONE, LOW);
        digitalWrite(LED_PIN_TWO, HIGH);
        delay(100);
    }
                    
                

The

if (Serial.available())
is called when we write something in the serial monitor.
You can open it in the ArduinoIDE by opening tools > serial monitor. Make sure to use the right rate (115200 Baud). mode1.gif

I am saving the character I wrote in the serial monitor and setting a boolean to enter and leave my while loops.

                    
    char readValue = Serial.read();
    boolean button = true;
                    
                

This is the mode 1.
It just switches both LEDs on by typing 1 in the serial monitor until one presses the button.

The Serial.println function types the String in the serial monitor.

                    
    if (readValue  == '1') {
        button = true;
        Serial.println("Entered Mode 1");

        while (button) {
            digitalWrite(LED_PIN_ONE, HIGH);
            digitalWrite(LED_PIN_TWO, HIGH);
    
            if ( digitalRead(BUTTON_PIN) ) {
                button = false;
                Serial.println("Button pressed. Mode 1 left.");
                delay(100);
            }
        }
    }    
                    
                
mode1.gif

The second mode flashes the LEDs until the button is pressed.

                    
    if (readValue  == '2') {
        button = true;
        Serial.println("Entered Mode 2");

        while (button) {
            digitalWrite(LED_PIN_ONE, HIGH);
            digitalWrite(LED_PIN_TWO, HIGH);
    
            delay(100);
    
            digitalWrite(LED_PIN_ONE, LOW);
            digitalWrite(LED_PIN_TWO, LOW);
    
            delay(100);
            if ( digitalRead(BUTTON_PIN) ) {
                button = false;
                Serial.println("Button pressed. Mode 2 left.");
                delay(100);
            }
        }
    }
                    
                
mode2.gif

The third mode waits for an input and uses this number to flash the LEDs according to the input.
For example if I write 25 both LEDs will go on and off 25 times.

I am also using a function

serialFlush()
to empty the serial to get the right value from my monitor.
It just reads until the serial is empty.

                    
    if (readValue  == '3') {
        bool set = false;
        Serial.println("Entered Mode 3");
        Serial.println("Write a number:");

        serialFlush();
        while (!set) {
            if (Serial.available()) {
                int times = Serial.parseInt();
                Serial.println(times);
                for (int j = 1; j <= times; j++) {
                    digitalWrite(LED_PIN_ONE, HIGH);
                    digitalWrite(LED_PIN_TWO, HIGH);
        
                    delay(100);
        
                    digitalWrite(LED_PIN_ONE, LOW);
                    digitalWrite(LED_PIN_TWO, LOW);
        
                    delay(100);
                }
                set = true;
                Serial.println("Mode 3 left.");
            }
        }

        digitalWrite(LED_PIN_ONE, LOW);
        digitalWrite(LED_PIN_TWO, LOW);
                    
                
                    
    void serialFlush() {
        while (Serial.available() > 0) {
            char t = Serial.read();
        }
    }
                    
                
mode3.gif normal.gif

Here is the output from the monitor.

mode1.gif mode1.gif