Hello,
I am trying to create a latching logic that is initiated by a serial command. For example, its supposed to operate so that when i send a 5 over serial, code will run. When i send a 5 again other code will run. I tried modifying the StateChangeDetection example without success. I can get code to run when the latch is on but I cannot get code to when it is turned off.
Im trying to modify the StateChangeDetection example so that I can run code depending on the state of the latch. If I send a 5 and the latch is on: do this. If I send another 5 and the latch turns off: do that. This is the part I cannot figure out.
I've been working at this for so long. I've read every forum post help guide i could find online without success. This is really kicking my butt. its 2:18AM and I'm completely out of ideas. I hope someone can give me guidance on this.
The area of interest is on lines 284 to 313
Note, there are some garbage integers declared that are not used.
I'm going to bed. Thanks in advance.
// constants won't change. They're used here to set pin numbers:
const int buttonPinA = 2; // the number of the pushbutton pin
const int buttonPinB = 3; // the number of the pushbutton pin
const int buttonPinC = 4; // the number of the pushbutton pin
const int buttonPinD = 5; // the number of the pushbutton pin
const int buttonPinM = 6; // the number of the pushbutton pin
const int ledPinA = 8; // the number of the LED pin
const int ledPinB = 9; // the number of the LED pin
const int ledPinC = 10; // the number of the LED pin
const int ledPinD = 11; // the number of the LED pin
const int ledPinM = 12; // the number of the LED pin
// variables will change:
int buttonStateA = 0; // variable for reading the pushbutton status
int buttonStateB = 0; // variable for reading the pushbutton status
int buttonStateC = 0; // variable for reading the pushbutton status
int buttonStateD = 0; // variable for reading the pushbutton status
int buttonStateM = 0; // variable for reading the pushbutton status
int outputVar = 0;
int lastButtonStateA = 0;
int lastButtonStateB = 0;
int lastButtonStateC = 0;
int lastButtonStateD = 0;
int lastButtonStateM = 0;
int lastLedPinA_Mem = 0;
int lastLedPinB_Mem = 0;
int lastLedPinC_Mem = 0;
int lastLedPinD_Mem = 0;
bool ledPinA_Mem = 0;
bool ledPinB_Mem = 0;
bool ledPinC_Mem = 0;
bool ledPinD_Mem = 0;
bool ledPinM_Mem = 0;
//serial ints
int incomingByte = 0;
int lastIncomingByte = 0;
int LoadButtonStateM = 0;
int ledPinMstate = 0;
int lastLedPinMstate = 0;
int tempMstate = 0;
int lastTempState = 0;
int buttonPushCounter = 0;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
// initialize the LED pin as an output:
pinMode(ledPinA, OUTPUT);
pinMode(ledPinB, OUTPUT);
pinMode(ledPinC, OUTPUT);
pinMode(ledPinD, OUTPUT);
pinMode(ledPinM, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPinA, INPUT);
pinMode(buttonPinB, INPUT);
pinMode(buttonPinC, INPUT);
pinMode(buttonPinD, INPUT);
pinMode(buttonPinM, INPUT);
ledPinA_Mem = 1; //Start with channel 1 activated
}
void loop() {
//lastIncomingByte = incomingByte;
lastButtonStateA = buttonStateA;
lastButtonStateB = buttonStateB;
lastButtonStateC = buttonStateC;
lastButtonStateD = buttonStateD;
//lastButtonStateM = buttonStateM;
// read the state of the pushbutton value:
buttonStateA = digitalRead(buttonPinA);
buttonStateB = digitalRead(buttonPinB);
buttonStateC = digitalRead(buttonPinC);
buttonStateD = digitalRead(buttonPinD);
buttonStateM = digitalRead(buttonPinM);
//test logic //ledPinA_Mem = buttonStateA == true && buttonStateB == false && buttonStateC == false && buttonStateD == false;
//test logic //ledPinB_Mem = buttonStateA == false && buttonStateB == true && buttonStateC == false && buttonStateD == false;
//test logic //ledPinC_Mem = buttonStateA == false && buttonStateB == false && buttonStateC == true && buttonStateD == false;
//test logic //ledPinD_Mem = buttonStateA == false && buttonStateB == false && buttonStateC == false && buttonStateD == true;
if (tempMstate == false && (buttonStateA == true || incomingByte == '1') && buttonStateB == false && buttonStateC == false && buttonStateD == false)
{
if (ledPinA_Mem == 0)
{
ledPinA_Mem = 1;
ledPinB_Mem = 0;
ledPinC_Mem = 0;
ledPinD_Mem = 0;
incomingByte = 0;
delay(3);
}
/*latching logic
else
{
ledPinA_Mem = 0;
}
*/
}
//BUTTON A/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (tempMstate == false && buttonStateA == false && (buttonStateB == true || incomingByte == '2') && buttonStateC == false && buttonStateD == false)
{
if (ledPinB_Mem == 0)
{
ledPinA_Mem = 0;
ledPinB_Mem = 1;
ledPinC_Mem = 0;
ledPinD_Mem = 0;
delay(3);
}
/*latching logic
else
{
ledPinB_Mem = 0;
}
*/
}
//BUTTON B/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (tempMstate == false && buttonStateA == false && buttonStateB == false && (buttonStateC == true || incomingByte == '3') && buttonStateD == false)
{
if (ledPinC_Mem == 0)
{
ledPinA_Mem = 0;
ledPinB_Mem = 0;
ledPinC_Mem = 1;
ledPinD_Mem = 0;
delay(3);
}
/*latching logic
else
{
ledPinC_Mem = 0;
}
*/
}
//BUTTON C/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (tempMstate == false && buttonStateA == false && buttonStateB == false && buttonStateC == false && (buttonStateD == true || incomingByte == '4'))
{
if (ledPinD_Mem == 0)
{
ledPinA_Mem = 0;
ledPinB_Mem = 0;
ledPinC_Mem = 0;
ledPinD_Mem = 1;
delay(3);
}
/*latching logic
else
{
ledPinD_Mem = 0;
}
*/
}
//BUTTON M (MUTE)/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*code that i use to copy paste
ledPinM_Mem = HIGH;
lastLedPinA_Mem = ledPinA_Mem;
lastLedPinB_Mem = ledPinB_Mem;
lastLedPinC_Mem = ledPinC_Mem;
lastLedPinD_Mem = ledPinD_Mem;
ledPinA_Mem = 0;
ledPinB_Mem = 0;
ledPinC_Mem = 0;
ledPinD_Mem = 0;
ledPinA_Mem = lastLedPinA_Mem;
ledPinB_Mem = lastLedPinB_Mem;
ledPinC_Mem = lastLedPinC_Mem;
ledPinD_Mem = lastLedPinD_Mem;
}
}
*/
incomingByte = Serial.read();
if (incomingByte != lastIncomingByte)
{
if(incomingByte == '5')
{
buttonPushCounter = 1;
delay(50);
}
else
{
//example has code here that i am not using
}
}
lastIncomingByte = incomingByte;
if(buttonPushCounter == 1)
{
digitalWrite(ledPinM, HIGH);
}
else
{
digitalWrite(ledPinM, LOW);//this does not turn off the LED as its supposed to in the StateChangeDetection example
}
digitalWrite(ledPinA, ledPinA_Mem);
digitalWrite(ledPinB, ledPinB_Mem);
digitalWrite(ledPinC, ledPinC_Mem);
digitalWrite(ledPinD, ledPinD_Mem);
//digitalWrite(ledPinM, ledPinM_Mem);
}