hi, im having trouble getting a set of 5 leds to twinkle or flicker, im using an arduino nano. i can get the sketch to work seperately, as in i upload and it runs constantly, but im trying to merge it with another sketch for switching things. the sketch uses a pushbutton to select 5 modes of lighting effects. 1 press i had a fade on and off effect and on the second press its supposed to twinkle but the lights are just on and the 5th led on pin 5 is dimmer.`They both use the analogWrite and pins are all pwm. once this works i can continue with the rest of the effects
> const int led1 = 11;
const int led2 = 10;
const int led3 = 9;
const int led4 = 6;
const int led5 = 5;
int pin_switch = 2;
int brightness = 0;
// variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState1 = LOW;
boolean newSwitchState2 = LOW;
boolean newSwitchState3 = LOW;
boolean newSwitchState4 = LOW;
boolean newSwitchState5 = LOW;
byte state = 0;
void setup()
{
pinMode(led1, OUTPUT); // Make Digital Pin 9 an OUTPUT
pinMode(led2, OUTPUT); // Make Digital Pin 9 an OUTPUT
pinMode(led3, OUTPUT); // Make Digital Pin 9 an OUTPUT
pinMode(led4, OUTPUT); // Make Digital Pin 9 an OUTPUT
pinMode(led5, OUTPUT); // Make Digital Pin 9 an OUTPUT
}
void fade()
{
for (brightness = 0; brightness <= 255; brightness += 5) {
analogWrite(9, brightness);
analogWrite(10, brightness);
delay(30); // Wait for 30 millisecond(s)
}
for (brightness = 255; brightness >= 0; brightness -= 5) {
analogWrite(9, brightness);
analogWrite(10, brightness);
delay(30); // Wait for 30 millisecond(s)
}
}
void twinkle() {
pinMode(led1, OUTPUT); // Make Digital Pin 9 an OUTPUT
pinMode(led2, OUTPUT); // Make Digital Pin 9 an OUTPUT
pinMode(led3, OUTPUT); // Make Digital Pin 9 an OUTPUT
pinMode(led4, OUTPUT); // Make Digital Pin 9 an OUTPUT
pinMode(led5, OUTPUT); // Make Digital Pin 9 an OUTPUT
// Set the brightness of the LED to a random value.
analogWrite(led1, random(200)+150);
analogWrite(led2, random(200)+150);
analogWrite(led3, random(200)+150);
analogWrite(led4, random(200)+150);
analogWrite(led5, random(200)+150);
// Randomly delay for a period of time between 0 and 0.3 seconds.
delay(random(300));
}
void loop() {
newSwitchState1 = digitalRead(pin_switch);
delay(1);
newSwitchState2 = digitalRead(pin_switch);
delay(1);
newSwitchState3 = digitalRead(pin_switch);
delay(1);
newSwitchState4 = digitalRead(pin_switch);
delay(1);
newSwitchState5 = digitalRead(pin_switch);
if ( (newSwitchState1==newSwitchState2) && (newSwitchState1==newSwitchState3) && (newSwitchState1==newSwitchState4) && (newSwitchState1==newSwitchState5))
if ( newSwitchState1 != oldSwitchState )
{
// has the button switch been closed?
if ( newSwitchState1 == HIGH )
{
// increase the value of state
state++;
if (state > 5) { state = 0; }
// turn all LEDs off. Doing it this way means we do not need to care about the individual LEDs
// simply turn them all off and then turn on the correct one.
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
// Turn on the next LED
// Because the value of state does not change while we are testing it we don't need to use else if
if (state==1) digitalWrite (led1, HIGH);
if (state==2) twinkle();
if (state==3) digitalWrite(led5, HIGH);
if (state==4) digitalWrite(led4, HIGH);
if (state==5) digitalWrite(led3, HIGH);
}
oldSwitchState = newSwitchState1;
}
}
//void effect1()
//{ digitalWrite(led1, HIGH);
// delay (500);
// digitalWrite(led2,HIGH);
// delay (500);
// digitalWrite(led3,HIGH);
// delay (500);
// digitalWrite(led4,HIGH);
// delay (500);
// digitalWrite(led5,HIGH);
// delay (28000);
// digitalWrite (led5,LOW);
// delay (500);
// digitalWrite (led4,LOW);
// delay (500);
// digitalWrite (led3,LOW);
// delay (500);
//digitalWrite (led2,LOW);
// delay (500);
// digitalWrite (led1,LOW); }
//void effect2()
//{ digitalWrite(led3, HIGH); }
//void effect3()
//{ analogWrite(led1, random(200)+150);
// analogWrite(led2, random(200)+150);
// analogWrite(led3, random(200)+150);
// analogWrite(led4, random(200)+150);
// analogWrite(led5, random(200)+150);
// Randomly delay for a period of time between 0 and 0.3 seconds.
// delay(random(300));; }
void blinks() {
digitalWrite (11, HIGH);
delay (500);
digitalWrite (11, LOW);
delay (100);
}
void blink10() {
for(int ii = 0; ii < 10; ii++){
blinks();
}
```