Embedded programing

This week assignment

In this week assingment we should get our hands dirty, and programm a bit on our own. The programm should be made with ArduinoIDE or c++ to burn it on the bootloader. Part of the assingment was, that the program is able to detect a button input and to present a seriel output.

For this we will use the PCB we created last week. Show in the Electronic Design assignment for more detailed information. We already created it with the idea that we could change the glowing LED via the button so that always only one is activ.

Source code

One thing to mention here are the software seriel.

Due to a lack of previous knowledge my PCB came without serial connections therefore. Foolishly I am, this serial connections are importent for seriel communication between the PC and the PCB.

                      

With the softwareseriel library, we can work around this problem by fooling the microship with one of the connected pins, in this case the MISO pin, which makes it a serial pin. Furthermore, we must define in advance for each pin used in the code whether this is an input or an output pin.

Now I explain the actual code. The crucial code comes in the loop function. This is repeatedly repeated by the processor, looped. Klaudia.print (); is for the serial text output. It gets more interesting. With this query we continuously check if the button has been pressed. So if the assigned input pin receives a HIGH. furthermore there is &&! Timerinteruppt. the && is an AND linked. This means that here are two conditions which both have to be TRUE so that when the instruction is executed. Timerinteruppt is a pre-created bool variable. Bool means that this can only be TRUE or FALSE. By the exclamation mark in front of the variable attribution, we reverse the content in the sense of the query. So he's asking: is Pin7 = HIGH AND TimerInterrput = false? only if both are true will the statement be executed. but why do we need a timer interrupt? I use this variable to make sure that you can not just hold down the button. Looking further through the code you will notice that I set this variable to true as soon as the first statement is met. Later in the code, there is a query that asks the other way around: Is Pin7 = Low AND TimerInterrupt = true? Only if this is the case will TimerInterrupt be set to false again. So we can be sure that one button press only one LED jumps on.

Now if you have pressed the button there is another query which checked which LED is currently lit. For this I use an integer variable, LEDCount. If it is already 10 and we press the button, it will be set to 8. But if it has another value, it will be increased by 1. With this method, the variable can contain three values. 8,9,10. How convenient I have used three LEDs. Stop! BUT why 8,9,10 and not 1,2,3? Very easily. Because 8,9,10 are the pins used to fire the LED.