I'm hoping that someone can help me suss this out. I'm getting an error message that, as far as I can tell, shouldn't be there. When the compiler gets to the line in red, it gives me the message " In function 'void setup()': error: expected `)' before ';' token In function 'void loop()':"
Ok, so your sharp eyes helped me with my first problem. I wonder if they could help me with my new one. Basically, what I want to happen is for piezoAPin to turn both leds on. Then piezoBPin turns ledB off on the first hit, and ledA off on the second. I can't seem to figure it out.
if (valA >= THRESHOLD)
{
digitalWrite(ledAPin, HIGH);
digitalWrite(ledBPin,HIGH);
delay(100);
}
if (valB >= THRESHOLD)
{
digitalWrite(ledBPin, LOW);
delay(100);
}
//* figure out how to get it to turn off ledAPin if and only if ledBPin is LOW *//
while (ledBPin == LOW)
{
if (valB >= THRESHOLD)
{
digitalWrite(ledBPin, LOW);
}
}
}
The bit after the comment is where I'm trying to tell it to turn ledA off.
I haven't been able to work out how to do 'if and' statements yet. I want to tell it, 'if ledB is off AND ledA is on, then on piezoB hit, turn ledA off.
If I could make a further request, I'd like to figure out how it works by myself, I'd at least like to write the code. I think my problem is just the way I'm thinking about how to tell the thing what I want it to do.
Well, this is a little ugly (for me,) but it's about what you want to do. I just hacked it together in a couple of minutes - it compiles, but I didn't test it on an Arduino (I don't have any piezos laying around)
#define ledAPin 13
#define ledBPin 12
#define piezoAPin 0
#define piezoBPin 1
#define threshold 100
void setup()
{
pinMode(ledAPin, OUTPUT);
pinMode(ledBPin, OUTPUT);
}
void loop() {
if (analogRead(piezoAPin) >= threshold & digitalRead(ledAPin) == LOW & digitalRead(ledBPin) == LOW) {
digitalWrite(ledAPin, HIGH);
digitalWrite(ledBPin,HIGH);
delay(100);
}
if (analogRead(piezoBPin) >= threshold) {
if (digitalRead(ledBPin) == HIGH & digitalRead(ledAPin) == HIGH) { // If they're both on, just turn off one
digitalWrite(ledBPin, LOW); // Just turn off B
}
else if (digitalRead(ledAPin) == HIGH) { // else if A is on, turn it off.
digitalWrite(ledAPin, LOW); // Turn off A
}
delay(100);
}
}
Basically, the first if statement only turns your LEDs on if piezo A if over the threshold and if the LEDs are already off (this differs from your code a bit - yours always turns the LEDs on whether they're already on or not.)
The second if statement reads the B piezo, and if both LEDs are on, it turns off LED B. Else, if only LED A is lit, then it will turn it off.