Hello 8)
So, I am pretty new to arduino (5 days to be exact) and just today I received a pack of 100 LEDs from Amazon.
I have made some code so that when my toggle switch is off, the LEDs should flash on and off with a 500 ms delay and then the switch is on they should do a night rider type thing. However, when running my code, only the 3rd(pin 4) and 4t(pin 5) LEDs do as they should, the first 2 LEDs(pins 2 + 3) just stay off.
I have tried many things to fix this including:
- Test the LEDs
- Try different resistors (450ohm instead of 220ohm)
- Testing the pins in question
I am able to use another piece of code to simply blink 1 led and that works fine for each LED on each pin so i don't understand the problem with this. Most likely it is something to do with my code, but i really don't know.
Here is my code:
//name pins
// constant variables cat change and are used for naming pins
// non constant variables can change, such as the state of the button
const int led[] = {2, 3, 4, 5 };
const int but1 = 6;
int but1state = 1;
//set up the pins and board
void setup() {
//configure pins
pinMode(led[2], OUTPUT);
pinMode(led[3], OUTPUT);
pinMode(led[4], OUTPUT);
pinMode(led[5], OUTPUT);
pinMode(but1, INPUT);
digitalWrite(but1, HIGH); // internal pull up resistor
//test led's Calling upon function (at bottom)
butPress();
}
//main code to run forever
void loop() {
but1state = digitalRead(but1);
// because of internal pull up, 0 means on, 1 means off
if(but1state == 0){
butPress(); //Calling upon function (at bottom)
}
else{
digitalWrite(led[2], HIGH);
digitalWrite(led[3], HIGH);
digitalWrite(led[4], HIGH);
digitalWrite(led[5], HIGH);
delay(500);
digitalWrite(led[2], LOW);
digitalWrite(led[3], LOW);
digitalWrite(led[4], LOW);
digitalWrite(led[5], LOW);
delay(500);
}
}
//a bit of code to 'call' upon in code so i dont have to rewrite it multiple times (saves space)
void butPress() {
digitalWrite(led[2], HIGH);
delay(500);
digitalWrite(led[2], LOW);
digitalWrite(led[3], HIGH);
delay(500);
digitalWrite(led[3], LOW);
digitalWrite(led[4], HIGH);
delay(500);
digitalWrite(led[4], LOW);
digitalWrite(led[5], HIGH);
delay(500);
digitalWrite(led[5], LOW);
}
And this is the code I used to test each pin individually:
int led = 5;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}