for the sake of the users who are just getting started with embedded system arduino will be very much ok for a beginners/starter, and it has a wide variety of appilcations from automations, robotics and IOTs and many more.
The tutorial will be focusing on the elementary explanations of arduino codes to the advanced level ranging from blinking of LEDs to IOT, ROBOTICS and the usage of millis(); micros(); and timer0,timer1,timer2, spi, i2c, uart, pwm and many more.
for the sake of the tutorial our targeted board will be arduino uno since it is a very popular and available board and can be used to program all atmega328p micro controllers. and all the necessary functions needed is readily available ranging from i/o pins, timers, pwm, spi, i2c,uart and many more. just activate by writing and instructions to the registers.
our first project will be blinking of LEDs.
the board comes with an inbuilt led connected to pin 13.i.e digital pin 13.
Note: arduino codes are called sketch.
connect a led with a current limiting resistor of 330ohms to the pin 13 of the arduino board and connect the negative of the led to the point marked Gnd on the arduino board, or conversly if you do not have a resistor no need to worry just use the inbuilt led attached to the pin 13. and copy the sketch below to the arduino ide.
the sketch below is to turn on the led on and off at an interval of 1 secs .
//led blinking code
const int ledpin=13; //led connected to pin 13
void setup(){
pinMode(ledpin,OUTPUT); //set led to output
}
void loop(){
digitalWrite(ledpin, HIGH); //set led on
delay(1000); //led goes on for 1 sec
digitalWrite(ledpin,LOW); //set led off
delay(1000); //led goes off for 1 sec
}
////////////////////////////////////////////////////////////////////////////////
if you want the led to only go on 10 times and go off 10 times you can copy the code below
int ledpin=13;
int k;
int duration=20;
void setup(){
pinMode(ledpin, OUTPUT);
blink(); //calling the blink function
}
void blink(){
for(k=0;k<duration;k++) //blink 10 times and go off 10 times and the code terminate.
{
digitalWrite(ledpin,HIGH);
delay(1000);
digitalWrite(ledpin,LOW);
delay(1000);
}
void loop()
{
//do another thing.
}
millis() Tutorial: Arduino Multitasking
The millis() function is one of the most powerful functions of the Arduino library. This function returns the number of milliseconds the current sketch has been running since the last reset. At first, you might be thinking, well that’s not every useful! But consider how you tell time during the day. Effectively, you look at how many minutes have elapsed since midnight. That’s the idea behind millis()!
Instead of “waiting a certain amount of time” like you do with delay(), you can use millis() to ask “how much time has passed”? Let’s start by looking at a couple of ways you can use delay() to flash LEDs.
Example #1: Basic Delay
You are probably already familiar with this first code example The Arduino IDE includes this example as “Blink.”
1 2 3 4 5 6 7 8 9 10 | void setup () { pinMode ( 13 , OUTPUT ); } void loop () { digitalWrite ( 13 , HIGH ); // set the LED on delay ( 1000 ); // wait for a second digitalWrite ( 13 , LOW ); // set the LED off delay ( 1000 ); // wait for a second } |
Reading each line of loop() in sequence the sketch:
- Turns on Pin 13’s LED,
- Waits 1 second (or 1000milliseconds),
- Turns off Pin 13’s LED and,
- Waits 1 second.
- Then the entire sequence repeats.
The potential issue is that while you are sitting at the delay(), your code can’t be doing anything else. So let’s look at an example where you aren’t “blocking” for that entire 1000 milliseconds.
Example #2: Basic Delay with for() loops
For our 2nd example, we are only going to delay for 1ms, but do so inside of a for() loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | void setup () { pinMode ( 13 , OUTPUT ); } void loop () { digitalWrite ( 13 , HIGH ); // set the LED on for ( int x = 0 ; x < 1000 ; x + + ) { // Wait for 1 second delay ( 1 ); } digitalWrite ( 13 , LOW ); // set the LED on for ( int x = 0 ; x < 1000 ; x + + ) { // Wait for 1 second delay ( 1 ); } } |
This new sketch will accomplish the same sequence as Example #1. The difference is that the Arduino is only “delayed” for one millisecond at a time. A clever trick would be to call other functions inside of that for() loop. However, your timing will be off because those instructions will add additional delay.
Example #3: for() loops with 2 LEDs
In this example, we’ve added a second LED on Pin 12 (with a current limiting resistor!). Now let’s see how we could write the code from Example #2 to flash the 2nd LED.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | void setup () { pinMode ( 13 , OUTPUT ); pinMode ( 12 , OUTPUT ); } void loop () { digitalWrite ( 13 , HIGH ); // set the LED on for ( int x = 0 ; x < 1000 ; x + + ) { // wait for a secoond delay ( 1 ); if (x = = 500 ) { digitalWrite ( 12 , HIGH ); } } digitalWrite ( 13 , LOW ); // set the LED off for ( int x = 0 ; x < 1000 ; x + + ) { // wait for a secoond delay ( 1 ); if (x = = 500 ) { digitalWrite ( 12 , LOW ); } } } |
Starting with the first for() loop, while Pin 13 is high, Pin 12 will turn on after 500 times through the for() loop. It would appear that Pin 13 and Pin 12 were flashing in Sequence. Pin 12 would turn on 1/2 second after Pin 13 turns on. 1/2 second later Pin 13 turns off, followed by Pin 12 another 1/2 second.
This code is pretty complicated, isn’t it?
If you wanted to add other LEDs or change the sequence, you have to start getting ingenious with all of the if-statements. Just like example #2, the timing of these LEDs is going to be off. The if() statement and digitalWrite() function all take time, adding to the “delay()”.
Now let’s look at how millis() gets around this problem.
The millis() Function
Going back to the definition of the millis() function: it counts the number of milliseconds the sketch has been running.
Step back and think about that for a second. In Example #3, we are trying to flash LEDs based on a certain amount of time. Every 500ms we want one of the LEDs to do something different. So what if we write the code to see how much time as passed, instead of, waiting for time to pass?
Example #4: Blink with millis()
This code is the same “Blink” example from #1 re-written to make use of millis(). A slightly more complicated design, because you have to include a couple of more variables. One to know how long to wait, and one to know the state of LED on Pin 13 needs to be.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | unsigned long interval = 1000 ; // the time we need to wait unsigned long previousMillis = 0 ; // millis() returns an unsigned long. bool ledState = false ; // state variable for the LED void setup () { pinMode ( 13 , OUTPUT ); digitalWrite ( 13 , ledState); } void loop () { unsigned long currentMillis = millis (); // grab current time // check if "interval" time has passed (1000 milliseconds) if (( unsigned long )(currentMillis - previousMillis) > = interval) { ledState = ! ledState; // "toggles" the state digitalWrite ( 13 , ledState); // sets the LED based on ledState // save the "current" time previousMillis = millis (); } } |
Let’s look at the code from the very beginning.
1 2 | unsigned long interval = 1000 ; // the time we need to wait unsigned long previousMillis = 0 ; // millis() returns an unsigned long. |
millis() returns an unsigned long.
The variable interval is the amount of time we are going to wait. The variable previousMillis is used so we can see how long it has been since something happened.
1 | bool ledState = false ; // state variable for the LED |
This code uses a variable for the state of the LED. Instead of directly writing a “HIGH” or “LOW” with digitalWrite(), we will write the value of this variable.
The setup() function is pretty standard, so let’s skip directly to the loop():
1 2 | void loop () { unsigned long currentMillis = millis (); // grab current time |
Each time loop() repeats, the first thing we do is grab the current value of millis(). Instead of repeated calls to millis(), we will use this time like a timestamp.
1 2 | void loop () { if (( unsigned long )(currentMillis - previousMillis) > = interval) { |
Holy cow does that look complicated! It really isn’t, so don’t be afraid to just “copy and paste” this one. First, it is an if()-statement. Second the (unsigned long) isn’t necessary (but I’m not going to sit around for 49 days to make sure). Third “(currentMillis – previousMillis) >= interval)” is the magic.
What it boils down to, this code will only become TRUE after millis() is at least “interval” larger than the previously stored value of millis, in previousMillis. In other words, nothing in the if()-statement will execute until millis() gets 1000 milliseconds larger than previousMillis.
The reason we use this subtraction is that it will handle the roll-over of millis. You don’t need to do anything else.
ledState = ! ledState; |
This line of code will set the value of ledState to the corresponding value. If it is true, it becomes false. If it is false, it becomes true.
digitalWrite ( 13 , ledState); // sets the LED based on ledState |
Instead of writing a HIGH or LOW directly, we are using a state variable for the LED. You’ll understand why in the next example.
previousMillis = millis (); |
The last thing you do inside of the if-statement is to set previousMillis to the current time stamp. This value allows the if-statement to track when at least interval (or 1000ms) has passed.
Example #5: Adding a 2nd LED with millis()
Adding a second flashing LED is straightforward with the millis() code. Duplicate the if-statement with a second waitUntil and LEDstate variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | // each "event" (LED) gets their own tracking variable unsigned long previousMillisLED12 = 0 ; unsigned long previousMillisLED13 = 0 ; // different intervals for each LED int intervalLED12 = 500 ; int intervalLED13 = 1000 ; // each LED gets a state varaible boolean LED13state = false ; // the LED will turn ON in the first iteration of loop() boolean LED12state = false ; // need to seed the light to be OFF void setup () { pinMode ( 13 , OUTPUT ); pinMode ( 12 , OUTPUT ); } void loop () { // get current time stamp // only need one for both if-statements unsigned long currentMillis = millis (); // time to toggle LED on Pin 12? if (( unsigned long )(currentMillis - previousMillisLED12) > = intervalLED12) { LED12state = ! LED12state; digitalWrite ( 12 , LED12state); // save current time to pin 12's previousMillis previousMillisLED12 = currentMillis; } // time to toggle LED on Pin 13? if (( unsigned long )(currentMillis - previousMillisLED13) > = intervalLED13) { LED13state = ! LED13state; digitalWrite ( 13 , LED13state); // save current time to pin 13's previousMillis previousMillisLED13 = currentMillis; } } |
This code has some small changes from the previous example. There are separate previousMillis and state variables for both pins 12 and 13.
To get an idea of what’s going on with this code change the interval values for each. Both LEDs will blink independently of each other.
please note: when ever you use millis(); function on arduino you can't make use of timer0 for any other function again.
Conclusion
At first it seems that using the millis() function will make a sketch more complicated than using delay(). In some ways, this is true. However, the trade-off is that the sketch becomes significantly more flexible. Animations become much simpler. Talking to LCDs while reading buttons becomes no problem. Virtually, you can make your Arduino multitask.
Comments