Monday 6 April 2015

Using Arrays and For loop

The following code demonstrates the simple use of Arrays Using for loop
In this code four Led's are On and Off Sequentially.


/*
Author : Kunchala Anil
demonstrating the use of Arrays using For loop
*/
int led[] = {8,9,10,11}; // initialize the 4 pins using array
void setup(){
Serial.begin(9600);
//for loop is used to set all pins to Outputs
for(int i=0;i<4;i++){
pinMode(led[i],OUTPUT);
Serial.print("pin\t");
Serial.print(i);
Serial.print("\t is set as Output")
Serial.println("");
}//end of for loop
}//end of setup() function

void loop(){
//for loop is used to On and Off Led's Sequentially
for(int j=0;j<4;j++){
digitalWrite(led[j],HIGH);
Serial.print("LED\t");
Serial.print(j);
Serial.print("\t is ON")
Serial.println("");
delay(1000);
digitalWrite(led[j],LOW);
Serial.print("LED\t");
Serial.print(j);
Serial.print("\t is OFF")
Serial.println("");

}//end of for loop
}//end of loop

No comments:

Post a Comment