Hi again,
Thanks for everybody's help last week it's much appreciated!
I've come up against a further hurdle that I hope you may be able to help with.
Let me just say that I only get Sunday's to work on this, so trying to code whilst learning to code, research and fault find all in one day is not proving easy!
Sorry if this post is quite long but I'm trying to give you as much info as I can.
The code posted last week worked perfectly, but the additions I thought I had done correctly won't compile.
As per last week, I have a trigger which flashes an LED three times in quick succession when the trigger is pulled, regardless whether its pulled and held or just quickly blipped.
What I have tried to add is a mode switch; when the switch is in one position the aforementioned happens. When it is in its second position the LED only flashes once, again regardless of whether the trigger is blipped or held. At some point the LED will be replaced with a solenoid, but to make sure the code is working properly I'm just using a light for the moment.
This is my new code -
const int triggerPin = 2; // trigger on pin 2
const int modePin = 3; // toggle switch w/PULLUP - single flash when switch off (pin 3 @ HIGH), three flashes when switch on (pin 3 @ LOW),
const int ledPin = 13; // led on pin 13
const int a = 100; //delay time
bool triggerState = false; // variable for the state of the trigger
bool modeState = false; // variable for the state of the mode selector
void setup() {
pinMode(triggerPin, INPUT_PULLUP); // sets pin 2 to input with pullup resistor engaged
pinMode(modePin, INPUT_PULLUP); //sets pin 3 to input with pullup resistor engaged
pinMode(ledPin, OUTPUT); //sets pin 13 to output
}
void loop() {
if ((!triggerState && digitalRead(triggerPin) == LOW) && (!modeState && digitalRead(modePin == LOW)); { // if trigger is pulled and mode switch is on
triggerState = true; // trigger is on
modeState = true; // mode switch is on
//led light x 3
digitalWrite(ledPin, HIGH);
delay(a);
digitalWrite(ledPin, LOW);
delay(a);
digitalWrite(ledPin, HIGH);
delay(a);
digitalWrite(ledPin, LOW);
delay(a);
digitalWrite(ledPin, HIGH);
delay(a);
} else {
digitalWrite(ledPin, LOW);
}
/* modeState = true; // mode switch is on - this wasnt in the last code but I've added because a second bool
has been used.
Do I need to make it this -
triggerState = false; // trigger is off
modeState = true; // mode switch is on
digitalWrite(ledPin, LOW); // LED off
delay(30); // Simple debouncing
because the modeState bool is still true so it has to do something as a result?
*/
if (triggerState && digitalRead(triggerPin)); { // if trigger is released and mode switch is on
triggerState = false; // trigger is off
modeState = true; // mode switch is on
delay(30); // Simple debouncing
}
if ((!triggerState && digitalRead(triggerPin) == LOW) && (modeState && digitalRead(modePin == HIGH)); { // if trigger is pulled and mode switch is off
triggerState = true; // trigger is on
modeState = false; // mode switch is off
//led light x 1
digitalWrite(ledPin, HIGH);
delay(a);
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, LOW);
}
if ((triggerState && digitalRead(triggerPin) && (modeState && digitalRead(modePin == LOW)); { // if trigger is released and mode switch is of
triggerState = false; // trigger is off
modeState = false; // mode switch is off
delay(30); // Simple debouncing
}
}
Not sure if I've got the use of the second bool in the 'if' statements correct, but I don't know cos I cant compile it.
I get the following error message when I try -
Arduino: 1.8.19 (Mac OS X), Board: "Arduino Uno"
/Users/taramcnamee/Documents/Arduino/New_trigger_test_2/New_trigger_test_2.ino: In function 'void loop()':
New_trigger_test_2:23:105: error: expected primary-expression before '{' token
if ((!triggerState && digitalRead(triggerPin) == LOW) && (!modeState && digitalRead(modePin == LOW)); { // if trigger is pulled and mode switch is on
^
New_trigger_test_2:23:105: error: expected ')' before '{' token
New_trigger_test_2:57:105: error: expected primary-expression before '{' token
if ((!triggerState && digitalRead(triggerPin) == LOW) && (modeState && digitalRead(modePin == HIGH)); { // if trigger is pulled and mode switch is off
^
New_trigger_test_2:57:105: error: expected ')' before '{' token
exit status 1
expected primary-expression before '{' token
It seems to suggest I'm missing either ')' or '}', but for the life of me I can't see where.
I'm sure other errors will pop up when this one is solved
but I need to get past this one first.
Thank you guys, I apologise for disturbing your Sunday.