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
Number of flashing lights. Code wrong ?
-
- Posts:3
- Joined:Sat Oct 05, 2013 10:21 am
- Location:Perth, Western Australia
- Contact:
Re: Number of flashing lights. Code wrong ?
Hi Benny,
Looks like you have an off by one error in your loop.
You are using
when you should be using
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.
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++) {
Code: Select all
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
Re: Number of flashing lights. Code wrong ?
Ahh ok I'll give that a try.
The book that comes with the Freetronics Eleven has a mistake then.
Thanks for your help.
The book that comes with the Freetronics Eleven has a mistake then.
Thanks for your help.
Re: Number of flashing lights. Code wrong ?
Here's the latest .pdf of the EXP kit guide:
http://cdn.shopify.com/s/files/1/0045/8 ... ide1.2.pdf
http://cdn.shopify.com/s/files/1/0045/8 ... ide1.2.pdf