The Assignment for this week was the following:
Develop program that:If you dont want to use the arduino uno to load your code on your board you can do the following:
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.