Pages: [1]   Go Down
Author Topic: Help with Night Rider style LED flash loop  (Read 837 times)
0 Members and 1 Guest are viewing this topic.
Georgia, US
Offline Offline
Sr. Member
****
Karma: 4
Posts: 371
Arduino makes my head hurt :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to do a basic LED loop flashing the LEDs on back and forth, my code is

Code:
int LED = 2; //Set the initial LED pin to 2
int LEDDirection = 1; //Set the direction LEDs flash in

void setup() {
  pinMode(LED, OUTPUT);
}

void loop() {
  LED = LED + LEDDirection;
  digitalWrite(LED, HIGH);
  
  if (LED == 2 || LED == 13) { LEDDirection = -LEDDirection; }
  
  delay(100);
}

Yea, my commenting kind of died off early

And my circuit is pins 2-13 have jumpers coming from them into the bread board, current limiting resistors jump to the LEDs (two values, 100 and 330) then all 12 LEDs go to the negative rail, and I have a single jumper jumping to the GND pin

(offtopic: Does anyone know of a decent open source/free circuit drawing program? (Google's after hitting post))


The problem, only the first (13) LED lights up (though the first in the sequence is supposed to be 2), 13 blinks rather randomly, sometimes it'll be on for two or three seconds, sometimes it blinks on and off in half a second...

All of the other LEDs are dimly lit, and some of them flash randomly as well...

*shrug* if you need further clarification of anything, just ask
« Last Edit: January 08, 2011, 09:31:56 pm by Valalvax » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You expect to have 12 output pins. 11 of them are INPUT, by default. Only pin 2 is explicitly declared as OUTPUT. Might be worthwhile fixing that.

At some point, you might want to consider turning some of the LEDs off.

« Last Edit: January 08, 2011, 10:28:11 pm by PaulS » Logged

Georgia, US
Offline Offline
Sr. Member
****
Karma: 4
Posts: 371
Arduino makes my head hurt :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bah... didn't think about that... >.> assumed when LED changed it would automatically turn off the old LED and turn on the new LED and would also set it as output

(referring to the int, of course)

So the same pin can't be an output and input in the same sketch?
« Last Edit: January 08, 2011, 10:29:50 pm by Valalvax » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So the same pin can't be an output and input in the same sketch?
Sure it can. You just need to call pinMode() at the right point.

By default all pins are INPUT. If you want to use them as output, which is the whole purpose of plugging an LED in, they need to be declared as OUTPUT.
Logged

Georgia, US
Offline Offline
Sr. Member
****
Karma: 4
Posts: 371
Arduino makes my head hurt :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ahh, I see...

Is there a faster way to set pins 2-13 as output besides calling pinMode for each individual pin?

I have

Code:
int LED = 2; //Set the initial LED pin to 2
int LEDDirection = 1; //Set the direction LEDs flash in

void setup() {
}

void loop() {
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED, LOW);
  LED = LED + LEDDirection;
  
  if (LED == 2 || LED == 13) { LEDDirection = -LEDDirection; }
  
  delay(100);
}

Which functions well, but I'd like to leave the last pin lit for a fraction of a second, as it feels kind of odd, maybe light the next led for the same fraction of a second early

I tried moving the command to turn off the LEDs to after the command to switch like this

Code:
void loop() {
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);
  delay(200);
  LED = LED + LEDDirection;
  delay(10);
  digitalWrite(LED - 1, LOW);

but that resulted in all sorts of shenanigans

First it lights up the leds and turns them off the same as the top sketch works, but when it reaches pin 13 that stays on and it lights all other leds leaving them on, then turns them off after reaching 2

Also, thanks for the help with my fancy shmancy thing...
« Last Edit: January 08, 2011, 10:52:43 pm by Valalvax » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there a faster way to set pins 2-13 as output besides calling pinMode for each individual pin?
Code:
for(int i=2; i<=13; i++)
{
   pinMode(i, OUTPUT);
}
It's not faster, but it is shorter. Do this in setup(), since the mode of the pins never changes.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As for lighting the LEDs, use for loops:

Code:
for(int i=2; i<=13; i++)
{
   digitalWrite(i, HIGH);
   delay(200);
   digitalWrite(i, LOW);
   delay(10);
}

for(int i=12; i>2; i--)
{
   digitalWrite(i, HIGH);
   delay(200);
   digitalWrite(i, LOW);
   delay(10);
}
The first loop with light all the pins going one direction, for a period of time, with a small delay between lights. The other loop will light all but the end pins, so the end pins are not on for twice as long as the middle pins.
Logged

Georgia, US
Offline Offline
Sr. Member
****
Karma: 4
Posts: 371
Arduino makes my head hurt :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also, how do you use/have more inputs/outputs than the 12 available on the board?
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Multiplexing, having a serial to parallel converter chip (shift register, I think they're called), getting an arduino mega..

Charlieplexing would be the easiest solution if you want to make the hardware yourself and only have one LED on at a time.
Logged

Pages: [1]   Go Up
Jump to: