I'm trying to do a basic LED loop flashing the LEDs on back and forth, my code is
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
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?
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.
Is there a faster way to set pins 2-13 as output besides calling pinMode for each individual pin?
I have
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
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...
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.