Code for relay activation via external trigger source


Hello - and thanks in advance for anyone that can assist! I am attempting to develop an ardunio system that receives a 12v external signal and uses that to open / close a linear actuator using a pair of relays. The actuator will open/close the cabinet where the speaker is housed that is supplied by the amplifier.

I am using an Arduno Uno, 2 5v relays and an RGB led and I have a voltage divider circuit that drops the 12v external voltage to 5v. I am pretty happy with the wiring as per the attached diagram, but the programming eludes me and I've spent considerable hours in this regard. Totally new to arduino programming. I'd like a sketch to do this. Amplifier 5v signal is to Pin9, RGB is to Pins 5,6,7 and cathode to Ardunio GND.
Relays are connected to pins 2 and 3

Sketch should do this:
RGB Led is Blue to show power is on to Ardunio (RGB of 0, 0, 255)
On receiving a 5v signal to Pin 9 which means Amplifier is on, RGB led changes to Orange (255, 70, 0) to show Amp on
Arduino then opens one relay and closes the other which activates the linear actuator opening he door, and turns RGB Green for 15 seconds, then returns to its normal 'power on' Orange state (once door closed after 15 seconds)
If 5v signal stops (ie Amplifier power is off) then the second relay opens, and first closes, both for 15 seconds to allow linear actuator to close door. RGB is Red during this.
RGB then returns to its normal 'power on' Orange state.

I have got the RBG working fine, the voltage regulator circuit works fine, I have been playing with some code but just can't get it to work. Perhaps someone could point me in the right direction?

many thanks in advance

// Prior to Setup
int green_light_pin = 5; ;// Green pin connected to digital pin 9
int blue_light_pin = 6; // Blue pin connected to digital pin 10
int red_light_pin = 7; // Red pin connected to digital pin 11
int RelayPinOne = 2;  // Pin of relay module one
int RelayPinTwo = 3;  // Pin of relay module two
int DenonPin = 9;  // Pin of 12V in from Denon Amp trigger down to 5v via resistor system
int AmpState = 0; // variable for recording the Amp status



// Setup
void setup() {
  pinMode(green_light_pin, OUTPUT); // sets the greenPin as an output
  pinMode(blue_light_pin, OUTPUT); // sets the bluePin as an output
  pinMode(red_light_pin, OUTPUT); // sets the redPin as an output
  pinMode(DenonPin, INPUT);  // sets the Amp pin as input
  pinMode(RelayPinOne, OUTPUT);  //Set pin connected to relay One as an output
  digitalWrite(RelayPinOne, LOW);  // Set pin to LOW to turn relay off
  pinMode(RelayPinTwo, OUTPUT);  //Set pin connected to relay Two as an output
  digitalWrite(RelayPinTwo, LOW);  // Set pin to LOW to turn relay off
  AmpState = digitalRead(DenonPin); // read the state

 RGB_color(0, 0, 255); // turn LED Blue to show Arduino power on
  delay(2000);
}


// Loop
void loop() {

  RGB_color(255, 70, 0); // turn LED Orange
  delay(2000);


  switch (AmpState) {


    case HIGH:
      RGB_color(0, 255, 0); // turn LED Green for actuator opening
      delay(2000);  // delay 2000 milliseconds or 2 seconds
      break;



    case LOW:
      RGB_color(255, 0, 0); // turn LED Red closing
      delay(4000);

      break;


  }

}

void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
{
  analogWrite(red_light_pin, red_light_value);
  analogWrite(green_light_pin, green_light_value);
  analogWrite(blue_light_pin, blue_light_value);

}

Hello
I guess a small FSM, using the SWITCH/CASE statement, a timer function and an I/O-handler for the RGB LED, relais and buttons should help.

Hi PP, thanks for the response...but I have no idea what you're saying? Clearly not as advanced as you wrt coding.

Hello
wrt your function describtion your sketch will have several internal stages. This stages will be handled by the FSM. Inside the stage the I/O and time related things has to be done until the next stage will be reached.

ok, thanks. Googled FSM 'finite state machine' and now reading on switch / case command. Was kinda hoping someone had a similar example I could leverage so as to not start from the very bottom

You asked for a way to solve the problem.

Not sure I am. Powering the Nano with 12V is close to the edge of what it's on-board regulator can tolerate before it overheats and shuts down or is damaged. Powering the coil of a 5V relay, plus an RGB led, could push it over the edge.

I would consider replacing the 2 relays with an h-bridge DC motor driver. There are many modules around, choose one suitable for the stall current of your actuator. For example drv8871. But avoid the ancient L298.

Post that code please. In the IDE, click "Tools>Auto format" then "Edit>copy to forum", then paste into your reply.

Hi PaulRB, great feedback - thank you. I haven't been able yet to get the Nano connected, so have used an Uno instead. Have already purchased the 5v relays, don't even know what an h-bridge DC motor driver is but will Google that now. Thanks again

Uno can probably cope a little better than Nano, driving the relays while powered by 12V. Uno's regulator is a little larger and more robust, and there is more space on the Uno's PCB to dissipate the heat. I'm not saying you should switch to using Uno, I much prefer Nano for any project.

Hi PaulRB - ok, have done. Attached sketch appears to work in that the LED shows the start colour and the LOW colour, but when I put +4.8V in to Pin 9/GND nothing happens. Also I realise a fundamental issue with the code as it cycles through AmpState LOW wheras ideally there would be a flag that is set to say 1 when the actuator is opened and only once it's at 1 can it then be closed - so it's not continually trying to close when it's already closed. My programming skills are lacking but I'm working on this now too.....

Thanks for posting the code correctly.

I suspect your problem is that you are not reading the input pin for the amp in loop(), only in setup(). So it only gets read once.

It is slightly odd to use a switch-case statement for something that can only ever have 2 states (the digital input from the amp). It should work, but an if-else statement is easier to read and more normal.

Great! Of course - duh! The reading of the amp needs to be in the loop to be continually read. Sketch now works - sorta. There are fundamental issues but at least it controls RGB fine, reads the amp state fine. I'll change to if/then and set up a flag on open/close which is I think the way to go. Thanks for the review of the code :slight_smile:

Revised Code using if statements. Kinda works. Seems to cycle through the if statements as if they were all satisfied when there is no +5v to pin 9 and dooropen=false so not sure why this is?

// Prior to Setup
int green_light_pin = 5; ;// Green pin connected to digital pin 9
int blue_light_pin = 6; // Blue pin connected to digital pin 10
int red_light_pin = 7; // Red pin connected to digital pin 11
int RelayPinOne = 2;  // Pin of relay module one
int RelayPinTwo = 3;  // Pin of relay module two
int DenonPin = 9;  // Pin of 12V in from Denon Amp trigger down to 5v via resistor system
int AmpState = 0; // variable for recording the Amp status

//declare door open flag variable
bool dooropen=false;   //declare door open variable



// Setup
void setup() {
  pinMode(green_light_pin, OUTPUT); // sets the greenPin as an output
  pinMode(blue_light_pin, OUTPUT); // sets the bluePin as an output
  pinMode(red_light_pin, OUTPUT); // sets the redPin as an output
  pinMode(DenonPin, INPUT);  // sets the Amp pin as input
  pinMode(RelayPinOne, OUTPUT);  //Set pin connected to relay One as an output
  digitalWrite(RelayPinOne, LOW);  // Set pin to LOW to turn relay off
  pinMode(RelayPinTwo, OUTPUT);  //Set pin connected to relay Two as an output
  digitalWrite(RelayPinTwo, LOW);  // Set pin to LOW to turn relay off

// turn LED Blue to show Arduino power on but amp not yet on
  RGB_color(0, 0, 255); 

// initialize serial communication:
  Serial.begin(9600);

} // close void setup()


// Loop
void loop() {

  AmpState = digitalRead(DenonPin); // read the state

// check whether if condition is true
  if (AmpState == HIGH && dooropen == false) {

Serial.println("AMPState = high and Dooropen = false");

    // open the door
      RGB_color(0, 255, 0); // turn LED Green for actuator opening
Serial.println("LED green for opening");
      
delay(8000);  // delay 4000 milliseconds 
dooropen=true;
RGB_color(255, 70, 0); // turn LED Orange
Serial.println("LED orange for Amplifier on");



  } // closing if statement

  

else if (AmpState == LOW && dooropen == true) {

Serial.println("AMPState = low and Dooropen = true");


    // close  the door

RGB_color(255, 0, 0); // turn LED RED
Serial.println("LED red for closing");

delay(8000);  // delay 4000 milliseconds 
dooropen=false;
RGB_color(0, 0, 255); // turn LED Blue
Serial.println("LED blue for Arduino only on");


  } // closing else if statement

else if (AmpState == LOW && dooropen == false) {

    // Arduino power on, amp off LED status
Serial.println("LED blue for Arduino only on via second else/if statement ");

RGB_color(0, 0, 255); // turn LED Blue to show only Arduino power on

  } // closing else if statement






}

// this following void command is like a DEFINE command in that it defines rgb color as a command consisting of 3 integers and then as specified in the curly brackets, analogwrites to each of these as specified
void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
{
  analogWrite(red_light_pin, red_light_value);
  analogWrite(green_light_pin, green_light_value);
  analogWrite(blue_light_pin, blue_light_value);

}

Hi,
What happenns if you apply 5v directly to pin 9?

You need to use the IDE monitor and place some Serial.print statements in your code to see what is happening to your variables.

Tom... :smiley: :+1: :coffee: :australia:

Hi TG, thanks for the response. On startup, LED is blue per the SETUP instruction. Good so far. When I apply +5v to pin 9, LED goes green, then yellow which is good. When I remove 5V LED goes red then blue. All good but then after sitting for ~10-20 seconds, LED inexplicably starts cycling through green/red twenty times then goes to blue.
I get what you're saying re serial monitor but that was partly what I was hoping the LED was doing - showing where in the code it is

Hi,
Do you have anything connected to pin9, is the potential divider connected?
If not then connect it up, it sounds like you have pin9 open circuit.

Tom... :smiley::+1: :coffee: :australia:

Not quite sure what you mean. Voltage divider works as I connected a led to 5v output when connected to Amp and works fine. For now with the Arduino testing, I've got a 4.8v power supply I'm connecting directly to pin 9 and Arduino GND. Have got serial monitor working, now time to investigate....... thank you

Update - ok officially weird. Serial monitor shows LED blue per second else statement which is correct. I f I so much as touch the 5v negative wire even though power supply not remotely connected to ardunio and +/- wires are loose, ardunio registers 5v at pin 9.....
I'm going to take a gamble any connect the 5v power supply to the ardunio via a switch to see if these spurious voltages are eliminated that way. OK did this, much better. Still seems to have some spurious idea that it's received 5v when it hasn't but infinitely better

Hi PaulRB, ok have looked up h-bridge DC motor drivers with drv8871. They're very small things but seem to function the same as a relay in that a small current can be used to switch a large current although they don't have the relay coil of the regular relays.

Kinda difficult to read. Please remember my previous advice from post #8.

Hi PRB, the code was pasted from the IDE? It now works so I updated the code, however it seems pretty touchy - all I have to do is touch the switch on the +5v side and the arduino registers this as receiving a HIGH signal on pin 9. For some reason after the switch is on and the actuator is opened, if the switch is turned off it seems to take the Arduino over 3 seconds to register this. Not sure why this is (i.e. 3 seconds to perform the first else statement)