Hi
I am working on a three LED, four button project at present and have got to a stage where it works, but not exactly how I need it to and after reviewing the forums for other user experiences and queries, I would would like to seek your advice.
Currently I have a button assigned to activate each LED colour, with the fourth button clearing the sequence/turning all active LEDs off. However I would like to change this so that the GREEN led only turns on when both the SWITCH_MASTER & SWITCH_GREEN buttons are pressed together..
Once I have solved this issue, I may extend my MASTER switch to activate another output when pressed on its own. But I am not yet ready for that as I am trying to get this working first (but to no avail).
I have seen in previous forum posts that "even if you hit it with a hammer you can not press both buttons at the same time", however I am hoping this maybe different, as my MASTER_SWITCH will in fact be pressed and held down, whilst the SWITCH_GREEN is then pressed.
In addition, I would also like to change my RED LED to blink/flash, rather than just coming on and staying on. I did see in another forum posting a suggestion of
digitalWrite(pin, millis() & 0x100);
but have not managed to get this to work. My LED would not activate at all when the button was pressed ![]()
Please can I ask if you can review the below code to see whether there is something that conflicts/prevents the blink and for suggestions to get the two buttons to trigger the GREEN Led together?
Additionally I'd appreciate any feedback as to whether you think this code is ok or if there are areas that need cleaning up or whether there is something else I should be using?
Below is my current code, which I have adapted from other projects and suggestions within the forum:
const byte maxDataLength = 20;
char receivedChars[21] ;
boolean newData = false;
const byte RED_PIN = 9;
const byte AMBER_PIN = 10;
const byte GREEN_PIN = 11;
const byte SWITCHRED_PIN = 3;
const byte SWITCHAMBER_PIN = 4;
const byte SWITCHGREEN_PIN = 2;
const byte SWITCHMASTER_PIN = 5;
boolean RED_State = false;
boolean AMBER_State = false;
boolean GREEN_State = false;
boolean SWITCHRED_State = false;
boolean SWITCHAMBER_State = false;
boolean SWITCHGREEN_State = false;
boolean SWITCHMASTER_State = false;
boolean oldSWITCHRED_State = false;
boolean oldSWITCHAMBER_State = false;
boolean oldSWITCHGREEN_State = false;
boolean oldSWITCHMASTER_State = false;
void setup()
{
pinMode(SWITCHRED_PIN, INPUT);
pinMode(SWITCHAMBER_PIN, INPUT);
pinMode(SWITCHGREEN_PIN, INPUT);
pinMode(SWITCHMASTER_PIN, INPUT);
pinMode(RED_PIN, OUTPUT); digitalWrite(RED_PIN,LOW);
pinMode(AMBER_PIN, OUTPUT); digitalWrite(AMBER_PIN,LOW);
pinMode(GREEN_PIN, OUTPUT); digitalWrite(GREEN_PIN,LOW);
Serial.begin(9600);
Serial.println(" ");
}
void loop()
{
checkSwitch();
recvWithStartEndMarkers();
if (newData) { processCommand(); }
}
void checkSwitch()
{
boolean state1 = digitalRead(SWITCHRED_PIN); delay(1);
boolean state2 = digitalRead(SWITCHRED_PIN); delay(1);
boolean state3 = digitalRead(SWITCHRED_PIN); delay(1);
boolean state4 = digitalRead(SWITCHRED_PIN); delay(1);
if ((state1 == state2) && (state1==state3)&& (state1==state4))
{
SWITCHRED_State = state1;
if ( (SWITCHRED_State == HIGH) && (oldSWITCHRED_State == LOW) )
{
RED_State = ! RED_State;
if ( RED_State == HIGH)
{
digitalWrite(AMBER_PIN,LOW);
digitalWrite(GREEN_PIN,LOW);
digitalWrite(RED_PIN,HIGH);
Serial.println("RED");
}
}
oldSWITCHRED_State = SWITCHRED_State;
}
// Simple toggle switch function with very simple debouce.
state1 = digitalRead(SWITCHAMBER_PIN); delay(1);
state2 = digitalRead(SWITCHAMBER_PIN); delay(1);
state3 = digitalRead(SWITCHAMBER_PIN); delay(1);
state4 = digitalRead(SWITCHAMBER_PIN); delay(1);
if ((state1 == state2) && (state1==state3) && (state1==state4))
{
SWITCHAMBER_State = state2;
if ( (SWITCHAMBER_State == HIGH) && (oldSWITCHAMBER_State == LOW) )
{
AMBER_State = ! AMBER_State;
if ( AMBER_State == HIGH)
{
digitalWrite(GREEN_PIN,LOW);
digitalWrite(RED_PIN,LOW);
digitalWrite(AMBER_PIN,HIGH);
Serial.println("AMBER");
}
}
oldSWITCHAMBER_State = SWITCHAMBER_State;
}
// Simple toggle switch function with very simple debouce.
state1 = digitalRead(SWITCHGREEN_PIN); delay(1);
state2 = digitalRead(SWITCHGREEN_PIN); delay(1);
state3 = digitalRead(SWITCHGREEN_PIN); delay(1);
state4 = digitalRead(SWITCHGREEN_PIN); delay(1);
if ((state1 == state2) && (state1==state3)&& (state1==state4))
{
SWITCHGREEN_State = state3;
if ( (SWITCHGREEN_State == HIGH) && (oldSWITCHGREEN_State == LOW) )
{
GREEN_State = ! GREEN_State;
if ( GREEN_State == HIGH)
{
digitalWrite(RED_PIN,LOW);
digitalWrite(AMBER_PIN,LOW);
digitalWrite(GREEN_PIN,HIGH);
Serial.println("GREEN");
}
}
oldSWITCHGREEN_State = SWITCHGREEN_State;
}
// Simple toggle switch function with very simple debouce.
state1 = digitalRead(SWITCHMASTER_PIN); delay(1);
state2 = digitalRead(SWITCHMASTER_PIN); delay(1);
state3 = digitalRead(SWITCHMASTER_PIN); delay(1);
state4 = digitalRead(SWITCHMASTER_PIN); delay(1);
if ((state1 == state2) && (state1 == state3)&& (state1 == state4))
{
SWITCHMASTER_State = state4;
if ( (SWITCHMASTER_State == HIGH) && (oldSWITCHMASTER_State == LOW) )
{
GREEN_State = ! GREEN_State;
if ( GREEN_State == HIGH)
{
digitalWrite(AMBER_PIN,LOW);
digitalWrite(RED_PIN,LOW);
digitalWrite(GREEN_PIN,LOW);
Serial.println("ALL LIGHTS OFF");
}
}
oldSWITCHGREEN_State = SWITCHGREEN_State;
}
}
void processCommand()
{
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
}
