Hello community,
I'm doing a project where I have a 2 channel relay board. I'd like to control the relays based on a digital input. The input is a 5V signal, which is HIGH or LOW depending voltage. What I want to do is, when 5V (HIGH) is received, switch on relay1 for 1 second and turn off. When the voltage drops thus LOW, switch on relay2 for 1 second and turn of.
I've tried many sketches, blink, led, boolean, timers etc and tried to modify them, but I'm not a programmer and my head hurts. The code I'm now using is below. This enables me to turn on relay1 or 2 depending on serial input. Off course I don't want to use serial input but I'm lost.
Can someone give me a guideline?
/*
simple LED test
*/
char val; // variable to receive data from the serial port
int ledpin1 = 2; // LED connected to pin 2 (on-board LED)
int ledpin2 = 3; // LED connected to pin 2 (on-board LED)
int trigger = 5; // trigger input 5V
void setup()
{
digitalWrite(ledpin1, HIGH);
digitalWrite(ledpin2, HIGH);
pinMode(ledpin1, OUTPUT); // pin 2 (on-board LED) as OUTPUT
pinMode(ledpin2, OUTPUT); // pin 3 (on-board LED) as OUTPUT
pinMode(trigger, INPUT); // pin 5 input 5V projector
Serial.begin(9600); // start serial communication at 115200bps
}
void loop()
{
if( Serial.available() ) // if data is available to read
{
;
}
val = Serial.read(); // read it and store it in 'val'
if( val == 'C' ) // if 'C' was received led 2 on for 1 second
{
digitalWrite(ledpin1 = 2, LOW); // turn ON pin 2
delay(1000); // wait 1 second
digitalWrite(ledpin1, HIGH); // turn Off pin 2
}
if( val == 'D' ) // if 'D' was received led 3 on for 1 second
{
digitalWrite(ledpin2 = 3, LOW); // turn ON pin 3
delay(1000); // wait 1 second
digitalWrite(ledpin2, HIGH); // turn Off pin 3
}
}