My Code Makes the Relay Turn My Light Into a Strobe

Whenever I use this code I think It cant run fast enough to just keep the stream of electricity constant. It takes digital input from my raspberry pi and turns it into an inverted digital output that goes to a relay to turn on my computer monitors and light.

/*
*
* Made By Evan Overman
* For Arduino Nano Every
* Febuary 17, 2020
*
*/

int digitalPin11;
int digitalPin12;

void setup() 
{
  //Define the Different Relay Output Pins
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  //Define Different Input Pins from the Raspberry Pi
  pinMode(11, INPUT);
  pinMode(12, INPUT);
}

void loop()
{
  //Digitaly Read the Input Pins from the Raspberry Pi
  digitalPin11 = digitalRead(11);
  digitalPin12 = digitalRead(12);
  //Check if the Raspberry Pi has set its Pin 28 to High or Low and act Accordingly
  if (digitalPin11 == HIGH)
  {
    digitalWrite(4, LOW);
  }
  else if (digitalPin11 == LOW)
  {
    digitalWrite(4, HIGH);
  }
  //Check if the Raspberry Pi has set its Pin 29 to High or Low and act Accordingly
  if (digitalPin12 == HIGH)
  {
    digitalWrite(2, LOW);
    delay(1000);
  }
  else if (digitalPin12 == LOW)
  {
    digitalWrite(2, HIGH);
    delay(1000);
  }
}

Here is one of the problems:

    delay(1000);

Oh sorry, I put that there to verify wether or not the problem was software or some kind of under voltage or something, the original code didn’t have it.

/*
*
* Made By Evan Overman
* For Arduino Nano Every
* Febuary 17, 2020
*
*/

int digitalPin11;
int digitalPin12;

void setup() 
{
  //Define the Different Relay Output Pins
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  //Define Different Input Pins from the Raspberry Pi
  pinMode(11, INPUT);
  pinMode(12, INPUT);
}

void loop()
{
  //Digitaly Read the Input Pins from the Raspberry Pi
  digitalPin11 = digitalRead(11);
  digitalPin12 = digitalRead(12);
  //Check if the Raspberry Pi has set its Pin 28 to High or Low and act Accordingly
  if (digitalPin11 == HIGH)
  {
    digitalWrite(4, LOW);
  }
  else if (digitalPin11 == LOW)
  {
    digitalWrite(4, HIGH);
  }
  //Check if the Raspberry Pi has set its Pin 29 to High or Low and act Accordingly
  if (digitalPin12 == HIGH)
  {
    digitalWrite(2, LOW);
  }
  else if (digitalPin12 == LOW)
  {
    digitalWrite(2, HIGH);
  }
}

Don't do: if (digitalPin11 == HIGH)

Check the state change instead: if(lastDigitalPin11 == LOW && currentDigitalPin11 == HIGH)
see input toggle relay

Hi,
I would suggest you give your pin numbers, proper variable names so that your code is more readable.
TIP;

What does Pin2 do?
Make it Relay1OutputPin, or what the relay actually does.

Same with the input pins, what does a signal on Pin 11 mean.
Make it PiSomefunctionPin.

digitalRead(11) means very little when reading your code.
digitalRead(PiSomefunctionPin), does mean something.

Also digitalPin11, make it a description of what it represents.

Remove the Pi and use a jumper lead to switch the inputs from 5V to 0v.
As you do not have pullups then 5V == HIGH and 0V == LOW.
An open input is floating and has no Logic Level.

Are you Pi outputs open collector or give true 5V and 0V?
You do have the gnd of the Arduino and gnd of the Pi connected together?

Tom.... :slight_smile:
PS. Put some Serial.print statements in your code to check your inputs.

(deleted)

Jeez, thats really elegant, thanks.