Controlling relay using digital input

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
  }


}
  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

The comments are stupid. Those pins are NOT the ones with the onboard LED. Pay some attention to what you are cutting and pasting.

Why are you setting the state of the input pins to HIGH? Do you KNOW what that does? WHY do you need to do that?

  if( Serial.available() )       // if data is available to read
  {
    ;
  }
  val = Serial.read();         // read it and store it in 'val'

If there IS serial data to read, do nothing. Then, read, whether there is anything to read, or not. WTF?

Why does your code that expects a HIGH or LOW on some undefined pin read serial data at all?

Your "digital input" is equivalent to a switch/button, it's either high 5V or low 0V.

The only trick you need to know is that you want to do things based on when that pin GOES high or GOES low not when it IS high or low (that's an important distinction).

By a bit of good luck that is covered in the IDE example sketch File/Examples/02. Digital/StateChangeDetection. That switches an LED on/off. Switching relays on or off is no more difficult...and then you're nearly there.

Steve