1

I have a very basic programming knowledge, but the thing I want to achieve is to alternate between switching 2 relays with a button or a RPi input pulse.

So something like this:

  1. Press the button
  2. Switch relay 1 on and off (1 second delay)
  3. Press the button
  4. Switch relay 2 on and off
  5. Press the button
  6. Switch relay 1 on and off (1 second delay)
  7. Press the button
  8. Switch relay 2 on and off
  9. Press the button
  10. Switch relay 1 on and off (1 second delay)
  11. Press the button
  12. Switch relay 2 on and off
  13. etc.

Here's my code, I thought if I'd just put the second relay after the first else everything would work. But I'm missing some basic knowledge to figure this out.

const int ledPin = 2; const int ledPinA = 3; void setup() { pinMode(ledPin, OUTPUT); pinMode(ledPinA, OUTPUT); Serial.begin(9600); } void loop() { int sensorValue = analogRead(A0); Serial.println(sensorValue); delay(1); if (sensorValue > 500) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } if (sensorValue > 500) { digitalWrite(ledPinA, HIGH); } else { digitalWrite(ledPinA, LOW); } } 

I'm thinking I should use while.

2
  • Do you want it to swtch the relay on ONCE or repeatedly (so make them blink)? Commented Sep 14, 2017 at 15:11
  • Can you also switch between relays with a long press? Commented Aug 19, 2019 at 23:04

2 Answers 2

0

So something like this:

easy:

  1. increment a counter based on button presses;

  2. run a state machine on that counter to decide on relay actions.

the state machine can be a simple switch / case statement.

1
  • 1
    Thank you dannyf. It was simple indeed. Made a counter, ==1 switches relay 1 and ==2 switches the other and resets the counter. Commented Sep 15, 2017 at 18:42
1

The way you have it, both if blocks will be evaluated each time you call to loop(). Instead, you need to create a variable that remains "alive" in between each call to loop() to keep track of which relay you want to activate:

const int ledPin = 2; const int ledPinA = 3; int active_relay = 2; // State variable for current relay pin void setup() { pinMode(ledPin, OUTPUT); pinMode(ledPinA, OUTPUT); Serial.begin(9600); } void loop() { int sensorValue = analogRead(A0); Serial.println(sensorValue); delay(1); // This delays for 1ms, not 1s ?? if (sensorValue > 500) { // Pulse received digitalWrite(active_relay, HIGH); // Relay on delay(1000); // Wait 1 second digitalWrite(active_relay, LOW); // Relay off // Update variable for next call to loop if (active_relay == ledPin) { active_relay = ledPinA; // Swap } else { active_relay = ledPin; // Swap } } // Remove 2nd if block } 
1
  • I'd use the digital reading of the pin with some debounce; if the read value must be analog (but only the OP knows this) maybe a bit of filtering is needed Commented Sep 14, 2017 at 15:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.