Blink LED's in a sequence while blinking an LED with a push button

Hello! I am working on some code for a Proton Pack from Ghostbusters. I would like five lights to blink in a sequence, and one stay on constantly. When I push a button, I would like another light to turn on and another light to blink. When I release the button, I need both lights to turn off.

I have all of this sort of functioning. The five LED's count in sequence and the one static light stays on just fine. When I push the push button, the light I want to light turn on, turns on, but slowly and the blinking light blinks very slowly. Adjusting the pot on A2 does not change this.

I think I have the (blink) function inserted incorrectly and the timing for the push button is getting fouled up.

Any help would be appreciated.

int cyclo = 1; //Index variable for cyclotron lights

int cyc1 = 2;
int cyc2 = 3;
int cyc3 = 4;
int cyc4 = 5;
int cyc5 = 6;
int ClippadLight = 7;

const byte fireLED = 10;
const byte BarrelLED = 11;
const byte button = 12;

int speed_adj = A1; // Cyclotron Pot
int Barrel_rate = A2; // Barrel Pot

int val = 0; //  Cyclotron Pot value
int barrel_adj = 0; // Barrel Pot value

// Barrel Logic
bool firing = false; //defines when blinking should occur
unsigned long blinkInterval = 250; // number of milliseconds for blink
unsigned long currentMillis; // variables to track millis()
unsigned long previousMillis;

void setup() 
{
//Serial.begin(9600);
pinMode(cyc1, OUTPUT); // Cyclo Light LED 1
pinMode(cyc2, OUTPUT); // Cyclo Light LED 2
pinMode(cyc3, OUTPUT); // Cyclo Light LED 3
pinMode(cyc4, OUTPUT); // Cyclo Light LED 4
pinMode(cyc5, OUTPUT); // Cyclo Light LED 5
pinMode(ClippadLight, OUTPUT); // Clippard Light comes on when the Nano is powered on
pinMode(fireLED, OUTPUT);
pinMode(BarrelLED, OUTPUT);
pinMode(button, INPUT);
}

void blink (const byte which)
{
  digitalWrite(ClippadLight, HIGH);
  val = analogRead(speed_adj);
  digitalWrite(which, HIGH);
  delay(val);
  digitalWrite(which, LOW);
  delay(val);
  }  

void loop()
{
  int stateButton = digitalRead(button);
  barrel_adj = analogRead(Barrel_rate);
//this will turn the fireLED ON if the push button is pressed and OFF if it is not
  if(stateButton == 1) { 
     digitalWrite(fireLED, LOW); 
  } else { //if not pressed
     digitalWrite(fireLED, HIGH);  
  }

    //controls the strobe rate of the barrel light
  if (firing) {
  currentMillis = millis(); // better to store in variable, for less jitter
  if ((unsigned long)(currentMillis - previousMillis) >= barrel_adj) { // enough time passed yet?
   digitalWrite(BarrelLED, !digitalRead(BarrelLED)); // shortcut to toggle the LED
   previousMillis = currentMillis; // sets the time we wait "from"
  }
 } else {
  digitalWrite(BarrelLED, LOW); // force LED off when not blinking
   }
   
 int reading = digitalRead(button);
 delay(50); // crude de-bouncing
 
 if (reading==LOW) // buttons with pull-up are pressed when LOW
  firing=true; // start blinking
 else
  firing=false; // stop blinking

digitalWrite(cyc1, LOW);
digitalWrite(cyc2, LOW);
digitalWrite(cyc3, LOW);
digitalWrite(cyc4, LOW);
digitalWrite(cyc5, LOW);

  blink (2);
  blink (3);
  blink (4);
  blink (5);
  blink (6);

}

Does "turns on, but slowly" mean it is dimmer and not getting sufficient power, or does it appear to be operating at the correct brightness just not blinking as quickly as desired? Are you powering all the lights off of the Arduino?

It’s is hardly ever a good idea to use delay() and millis()/BWD in the same sketch.

Study ‘state machine’ programming examples, some examples in the tutorial section.

The brightness of the LED's are correct. I am powering them through the Arduino. They blink rate is very slow and when I adjust the pot, there is no effect.

Print the value from the analog input to see what is being read.

Your pots may be wired incorrectly.

Rewrite blink() to get rid of delay().

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:
https://forum.arduino.cc/index.php?topic=519037.0

BTW, why are you making this?

I do not think the wiring is incorrect. When I separate these sketches, they work correctly. When I combine the pushbutton idea with the five lights, it doesn't work as I intend it to; when the push button is pressed, the LED blinks very slowly.

I am building a Proton Pack from Ghostbusters. The five lights are the red lights seen in the movie and the pushbutton will activate the light at the end of the gun assembly to blink rapidly while another light turns on. I like having pots to adjust the rate of speed.

Showing us the schematic and wiring is for our benefit, so we can see exactly what is happening.

Did you print the analog value to the serial monitor, if so what were the results? if not why not?

Can you understand how this code section replaces your blink() function and how it gets rid of delay()s?

//************************************************************************
void blinkLEDs()
{
  //machine state variable
  static byte mState = 0;

  //read potentiometer
  val = analogRead(speed_adj);

  //***************************
  if (millis() - cycMillis < val)
  {
    //not time yet
    return;
  }

  //restart timer
  cycMillis = millis();

  //***************************
  switch (mState)
  {
    //**************
    case 0:
      {
        //LED on
        digitalWrite(LEDpin, HIGH);
        //next state
        mState++;
      }
      break;

    //**************
    case 1:
      {
        //LED OFF
        digitalWrite(LEDpin, LOW);
        //next state
        mState++;
      }
      break;

    //**************
    default:
      {
        //back to the begining state
        mState = 0;

        //next LED
        LEDpin++;
        //have we finished with the top LED?
        if (LEDpin > cyc5)
        {
          //back to the 1st LED
          LEDpin = cyc1;
          //Note: in this example, LEDs must be sequential values, i.e. 2,3,4,5,6
        }
      }
      break;

  } //END of switch/case


} //END of blinkLEDs()

//************************************************************************

Is this a school assignment?

Thank you for your help LarryD. This is not a school assignment, I am doing this on my own time.
I have built the logic in discrete electronics for the proton pack and I figured I could give it a try with an Arduino. It has been more challenging than I anticipated. I have taken some C++ courses in high school and college. Those days are long behind me and I am having more trouble than I care to admit wrapping my head around the logic. I think I need a course in this stuff so I can take full advantage of the Arduino.

Multiple question have been asked of you, but you do not supply the answers to them, why?

The first rule is: when someone is willing to help 'you' with 'your' project, 'you' should give them help in return. :frowning:

Here is your code properly done.

//Version 2

const byte cyc1 = 2; //first sequence LED
const byte cyc2 = 3;
const byte cyc3 = 4;
const byte cyc4 = 5;
const byte cyc5 = 6; //last sequence LED

const byte ClippadLight = 7;  //power ON LED
const byte fireLED      = 10; //ON when button switch is pressed
const byte BarrelLED    = 11; //flashes when button switch is pressed

const byte button       = 12; //switch pressed = LOW

const byte speed_adj    = A1; //Cyclotron potentiometer
const byte Barrel_rate  = A2; //Barrel potentiometer

byte LEDpin = cyc1;           //current sequence LED being timed
byte lastbuttonState;

bool firing = false;          //true = blinking should occur

int val        = 0;           //Cyclotron potentiometer value
int barrel_adj = 0;           //Barrel potentiometer value

//timing variables
unsigned long firingMillis;
unsigned long cycMillis;
unsigned long switchMillis;


//************************************************************************
void setup()
{
  //Serial.begin(9600);
  pinMode(cyc1, OUTPUT); //Cyclo Light LED 1
  pinMode(cyc2, OUTPUT); //Cyclo Light LED 2
  pinMode(cyc3, OUTPUT); //Cyclo Light LED 3
  pinMode(cyc4, OUTPUT); //Cyclo Light LED 4
  pinMode(cyc5, OUTPUT); //Cyclo Light LED 5

  //Clippard Light comes on when the Nano is powered on
  pinMode(ClippadLight, OUTPUT);
  digitalWrite(ClippadLight, HIGH);

  pinMode(fireLED, OUTPUT);
  digitalWrite(fireLED, LOW);

  pinMode(BarrelLED, OUTPUT);
  digitalWrite(BarrelLED, LOW);

  //pressed = LOW, could use INPUT_PULLUP to save on the external pull up resistor
  pinMode(button, INPUT);
  lastbuttonState = digitalRead(button);

} //END of setup()


//************************************************************************
void loop()
{
  //***************************
  //handle the switch
  readSwitch();

  //***************************
  //handle the firing LED
  firingLED();

  //***************************
  //handle the sequence LEDs
  blinkLEDs();

} //END of loop()


//************************************************************************
void readSwitch()
{
  //***************************
  //de-bounce time expired?
  if (millis() - switchMillis < 50)
  {
    //not time yet
    return;
  }

  //reset the timer
  switchMillis = millis();

  byte stateButton = digitalRead(button);

  //***************************
  //has the switch changed state?
  if (lastbuttonState != stateButton)
  {
    //update to the new state
    lastbuttonState = stateButton;

    //switch is pressed?
    if (stateButton == LOW)
    {
      //LED ON
      digitalWrite(fireLED, HIGH);

      //enable blinking
      firing = true;
      //reset the timer
      firingMillis = millis();
    }

    //switch is not pressed
    else
    {
      //LED OFF
      digitalWrite(fireLED, LOW);

      //disable blinking
      firing = false;
    }

  } //END of if (lastbuttonState != stateButton)

} //END of readSwitch()


//************************************************************************
void firingLED()
{
  //***************************
  //strobe enabled?
  if (firing == true)
  {
    //read potentiometer
    barrel_adj = analogRead(Barrel_rate);

    //timer expired?
    if (millis() - firingMillis >= barrel_adj)
    {
      //reset the timer
      firingMillis = millis();

      //toggle LED
      digitalWrite(BarrelLED, !digitalRead(BarrelLED));
    }

  } //END of if(firing == true)

  //strobe disabled
  else
  {
    //LED OFF
    digitalWrite(BarrelLED, LOW);
  }

} //END of firingLED()


//************************************************************************
void blinkLEDs()
{
  //machine state variable
  static byte mState = 0;

  //read potentiometer
  val = analogRead(speed_adj);

  //***************************
  //timer expired?
  if (millis() - cycMillis < val)
  {
    //not time yet
    return;
  }

  //reset the timer
  cycMillis = millis();

  //***************************
  switch (mState)
  {
    //**************
    case 0:
      {
        //LED on
        digitalWrite(LEDpin, HIGH);
        //next state
        mState++;
      }
      break;

    //**************
    case 1:
      {
        //LED OFF
        digitalWrite(LEDpin, LOW);

        //back to the begining state
        mState = 0;

        //next LED
        LEDpin++;
        //have we finished with the top LED?
        if (LEDpin > cyc5)
        {
          //back to the 1st LED
          LEDpin = cyc1;
          //Note: in this example, LEDs must be sequential values, i.e. 2,3,4,5,6
        }
      }
      break;

    //**************
    default:
      {
      }
      break;

  } //END of switch/case

} //END of blinkLEDs()

//************************************************************************

EDIT:
Version 2 code, adjusted sequence LED timing.