Thursday, 19 March 2015

Arduino Tips & Tricks: 

Making the blinky blink with a one liner

Any one who has ever used an Arduino would have certainly wrote a program to toggle  a LED. In fact it is the first code which you will run when you will get  an arduino board in your hand,this is one of the way of saying " Hello! world" from arduino or any other programable embedded system.
But the code we use to make the LED blink for the first time is not one of the most efficient way of making it happen. There can be number of ways of writing a code to achieve a particular objective . However there can be various pros and cons of going with a particular approach. While beginning the programming the first thing in the mind of a programmer is to accomplish the functionality and efficiency of the code both in terms of density of code and actual performance that it will provide is something that is on a sideline. But as you go pro you become hungry for greater performance and more condence code  rather then merely achieving the objective.
So here is a quick talk about the two approaches of making blinky blink,one is a beginner way and the second one is a way that you would prefer more as a pro.
const int ledPin=13;// give name to pin 13
void setup()// this runs just once
{
  pinMode(ledPin,OUTPUT);// set the pin 13 to output
  digitalWrite(ledPin,LOW);// pull pin 13 to ground
}
void loop()// keeps on running again and again
{
  digitalWrite(ledPin,HIGH);// turn on the LED
  delay(1000);// wait for 1sec.
  digitalWrite(ledPin,LOW);//turn off the led
  delay(1000);
  
}
The code above will give you a nicely blinking led 13 on the arduino board. You have achieved the functionality you intended to achieve.But there is a much better way of doing this.Here is a code below that provides same output but the code is simply better than before.
const int ledPin=13;
void setup()
{
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,LOW);
}
void loop()
{
  digitalWrite(ledPin,!digitalRead(ledPin));
  delay(1000);
}
How is it working?
Everytime the loop is executed arduino reads the state of pin no 13 to which our LED is connected using digitalRead(ledPin) then using the '!' NOT operator the value of state is altered ,Suppose the loop is under execution and the program checks the state of pin 13 for the first time,it should be low ,the NOT operator performs its action on this state and finally a HIGH is passed to the pin 13,then the program stops for 1Sec and then again when loop is executed this time the  state of ledPin is HIGH so  this time the pin is made LOW. This keeps on happening again and again
We have seen the two ways of making the LED blink. Both the codes perform the same task. lets see how much space does each of them actually take on an arduino.
You can see here the pro version takes 1242 bytes while the beginner version takes 1084 bytes of space on an Arduino. It hardly matters which code you use when it is just about making the LED blink but when the we are trying to do something more complex may be like listening to  a character over serial and making the LED blink accordingly the pro version is just a better way at the expense of a few more bytes.
Here is a quick code which listens for a particular character over the serial and when the character 'h' is received  it checks the state of LED on pin 13 and alters the state to HIGH if it is LOW and vise versa. After you upload this code on your arduino open the serial monitor write h and hit enter. the LED will glow
const int ledPin=13;
char val;
void setup()
{
  Serial.begin(9600);// Enable Serial at a baud rate of 9600
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,LOW);
}
void loop()
{
  if(Serial.available()>0)// gets the number of bytes available to read
  {
    val=Serial.read();// reads the character and stores in in val
    if(val=='h')// checks if the received character is 'h' or not
    {
  digitalWrite(ledPin,!digitalRead(ledPin));// toggles the LED
 
    }
  }
}
The codes used in this post can be downloaded here

No comments:

Post a Comment