Number of flashing lights. Code wrong ?

The Experimenters Kit includes an "Eleven" Arduino-compatible board, plus a range of parts and a guide to make it easy to get started with Arduino projects. [Product Page]
Post Reply
bennyp85
Posts: 4
Joined: Fri Oct 25, 2013 7:41 am

Number of flashing lights. Code wrong ?

Post by bennyp85 » Fri Oct 25, 2013 7:49 am

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

gregington
Posts: 3
Joined: Sat Oct 05, 2013 10:21 am
Location: Perth, Western Australia
Contact:

Re: Number of flashing lights. Code wrong ?

Post by gregington » Sat Oct 26, 2013 4:34 am

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.

bennyp85
Posts: 4
Joined: Fri Oct 25, 2013 7:41 am

Re: Number of flashing lights. Code wrong ?

Post by bennyp85 » Sat Oct 26, 2013 10:07 am

Ahh ok I'll give that a try.
The book that comes with the Freetronics Eleven has a mistake then.
Thanks for your help.

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Number of flashing lights. Code wrong ?

Post by andrew » Sun Oct 27, 2013 1:38 am

Here's the latest .pdf of the EXP kit guide:
http://cdn.shopify.com/s/files/1/0045/8 ... ide1.2.pdf

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests