Ok, first of all I will apologize as I have been a lurker here without an account since 2013 when I got my first Arduino. Since then almost all of my questions have been answered by searching this and other forums and tutorials. That being said I am now having an issue that I cannot seem to explain.
I am horrible at coding so I apologize how clunky my sketch is and if this is a reason for my problem please point out where.
I am making a DMX controller using a MAX485 breakout to control lights in a small private theater when we don't feel like taking out the big 144 channel control desk.
I am building a small box with 5 buttons and 6 LEDs to be able to choose from 7 basic options
Options one through 3 = House Lights at either Full, Half or Out
Options 4 through 7 = Stage Lights for Presentation, Video, Band or OFF
It is set up such that
button1 controls house to either full or half
button2 is presentation
button3 is video
button4 is band
button5 is House out
I have stripped out all of my DMX code so that I can use Serial since the DMX uses the TX and RX lines.
For some reason intermittently the code seems to be ignoring the fixed debounce delay and flashing the chosen scene on and off faster than the debounce should allow. I have my debounce delay set at 1000ms and I am seeing my previous time of only 84s and it is changing again even without the button being pressed. if I press it again it stops flashing and behaves for a while till this glitch seems to happen randomly again
here is my code, I think I put it here appropriately. again I apologize I am not very good at writing sketches.
/*Trial sketch for debugging
*
*
*/
int button1 = 3; //Scene1 - House on/half - Red Buton
int button2 = 4; //Scene2 - Speaker
int button3 = 5; //Scene3 - Speaker w/video
int button4 = 6; //Scene4 - Band
int button5 = 7; //Scene5 - House OUT
int led1 = 8; //Scene1 - On Green
int ledH = 9; //Scene1 - Half Yellow
int led2 = 10; //Scene2 Green
int led3 = 11; //Scene3 Green
int led4 = 12; //Scene4 Green
int led5 = 13; //Scene5 Blue
//one additional LED directly between VCC and GND as power indicator
bool sceneH = true;
bool scene2 = false;
bool scene3 = false;
bool scene4 = false;
bool scene5 = false;
unsigned long debounce = 1000; //This is the debounce delay make longer if engagement is unreliable
unsigned long t1 = 0;
unsigned long t2 = 0;
unsigned long t3 = 0;
unsigned long t4 = 0;
unsigned long t5 = 0;
unsigned long lt1 = 0;
unsigned long lt2 = 0;
unsigned long lt3 = 0;
unsigned long lt4 = 0;
unsigned long lt5 = 0;
void setup() {
Serial.begin(9600);
Serial.println("Startup");
//Set Modes for pins
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(led1, OUTPUT);
pinMode(ledH, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
Serial.println("modes established");
//Use LED5 as indicator of power on progress beginning
digitalWrite (led5, HIGH);
delay(100);
digitalWrite (led1, HIGH);
delay(10);
digitalWrite (led2, HIGH);
delay(10);
digitalWrite (led3, HIGH);
delay(10);
digitalWrite (led4, HIGH);
delay(10);
digitalWrite (ledH, HIGH);
delay(100);
digitalWrite (led1, LOW);
digitalWrite (led2, LOW);
digitalWrite (led3, LOW);
digitalWrite (led4, LOW);
digitalWrite (ledH, LOW);
delay(10);
Serial.println("Startup Complete");
house();
}
void loop() {
bool state1 = digitalRead(button1);
bool state2 = digitalRead(button2);
bool state3 = digitalRead(button3);
bool state4 = digitalRead(button4);
bool state5 = digitalRead(button5);
Serial.print(state1);
Serial.print(lt1);
Serial.print(" , ");
Serial.print(state2);
Serial.print(lt2);
Serial.print(" , ");
Serial.print(state3);
Serial.print(lt3);
Serial.print(" , ");
Serial.print(state4);
Serial.print(lt4);
Serial.print(" , ");
Serial.print(state5);
Serial.print(lt5);
Serial.print(" , ");
if (state1 == HIGH){
t1 = millis();
}
if (state2 == HIGH){
t2 = millis();
}
if (state3 == HIGH){
t3 = millis();
}
if (state4 == HIGH){
t4 = millis();
}
if (state5 == HIGH){
t5 = millis();
}
if ((t1-lt1)>debounce){
sceneH = !sceneH;
house();
lt1 = millis();
Serial.print("House-");
Serial.println(sceneH);
delay(10);
}
if ((t2-lt2)>debounce && t2>t3 && t2>t4){
scene2 = !scene2;
scene3 = false;
scene4 = false;
stage();
lt2 = millis();
Serial.print("Scene2-");
Serial.println(scene2);
}
if ((t3-lt3)>debounce && t3>t2 && t3>t4){
scene3 = !scene3;
scene4 = false;
scene2 = false;
stage();
lt3 = millis();
}
if ((t4-lt4)>debounce && t4>t2 && t4>t3){
scene4 = !scene4;
scene2 = false;
scene3 = false;
stage();
lt4 = millis();
}
if ((t5-lt5)>debounce){
scene5 = !scene5;
house();
lt5 = millis();
}
Serial.println("End Of Loop");
}
//-------------------Subroutine for Stage-----------------------
void stage(){
//-----Scene 2 Presentation No Video
if (scene2 == true) { //engage dimmers for Scene2 and write all other dimmers other dimmers to 0
digitalWrite(led2, LOW);
delay(100);
digitalWrite(led2, HIGH);
scene3 = false;
digitalWrite(led3, LOW);
scene4 = false;
digitalWrite(led4, LOW);
Serial.println("Presentation");
}
//-----Scene 3 Presentation WITH Video
if (scene3 == true) { //engage dimmers for Scene3 and write all other dimmers other dimmers to 0
digitalWrite(led3, HIGH);
delay(100);
digitalWrite(led3, HIGH);
scene2 = false;
digitalWrite(led2, LOW);
scene4 = false;
digitalWrite(led4, LOW);
Serial.println("Video");
}
//-----Scene 4 Band
if (scene4 == true) { //engage dimmers for Scene4 and write all other dimmers other dimmers to 0
digitalWrite(led4, LOW);
delay(10);
digitalWrite(led4, HIGH);
scene2 = false;
digitalWrite(led2, LOW);
scene3 = false;
digitalWrite(led3, LOW);
Serial.println("BAND");
}
if (scene4 == false && scene3 == false && scene2 == false) {
// if no scenes are engaged then kill the stage lights
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
Serial.println("No Stage Cue");
}
}
//------------------End Stage----------------------------------------
//-------------------Subroutine for House Lights--------------------
void house(){
//-----HOUSE FULL
if (sceneH == true) {
digitalWrite(led1, HIGH); // digital writes are just controlling LEDs for indicators
digitalWrite(ledH, LOW);
digitalWrite(led5, LOW);
Serial.println("House Full");
scene5 = false;
}
//-----House Lights To Half
if (sceneH == false && scene5 == false) {
digitalWrite(led1, LOW); // digital writes are just controlling LEDs for indicators
digitalWrite(ledH, HIGH);
digitalWrite(led5, LOW);
Serial.println("House at Half");
}
//-----House OUT
if (scene5 == true && sceneH == false){
//Turn house lights off
digitalWrite(led5, HIGH);
delay(10);
digitalWrite(led5, LOW);
delay(10);
digitalWrite(led5, HIGH);
digitalWrite(led1, LOW);
digitalWrite(ledH, LOW);
Serial.println("HOUSE OUT");
}
}
//----------End House Lights---------------------------