Author: Christina He
Difficulty Level: Level 3
Challenge Description: Make 5 LEDs to light up in orders: 1, 2, 3, 4, 5. And then light off in orders: 5, 4, 3, 2, 1. Then repeat.
Hint: Use for loop to count up and down.
Answer:
Code:
int ledPins[] = {1,2,3,4,5};
void setup()
{
for(int x=0; x<6;x++)
{
pinMode(ledPins[x],OUTPUT);
}
}
void loop()
{
int delaytime = 100;
for(int x=0; x<6; x++)
{
digitalWrite (ledPins [x], HIGH);
delay(delaytime);
}
for(int x=5; x>=0; x--)
{
digitalWrite (ledPins [x], LOW);
delay(delaytime);
}
}
Visual Answer:
Code:
int ledPins[] = {1,2,3,4,5};
void setup()
{
for(int x=0; x<6;x++)
{
pinMode(ledPins[x],OUTPUT);
}
}
void loop()
{
int delaytime = 100;
for(int x=0; x<6; x++)
{
digitalWrite (ledPins [x], HIGH);
delay(delaytime);
}
for(int x=5; x>=0; x--)
{
digitalWrite (ledPins [x], LOW);
delay(delaytime);
}
}
Visual Answer:
No comments:
Post a Comment