Print a message when both inputs goes high

Hello I am trying to start a code to light up a bi-color LED (Red, Green and Orange - Orange lights up when both pins go HIGH). But I cannot get the message when the ORANGE LED lights up! The code below was develop to print the message once (when the status change in the input pins), according to this thread -> https://forum.arduino.cc/index.php?topic=492865.0 (state change detection)

const int PortPin = 2;
const int PortPin1 = 3;

int PortState = 0;
int lastPortState = 0;
int PortState1 = 0;
int lastPortState1 = 0;

void setup()
{
pinMode(PortPin, INPUT);
pinMode(PortPin1, INPUT);
Serial.begin(9600);
}

void loop()
{
PortState = digitalRead(PortPin);
PortState1 = digitalRead(PortPin1);

if (PortState != lastPortState)
{

if (PortState == HIGH)
{
Serial.print("GREEN LED IS ON");
delay(50);
}

if (PortState == LOW)
{
Serial.print("GREEN LED IS OFF");
delay(50);
}

lastPortState = PortState;
}


if (PortState1 != lastPortState1)
{
  if (PortState1 == HIGH)
  {
    Serial.print("RED LED IS ON");
  }

  if (PortState1 = LOW)
  {
    Serial.print("RED LED IS OFF");
  }
lastPortState1 = PortState1;
}

if (PortState1 != lastPortState)
{
  if (PortState1 == HIGH && PortState == HIGH)
  {
    Serial.print("LED ORANGE IS ON");
  }

  if (PortState1 == LOW && PortState == LOW)
  {
    Serial.print("LED ORANGE IS OFF");
  }
}
}

Thank you.

hideki: ``` if (PortState1 != lastPortState)   {     if (PortState1 == HIGH && PortState == HIGH)     {       Serial.print("LED ORANGE IS ON");     }

    if (PortState1 == LOW && PortState == LOW)     {       Serial.print("LED ORANGE IS OFF");     }   }

What is the meaning of comparing the current state of one input with the previous state of another input?

Pieter

if (PortState1 != lastPortState)

I found this mismatch in the last set of if statements. Shouldn't that be lastPortState1? And should that if statement consider both ports?

Should this …

if (PortState1 != lastPortState)
{
  if (PortState1 == HIGH && PortState == HIGH)
  {
    Serial.print("LED ORANGE IS ON");
  }

be this?

if (PortState1 != lastPortState1)//<--missing the 1 ?
{
  if (PortState1 == HIGH && PortState == HIGH)
  {
    Serial.print("LED ORANGE IS ON");
  }

Yeah. What 2trillion said. :slight_smile:

const uint8_t greenPin = 2;
const uint8_t redPin = 3;

char* lastColor = NULL;

void setup() {
  pinMode(greenPin, INPUT);
  pinMode(redPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  bool greenState = digitalRead(greenPin);
  bool redState = digitalRead(redPin);

  char* color = statesToColor(greenState, redState);
  
  if (color != lastColor) {  // if the color has changed since last time
    if (lastColor) {  // if an LED was previously on
      Serial.print("LED ");  // print that it went out
      Serial.print(lastColor);
      Serial.println(" IS OFF");
    }
    if (color) {  // if an LED is now on
      Serial.print("LED ");  // print that it went on
      Serial.print(color);
      Serial.println(" IS ON");
    }
    lastColor = color;
  }
}

char* statesToColor(bool greenState, bool redState) {
  if (greenState && !redState) return "GREEN";
  else if (redState && !greenState) return "RED";
  else if (greenState && redState) return "ORANGE";
  else return NULL;
}

IMHO, it makes more sense to check if the color has changed, rather than checking whether the separate inputs have changed.

Pieter

2trillion: if (PortState1 != lastPortState)

I found this mismatch in the last set of if statements. Shouldn't that be lastPortState1? And should that if statement consider both ports?

It was my mistake, sorry. I tried consider both ports inside the same statement 'if', but it did not work. I will try your code, instead. Thanks a lot!