Hi,
I'm a new Arduino owner. I have a fair understanding of programming with loops in Visual Basic, but the C++ syntax is still very strange to me.
I've worked through the beginner tutorials that came with my Arduino kit an decided to experiment by combining two tutorials - the one tutorial makes a single LED glow when the board gets digital input from a push button connected to 5V. The other tutorial simulates a LED chase effect between 6 LED's. I tried to make the board wait for me to press the button before it starts the loop which switches the LED's on and off. I works in that the LED's cycles on and off while I hold in the button. What surprised me however, is that all 6 the LED's light up an stay on when I power up the board or load the sketch. Please can someone explain to me why this happens as the only place where the instruction is given to power any LED is after the button is pressed. (Sketch is below...)
Thanks in advance!
Johan
int BASE = 2;
int NUM = 6;
int inpin = 8;
int val;
void setup()
{
pinMode(inpin, INPUT);
for (int i = BASE; i < BASE + NUM; i++)
{
pinMode(i, OUTPUT);
}
}
void loop()
// tried this first For loop to switch everything off which might be on
{
for (int i = BASE; i < BASE + NUM; i++)
{
digitalWrite(i, 0);
delay(200);
}
val = digitalRead(inpin);
if (val == 1)
{
for (int i = BASE; i < BASE + NUM; i++)
{
digitalWrite(i, 0);
delay(200);
}
for (int i = BASE; i < BASE + NUM; i++)
{
digitalWrite(i, 1);
delay(200);
}
}
else
{
for (int i = BASE; i < BASE + NUM; i++)
{
digitalWrite(i, 0);
delay(200);
}
}
}