How to detect digital input pins blinking without sensors

#include <Arduino.h>
#include <Arduino_MachineControl.h>
#include <rgb_lcd.h>

using namespace machinecontrol;

rgb_lcd lcd;

byte lock[8] = {
  0b01110,
  0b10001,
  0b10001,
  0b10001,
  0b11111,
  0b11011,
  0b11111,
  0b11111,
};
 
byte unlock[8] = {
  0b01100,
  0b10010,
  0b10010,
  0b10010,
  0b00011,
  0b00011,
  0b00011,
  0b00011,
};

const int RED_SIGNAL_PIN = DIN_READ_CH_PIN_01;
const int AMBER_SIGNAL_PIN = DIN_READ_CH_PIN_02;
const int GREEN_SIGNAL_PIN = DIN_READ_CH_PIN_03;
const int BLUE_SIGNAL_PIN = DIN_READ_CH_PIN_04;
const int WHITE_SIGNAL_PIN = DIN_READ_CH_PIN_05;

const int DOOR_LOCK_PIN = 0;

const int debounceDelay = 1000;
unsigned long redlastDebounceTime = 0;    
unsigned long amberlastDebounceTime = 0;
unsigned long greenlastDebounceTime = 0;

int redPrevState = LOW;
int amberPrevState = LOW;
int greenPrevState = LOW;

bool checkBlink(int currentState, int &prevState, unsigned long &lastDebounceTime) {
  bool isBlinking = false;
  if (currentState != prevState) {
    if (currentState == HIGH) {
      unsigned long currentTime = millis();
      if ((currentTime - lastDebounceTime) < debounceDelay) {
        isBlinking = true;
      }
      lastDebounceTime = currentTime;
    }
  }
  prevState = currentState;
  return isBlinking;
}

void setup() {
  lcd.begin(16,2);
  Wire.begin();
  digital_outputs.setAll(0);
 
  // Initialize the digital inputs
  if (!digital_inputs.init()) {
    lcd.print("Failed to initialize");}

  lcd.createChar(0, lock);
  lcd.createChar(1, unlock);

}
 
void loop(){
  lcd.setCursor(0,1);
  lcd.print("RAGBW");

  lcd.setCursor(5,1);
  lcd.write(byte(5));

  lcd.setCursor(0,0);
  r = digital_inputs.read(DIN_READ_CH_PIN_01);
   lcd.print(r);                                                                        
  a = digital_inputs.read(DIN_READ_CH_PIN_02);
   lcd.print(a);                                                                        
  g = digital_inputs.read(DIN_READ_CH_PIN_03);
   lcd.print(g);                                                                        
  b = digital_inputs.read(DIN_READ_CH_PIN_04);
   lcd.print(b);                                                                        
  w = digital_inputs.read(DIN_READ_CH_PIN_05);
   lcd.print(w);

if(door==HIGH){
  if (r==LOW && a==LOW && g==LOW && b==LOW && w==HIGH){
    if(temp_ch0 >= 10 && temp_ch0 <= 50){
      digital_outputs.set(DOOR_LOCK_PIN, LOW);
      lcd.setCursor(8,0);
      lcd.write(byte(1));
      //buzzer();
    }else{
      digital_outputs.set(DOOR_LOCK_PIN, HIGH);
      lcd.setCursor(15,0);
      lcd.write(byte(4));
      lcd.setCursor(8,0);
      lcd.write(byte(0));
    }
  }
}

int currentredState = digital_inputs.read(DIN_READ_CH_PIN_01);
bool redBlink = checkBlink(currentredState, redPrevState, redlastDebounceTime);

if(door==HIGH){
  if(redBlink && a==LOW && g==LOW && b==LOW && w==HIGH){
   if(temp_ch0 >= 10 && temp_ch0 <= 50){
      digital_outputs.set(DOOR_LOCK_PIN, LOW);
      lcd.setCursor(8,0);
      lcd.write(byte(1));
      //buzzer();
    }else{
      digital_outputs.set(DOOR_LOCK_PIN, HIGH);
      lcd.setCursor(15,0);
      lcd.write(byte(4));
      lcd.setCursor(8,0);
      lcd.write(byte(0));
    }
  }
}else{
  digital_outputs.set(DOOR_LOCK_PIN, LOW);
  lcd.setCursor(8,0);
  lcd.write(byte(1));
}
}

Hi, I am trying to program my Arduino Portenta Machine Control to detect whether a external light signal from a light tower is blinking, on, or off.

For on and off I am able to read, however I am facing some difficulties detecting/reading if an led is blinking.

I am using the code above but it doesn't work. when red light signal from light tower goes high during the blinking stage, the second if condition
if(door==HIGH){ if(redBlink && a==LOW && g==LOW && b==LOW && w==HIGH){ if(temp_ch0 >= 10 && temp_ch0 <= 50){ digital_outputs.set(DOOR_LOCK_PIN, LOW); is true but when red light signal from light tower goes back to low during the blinking stage the first condition if(door==HIGH){ if (r==LOW && a==LOW && g==LOW && b==LOW && w==HIGH){ if(temp_ch0 >= 10 && temp_ch0 <= 50){ digital_outputs.set(DOOR_LOCK_PIN, LOW);
is true. so the result of this code is that it will toggle between the 2 if condition which is not what I want.

what I am planning to do is that during the one whole cycle of the blinking stage only the second if condition should be true, even if it goes to low during the blinking stage only second condition should stay true.

Even though both condition will result in the door being unlock, if I would to change one of the condition to lock the door, it will cause an issue since the output will be going back and forth between lock and unlock. For example,

  1. first if condition
  if (r==LOW && a==LOW && g==LOW && b==LOW && w==HIGH){
    if(temp_ch0 >= 10 && temp_ch0 <= 50){
      digital_outputs.set(DOOR_LOCK_PIN, HIGH);
  1. second if condition
  if(redBlink && a==LOW && g==LOW && b==LOW && w==HIGH){
   if(temp_ch0 >= 10 && temp_ch0 <= 50){
      digital_outputs.set(DOOR_LOCK_PIN, LOW);

Hence, I need some help to try and solve this issue. I am trying to detect without using any additional electrical component such as sensors. If there is any clarification please let me know, Thanks in advance!

Hi @oosoom

programming means to be very very very precise.

precise in what character-sequences build your code.
If you forget a single semicolon ";" the compiler will complain
If you write one single letter CAPITALIZED instead of lowercase the compiler will complain

And beeing very very very precise in describing the wanted functionality.

I will not even start trying to speculate which if-condition is for you "the first" one or "the second" one.

Your title is talking about an "external" light. Your medium confusing description makes me think your code does switch on/off this light. So I would call this an internal light because the switching on/off is done inside your code.

You should re-edit your description so that each and evey single detail is named and described in a way that there is no more room for interpretetion.

Another example:

WHICH output do you mean?
Your output pin has a name inside your code.

You will have to clarify all these details first to make it easy for other users to understand what you mean.

1 Like

You can not.

1 Like

Your title is talking about an "external" light. Your medium confusing description makes me think your code does switch on/off this light. So I would call this an internal light because the switching on/off is done inside your code.

my code does not switch on/off the lights. light signals are coming from an external source (light tower) which is controlled by a PLC. Those light signals will be connected to the Arduino Portenta Machine Control Digital Input pins and will be read.

Another example:

WHICH output do you mean?
Your output pin has a name inside your code.

My output will be either the door locking or unlocking.

A trick I have used is 1000 micron plastic fiber optic cable. Put one as close to the light source as possible, the other end will give you a colored dot same as the light source. I use the jacketed type. This plastic cable is used for communications and there are pin diode and transistor receivers for it. It is not that expensive.

This does not require external electronic components unless you use a receiver. You can observe the cable to see it.

so its not possible to just detect light blinking by codes? you would need additional components like LDR + codes to detect?

but i need it to work with the code, is it possible?

With a light sensor and some code, yes.

This is my external light source controlled by PLC
image
what type of sensors should i use?

You can use an LDR, photodiode or phototransistor plus a resistor. An internet search for "arduino light sensor" will turn up many tutorials.

What's the difference between on/off and blinking??
I'm thinking you got the board confused as your title is a bit misleading..
The way I understand, these lights are connected to your digital inputs and you can read the states on and off properly..
The only difference between on and off and blinking is the duration on and off..
Is it only the red light that blinks??
What is duration of blink, on time, off time??

Should be achievable in code..

good luck.. ~q

on/off meaning the light signal will stay on/off until it changes state
blinking meaning the light signal will change state on and off at regular intervals

sorry how is it misleading? maybe i could edit my title to be more precise.

nope, red, amber, and green can blink, on and off
while blue and white can only on or off

about 0.5s to on and 0.5 to off at regular timings

without the use of sensors?

thanks in advance!

How do you know if the light is on or off without the use of a sensor?

is my understanding correct??
are the lights in fact wired to your inputs??

~q

these are you lights??

~q

  r = digital_inputs.read(DIN_READ_CH_PIN_01);
   lcd.print(r);                                                                        
  a = digital_inputs.read(DIN_READ_CH_PIN_02);
   lcd.print(a);                                                                        
  g = digital_inputs.read(DIN_READ_CH_PIN_03);
   lcd.print(g);                                                                        
  b = digital_inputs.read(DIN_READ_CH_PIN_04);
   lcd.print(b);                                                                        
  w = digital_inputs.read(DIN_READ_CH_PIN_05);
   lcd.print(w);

using these codes it can read if light signals are on or off

yes the light signals controlled by a PLC is wired to my Arduino board's digital input

1 Like

yes r, a, g, b and w are my light signals

People don't think so..
Per you title, there's nothing.. :slight_smile:

~q

oh i see yes now that you mention this, i can see why its misleading.
'How can i detect whether an external light controlled by a PLC that is wired to my arduino board's digital input is blinking' would this be better?