Go Down

Topic: Random LED comes on in series loop (Read 757 times) previous topic - next topic

atrus

Jan 17, 2011, 09:28 pm Last Edit: Jan 17, 2011, 09:40 pm by atrus Reason: 1
I just got my SIK in today, freaking loving it. I've got 10 LEDs wired up in a line with the 330ohm resisters, which is the second example in their guide. First of all, I'm still not comfortable with how the main loop() function reacts with loop inside of it. I've created a function snake(), which basically runs a line of LEDs down the series. I got it working after some effort, but as soon as the snake overruns (and beings to disappear) the series, the third LED turns on. Is it my code, the wiring, or some symptom of overlapping loops?

I've ran the example code for my config, and it works just fine. I've switched out the LED, the wiring, switched the pin. All the same symptom. As soon as the "head" of the snake overflows the LED series, the third bulb, pin 4, lights up, and stays light until the snake passes over it again and it is turned off.

Here is my code:

Code: [Select]

int ledPins[] = {2,3,4,5,6,7,8,9,10,11};
int length = 4;
int head = 0;

void setup() {
 for(int i = 0; i < 10; i++){
   pinMode(ledPins[i],OUTPUT);
 }
}

void loop() {
 snake();
}

void snake() {
 int tail = (head-length > 0) ? head-length : 0;

 for (int i = 0; i < tail; i++) {
   digitalWrite(ledPins[i], LOW);
 }

 for (int i = tail; i < head; i++) {
   digitalWrite(ledPins[i], HIGH);
 }

 if (tail > 9) {
   head = 0;
 } else {
   head++;
 }
 
 delay(500);  
}

atrus

So I wrote in some debugging code, and it was then that it settled into my brain that I was definitely overrunning my led array, and I was surprised at how well the little thing seemed to be taking it. (I'm a lazy web programmer, so I'm used to this after all.) I decided that we should avoid this, and a simple if statement later, everything appears to be working. Feel free to suggest better code style, or whatever. But it appears to be working.

Code: [Select]

/*  
*  LED Snake!
*
*/
int ledPins[] = {2,3,4,5,6,7,8,9,10,11};
int length = 4;
int head = 0;

void setup() {
 Serial.begin(9600);
 for(int i = 0; i < 10; i++){
   pinMode(ledPins[i],OUTPUT);
 }
}

void loop() {
 snake();
}

void snake() {
 int tail = (head-length > 0) ? head-length : 0;

 Serial.println(head);
 Serial.println(tail);
 Serial.println("---");
 
 for (int i = 0; i < tail; i++) {
   digitalWrite(ledPins[i], LOW);
 }
 
 Serial.print("Turning on: ");
 for (int i = tail; i < head; i++) {
   Serial.print(i);
   Serial.print(",");
   if (i < 10) {
     digitalWrite(ledPins[i], HIGH);
   }
 }
 Serial.println();
 if (tail > 9) {
   head = 0;
 } else {
   head++;
 }
 
 delay(500);  
}



One full series in the serial log:
Code: [Select]

0
0
---
Turning on:
1
0
---
Turning on: 0,
2
0
---
Turning on: 0,1,
3
0
---
Turning on: 0,1,2,
4
0
---
Turning on: 0,1,2,3,
5
1
---
Turning on: 1,2,3,4,
6
2
---
Turning on: 2,3,4,5,
7
3
---
Turning on: 3,4,5,6,
8
4
---
Turning on: 4,5,6,7,
9
5
---
Turning on: 5,6,7,8,
10
6
---
Turning on: 6,7,8,9,
11
7
---
Turning on: 7,8,9,10,
12
8
---
Turning on: 8,9,10,11,
13
9
---
Turning on: 9,10,11,12,
14
10
---
Turning on: 10,11,12,13,

PaulS

Good job on finding your problem. I have one small suggestion. When you print debug data to the serial port, print some meaningful identification string in front of it.

Serial.print("Head: ");
Serial.print(head);
Serial.print("Tail: ");
Serial.print(tail);

This way, when you are looking at the output, it is very easy to see, when an unexpected value appears, what that value represents, making it much easier to find and fix the problem.

Go Up