Frobo V3 programming lesson Part 2
In part 2 of Froboduino programming lessons, we will look at connection and control of Frobo LEDs as glwoing eyes. Look at the Arduino Led blinking code below for basic understanding. This kit contains 12 Leds in three colours; red, green, yellow, RGB. Have a close look at any Led. Each Led (Other than RGB) has two legs each with different lengths. The longer leg is +ve and shorter one is -ve (Ground). Like any other Diode, Led also allows current to flow from +ve to -ve. If connected wrongly, the Led will not work. Connect left Led to L1+ and L1 -ve ports and similarly connect right Led to L2 ports. Insert the Leds to eye holes on upper frame of Frobo. Connect your Frobo micro USB port to your computer and select Arduino Nano and ports (As we learnt in lesson Part 1).
Now, time to look at the code.
//Let us first declare which pins of froboduino are connected to left Led and right Led.
// On froboduino these are connected to programming pin 9 and pin 10 (Both are PWM capable, we will learn about that shortly)
// We will discuss later why actual pin numbers on 328P are different from programming pin numbers.
const int ledLeft = 9;
const int ledRight = 10;
// setup code runs once when the froboduino is switched on.
void setup()
{
// Here, we declare that ledLeft and ledRight are Outputs. It is important. Inputs are taken by default.
pinMode(ledLeft, OUTPUT);
pinMode(ledRight, OUTPUT);
//This is meant for program testing in serial monitor.
Serial.begin(9600);
}
// This code runs again and again when froboduino is powered.
void loop()
{
//This code is to turn left Led on for 100 milli seconds then off for 100 mili seconds. i second = 1000 milli seconds
Serial.println("Left LED On");
digitalWrite(ledLeft, HIGH);
// This stops the program for 100 milliseconds, but last commands will continue. Left Led will remain on for 100 milli seconds.
delay(100);
Serial.println("Left LED Off");
digitalWrite(ledLeft, LOW);
delay(100);
//This code is to turn right Led on for 100 milli seconds then off for 100 milli seconds.
Serial.println("Right LED On");
digitalWrite(ledRight, HIGH);
delay(100);
Serial.println("Right LED Off");
digitalWrite(ledRight, LOW);
delay(100);
}
Here is Video for your reference...
Copy the above the code, and paste in your arduino dialog screen and save. After saving the code, click on "Compile" on top right of your IDE and see if the code complies ok. Now, if your froboduino is connected to your computer via Arduino uno, click "Upload". See the bottom of IDE for uploading progress window. If your IDE says "Done uploading", your program has been successfully uploaded on your froboduino. Your leds will start blinking alternately. Whoo.., you have just become an Froboduino/Arduino programmer. Congratulations !.
This is just the beginning of your Froboduino journey. Let us look at some important things to know at this moment.
"delay() function is very often used in Arduino programming, but this function causes the program to stop while it lasts. The micro processor cannot handle any commands during this period. It is recommended to use 'delay()' only when the MCU is not required to handle any other commands or read "Input data".
How do we blink Leds without using the 'delay()' function.
Below is the code that will help you understand how to use millis() function to do just the same. 'millis()' is very useful function that uses the timer to calculate time from the last event and then executes the command scheduled after that. The good thing is that this function doesn't stop the program from doing other tasks.
Copy the code below.
// Declare pins for your Leds. const int, #define are used to define fixed pins. const int ledLeft = 9; const int ledRight = 10; //Declare led state variables below. These define the current state of Leds. Notice that we are setting one state "LOW" and the other state "HIGH" int ledLeftState = LOW; int ledRightState = HIGH; /*Declare time interval variables below. Notice that we are using "unsigned long" here and we used "int" earlier for led states. Why? Please know about these different variable types below.*/ unsigned long previousMillis = 0; unsigned long interval = 100; unsigned long currentMillis = 0; // setup code runs once when the froboduino is swtched on. void setup() { // Here, we declare that ledLeft and ledRight are Outputs. It is important. Inputs are taken by default. pinMode(ledLeft, OUTPUT); pinMode(ledRight, OUTPUT); //This is meant for program testing in serial monitor. Serial.begin(9600); } // This code runs again and again when froboduino is powered. void loop() { //This is to set the value of "currentMillis" equal to milli seconds since the program started. currentMillis = millis(); /*previousMillis began with value 0, so we check the difference of currentMillis and previousMillis, and if this bigger than or equal to "interval" (defined in variables in starting), we command to change the led states*/ if ((currentMillis - previousMillis) >= interval){ previousMillis = currentMillis; if (ledLeftState == LOW && ledRightState == HIGH){ ledLeftState = HIGH; ledRightState = LOW; } // if the above statement is not correct, that is if two Led states are different, we give an "otherwise" command with "else". else { ledLeftState = LOW; ledRightState = HIGH; } // Here we command the left and right Leds to follow the states. If ledLeftState is HIGH, the left Led will turn on. digitalWrite(ledLeft, ledLeftState); digitalWrite(ledRight, ledRightState); /*You can read about various variable types here. https://www.arduino.cc/en/Reference/VariableDeclaration Variable "int" is 2 byte (1 byte is 8 bits, so it is 16 bit number) variable which can be both negative and positive number. The range of "int" variable value is -32,768 to 32,767, which is equal to -2^15 to 2^15-1. Variable "unsigned long" is 4 byte (32 bit) number with maximum value of 2^32-1, which is 4,294,967,295. All unsigned variables are only positive numbers. This is a useful variable to store time in milli second and micro second. IMPORTANT- After the variable value reaches the maximum, it rolls over to the minimum value.*/ } } |
Here is the video for yur reference....
I think, this is already too much for just lesson part 2. So we will stop here. Think deeper about the functions described above. Try to create your own code to for creating diffrent patterns for Leds blinking. Let us know on this forum if you have any questions.