#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,
- 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);
- 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!
