Having problems getting leds to twinkle in a sketch

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();
     }
 ```

this is the twinkle i can get working

// Create a new variable called ledPin which will hold the value 9.
// We're using the value 9 because our LED is connected to Digital Pin 9.
int led1 = 9;
int led2 = 10;
int led3 = 11;
int led4 = 6;
int led5 = 5;

// This is the void setup() function.
// Code within this function runs only once.
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 loop() {

  // 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));
}

The first code you posted does not compile. It has a couple of syntax errors.

You should fix the errors and clean up all the commented lines

Also, in

        if (state > 5) {
          state = 0;
        }

The state can be set to 0, which is not a valid state.

If the switch is pressed for more than a few milliseconds, the program will cycle very fast through all states (with a short pause in state 2).

twinkle() does not need the pinMode instructions. It is OK to leave them just in setup(). analogWrite does need the pinMode to be set, anyway.

random(200) returns a number between 0 and 199. When you add 150 you get a number between 150 and 349. analogWrite expects a number between 0 and 255. You can use

   analogWrite(led1,random(256));

I don't see that the fade() function is called

Check the debounce example in the Arduino IDE>Examples>02.Digital to learn an alternate (and effective) way to read a button state

The first code i posted compiles fine on my ide, i didnt write any of the codes, i just managed to smash the two together. As for the “state” referencing the button pushes its worked in a previous sketch ive done for a black panther statue. I have disabled calling the fade function temporarily as im trying to get the flicker part to work first. Calling the pinmode works already so i havnt changed it.

Well, analogWrite will control the average voltage delivered to a pin using PWM, from 0 to 5V. This means that the pin will be turned on and off 490 times per second. You will not be able to notice the flickering (or twinkle). The leds will appear to stay on each at a different brightness level for whatever random (300) milliseconds last.

If this is new info for you, you may find detailed explanations of how PWM works and how it is used to control LED/lamp brightness, motor speeds, etc. in this forum and in the internet at large.

I changed the random (200)+150) to just random(200). Had zero effect. The thing that puzzles me is how can the twinkle sketch work, and not the new one when the functions were just copied and pasted, everything else seems the same. I even pasted the analogWrite etc etc directly after the switch state, in place of digitalwrite led1 high..

   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);

What is the purpose of the above code? Why read the pin 5 times? If this is an attempt to debounce the button, there are far simpler ways, and 4ms is probably not long enough anyway, you probably need to wait 20~30ms to avoid picking up any switch bounce.

     if ( newSwitchState1 != oldSwitchState ) 
        {
 
           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);
 
           }

In the above, your twinkle() function gets called only once, at the instant the button changes from LOW to HIGH. You need it to be called repeatedly in order for it to create the effect you want.

Almost everything in your code is repeated 5 times over. You should learn to use arrays.

as i said before i wrote pretty much none of the sketches, just piecing them together like frankensteins monster. the sketch im making is for a black panther statue with lights and sounds triggered with each button press 5 or 6 in total. ive coppied the switching things sketch which ive been using as the base for the current sketch

//  Sketch: SwitchingThings_04
//
//  An  example of using a single button switch to set multiple states or conditions
//
//  Pins
//  D10 to resister and green LED
//  D9 to resister and yellow LED
//  D8 to resister and red LED
//  D2 to push button switch
//  
//  state holds the current status.
//  0 = all off.
//  1 = green LED
//  2 = yellow LED
//  3 = red LED
 
// Define the pins being used
int pin_LEDgreen = 10;
int pin_LEDyellow = 9;
int pin_LEDred = 8;
 
int pin_switch = 2;
 
// variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState1 = LOW;
boolean newSwitchState2 = LOW;
boolean newSwitchState3 = LOW;
 
byte state = 0;
 
void setup() 
{
    Serial.begin(9600);
    Serial.print("Sketch:   ");   Serial.println(__FILE__);
    Serial.print("Uploaded: ");   Serial.println(__DATE__);
    Serial.println(" ");
 
    pinMode(pin_LEDgreen, OUTPUT);    digitalWrite(pin_LEDgreen,LOW); 
    pinMode(pin_LEDyellow, OUTPUT);   digitalWrite(pin_LEDyellow,LOW); 
    pinMode(pin_LEDred, OUTPUT);      digitalWrite(pin_LEDred,LOW); 
 
    pinMode(pin_switch, INPUT); 
}
 
void loop()
{
    newSwitchState1 = digitalRead(pin_switch);
    delay(1);
    newSwitchState2 = digitalRead(pin_switch);
    delay(1);
    newSwitchState3 = digitalRead(pin_switch);
 
    // if all 3 values are the same we can continue
    if (  (newSwitchState1==newSwitchState2) && (newSwitchState1==newSwitchState3) )
    {
 
        if ( newSwitchState1 != oldSwitchState ) 
        {
 
           // has the button switch been closed?
           if ( newSwitchState1 == HIGH )
           {
                // increase the value of state
                state++;
                if (state > 3) { 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(pin_LEDgreen, LOW);
                digitalWrite(pin_LEDyellow, LOW);
                digitalWrite(pin_LEDred, 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(pin_LEDgreen, HIGH);  }
                if (state==2) { digitalWrite(pin_LEDyellow, HIGH);  }
                if (state==3) { digitalWrite(pin_LEDred, HIGH);  }
 
           }
           oldSwitchState = newSwitchState1;
        }  
    }
}

im really new at coding and probably in too deep, but learning slowly..

If you don't know how or why each individual piece of code works on it's own, you can't expect to know why the two of them don't work together, can you? You'll be better served by writing something simpler that you understand fully and work up from there.

I didn't look through your program in any detail, but getting rid of those blocking delays you have may be a good start.

So as an update i have various led effects running with each button push, the flicker or twinkle function is killing me. The leds are on steady, they dont seem to flicker at all. Ive tried numerous libraries and different examples using thise libraries then when implemented into my sketch the lights are just steady. Can flickering light effects even be added with other effects?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.