Page 1 of 1

Number of flashing lights. Code wrong ?

Posted: Fri Oct 25, 2013 7:49 am
by bennyp85
Hi how is everyone.
Got a quick question from an intro build on the Eleven.
Its just meant to scan through the four lights with a button attached and when pressed it stops the scan and lights just a single led up.
The problem is that only 3 led's are lighting up
Code
int ledCount = 4;
int ledPins[] = {13, 12, 11, 10};
int ledDelay = 50;
int buttonPin = 2;

void setup() {
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
pinMode (buttonPin, INPUT);
}

void loop() {
for (int thisLed = 0; thisLed < ledCount-1; thisLed++) {
digitalWrite(ledPins[thisLed], HIGH);
delay(ledDelay);
while(digitalRead(buttonPin) == HIGH) {
delay(10);
}
digitalWrite(ledPins[thisLed], LOW);
}
}

If I change intCount to 5 it lights up all 4 led's
Is this due to the button being attached ?
Thanks for your time
Benny P

Re: Number of flashing lights. Code wrong ?

Posted: Sat Oct 26, 2013 4:34 am
by gregington
Hi Benny,

Looks like you have an off by one error in your loop.

You are using

Code: Select all

for (int thisLed = 0; thisLed < ledCount-1; thisLed++) {
when you should be using

Code: Select all

for (int thisLed = 0; thisLed < ledCount; thisLed++) {
Because you starting counting from zero, you don't need to subtract one from the loop condition. The idiom in C is that you start counting from zero and test using a less than for the number of iterations required.

Re: Number of flashing lights. Code wrong ?

Posted: Sat Oct 26, 2013 10:07 am
by bennyp85
Ahh ok I'll give that a try.
The book that comes with the Freetronics Eleven has a mistake then.
Thanks for your help.

Re: Number of flashing lights. Code wrong ?

Posted: Sun Oct 27, 2013 1:38 am
by andrew
Here's the latest .pdf of the EXP kit guide:
http://cdn.shopify.com/s/files/1/0045/8 ... ide1.2.pdf