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?
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.
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.
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()
//************************************************************************
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.