01 April 2010

Experiment with using 'int' variables in place of HIGH/LOW

/*
* Project: Experiment#1_simpleBlink
* Author: Jane-Maree Howard
* Date: Thursday 01/04/2010
* Platform: Arduino 18
* Purpose: To demonstrate variables in digitalWrite()
* Operation: Sets up output pin 13 in setup() method;
* Declares pin definition variable, delay variable, pin value variable
* Defines loop() to perform various LED blinking operations
*/

int ledPin = 13; // on-board LED connected to digital pin 13
int dLay = 250; // delay variable
int pinVal = 1; // pinVal = 1/0, == HIGH/LOW
int j;
int iFlash = 10; //flashing counter
// The setup() method runs once, when the sketch starts
void setup()
{
// initialize digital pin 13 as output
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, pinVal); // turn on-board LED on
}//end setup()

void loop()
{
//flashing sequence
for (j=0; j {
if (pinVal == 1) //if LED is ON..
pinVal = 0; //..turn it OFF..
else //..or if LED is OFF..
pinVal = 1; //..turn it ON
//now write to on-board LED
digitalWrite(ledPin, pinVal);
delay(50);
}//end for(j++) flash
//delay 1 second
delay(4*dLay);
digitalWrite(ledPin,LOW);
/* Above, I could have written
pinVal = 0;
digitalWrite(ledPin,pinVal);
and it would've produced the same result.
This technique enables more flexible manipulation of
'digitalWrite()' statements because you can set up your
parameter values in advance using, e.g. various 'if()'
statements then writing the parameter values to a single
'digitalWrite()' statement rather than having a whole
raft of them cluttering up your program (& gobbling up
excessive numbers of Bytes!) */

}//end loop()

No comments:

Post a Comment