Minggu, 29 Juli 2012

Arduino language tutorial (Lesson 2)

LESSON 2 :

Using a Pushbutton to Control the LED

To monitor the state of a switch, there’s a new Arduino instruction that you’re going to learn: the digitalRead()function.
digitalRead()checks to see whether there is any voltage applied to the pin that you specify between parentheses, and returns a value of HIGH or LOW, depending on its findings.

assemble the circuit as shown where the switch is connected to digital pin number 7 :

// Example 02: Turn on LED while the button is pressed

const int LED = 13;                              // the pin for the LED
const int BUTTON = 7;                       // the input pin where the pushbutton is connected
int val = 0;                                           // val will be used to store the state of the input pin
void setup() 
{
pinMode(LED, OUTPUT); // tell Arduino LED is an output
pinMode(BUTTON, INPUT); // and BUTTON is an input
}
void loop()
{
val = digitalRead(BUTTON);                       //read input value and store it 
// check whether the input is HIGH (button pressed)
if (val == HIGH) 
{
digitalWrite(LED, HIGH); // turn LED ON
else 
{
digitalWrite(LED, LOW);
}
}

in this sketch the LED is on when the switch is pressed but when the switch is released the LED is turned off (i.e. the LED is on as long as the switch is pressed )

NOW we want to change this sketch so that the LED is on when the switch is pressed and still on even if the switch is released and when the switch is pressed again the LED is turned off .
in this case we want to store the state of the LED ( for example when the LED is off assign a variable with zero and when the LED is on assign this variable with 1) as shown in the following sketch :

// Example 03A: Turn on LED when the button is pressed and keep it on after it is released 

const int LED = 13;                                  // the pin for the LED
const int BUTTON = 7;                           // the input pin where the pushbutton is connected
int val = 0;                                               // val will be used to store the state of the input pin
int state = 0;                                            // 0 = LED off while 1 = LED on
void setup() 
{
pinMode(LED, OUTPUT);              // tell Arduino LED is an output
pinMode(BUTTON, INPUT);        // and BUTTON is an input
}
void loop() 
{
val = digitalRead(BUTTON);                     // read input value and store it
// check if the input is HIGH (button pressed) and change the state
if (val == HIGH) {
state = 1 - state;
}
if (state == 1) {
digitalWrite(LED, HIGH);                               // turn LED ON
} else {
digitalWrite(LED, LOW);
}
}

Now go test this code. You will notice that it works … somewhat. You’ll find that the light changes so rapidly that you can’t reliably set it on or off with a button press.

because the time you press the button the sketch is executed thousands of times and the state of the LED is changed thousand times so we want to detect only the change in the voltage applied to the pin (i.e the positive edge change of the button and this can be done by the following sketch ) :
// Example 03B: Turn on LED when the button is pressed and keep it on after it is released
// Now with a new and improved formula!

const int LED = 13;                           // the pin for the LED
const int BUTTON = 7;                 // the input pin where the pushbutton is connected
int val = 0;                                      // val will be used to store the state of the input pin
int old_val = 0;                                 // this variable stores the previous value of "val"
int state = 0;                                   // 0 = LED off and 1 = LED on
void setup()
{
pinMode(LED, OUTPUT); // tell Arduino LED is an output
pinMode(BUTTON, INPUT); // and BUTTON is an input
}
void loop()
{
val = digitalRead(BUTTON);            // read input value and store it yum, fresh

// check if there was a transition
if ((val == HIGH) && (old_val == LOW)){
state = 1 - state;
}
old_val = val;                       // val is now old, let's store it
if (state == 1) {
digitalWrite(LED, HIGH);           // turn LED ON
} else {
digitalWrite(LED, LOW);
}
}

END OF LESSON 2




- Reference : O'REILLY  Getting Started with Arduino by Massimo Banzi  , 2nd Edition.

0 komentar:

Posting Komentar