I am very new to Arduino and its been a very long time since I've done any programming and looking for some guidance. Here is what I have.
I have two buttons, we will call them Red and Green, the purpose of the buttons is to tell a linear actuator to either push or pull a railroad switch into the correct position. The buttons will also sequence railroad signal lights to indicate which direction is safe or not safe to travel. I believe I have the basic functionality in place but looking to build in some additional intelligence.
Here is a video to what I've already created- https://photos.app.goo.gl/ThjBtvvJntKxBweh7
My Request:
I would like to have it so when I push the Red button for example that if a particular LED is on ignore the push. However if a particular LED is off process the commands that will set the linear actuator and turn the correct LED's on. The same would be true if the Green button is pushed.
In the video attached you will see that when I push the top or Red button the lights on the right go on/flash and then eventually it leaves a Green LED on. It is the Green LED that I want to use as my indicator saying I can push the button that activated that LED again until the LED is off.
I hope this makes a little sense and look forward to learning more about how to use and program Arduino
Robby_Signal_Steady_Yellow.ino (4.24 KB)
I can't see any LED changes in your video- try dimming the room lights.
I think what you are asking is akin to a state machine. Simply have a global variable for the current states, and test those states before proceeding with an action.
Some comments on your code.
-
Your button pins are defined with const so the compiler knows that the values can't be changed in the code. Why did you not do the same for the other pins?
2)
The use of delay() is probably going to give you grey hairs in the future.
3)
Try to stay away from pins 0 and 1; they are, on most Arduino boards, used for serial communication. You still have digital pins available and you can use analogue pins as well for digital IO.
To solve your question
1)
On an AVR based board, you can read back the status of the pin using digitalRead() . Simple example
const int rightsignaredlRelay4 = 4; //Right Signal Red
void setup()
{
Serial.begin(57600);
pinMode(rightsignaredlRelay4, OUTPUT);
digitalWrite(rightsignaredlRelay4, LOW);
Serial.print("rightsignaredlRelay4 set to ");
Serial.println(digitalRead(rightsignaredlRelay4));
digitalWrite(rightsignaredlRelay4, HIGH);
Serial.print("rightsignaredlRelay4 set to ");
Serial.println(digitalRead(rightsignaredlRelay4));
}
-
As a better alternative (read, will work on any board), you can keep track of the status of the relays.
Simple example:
const int rightsignaredlRelay4 = 4; //Right Signal Red
int redlRelay4Status = LOW;
void setup()
{
Serial.begin(57600);
if(redlRelay4Status == LOW)
{
redlRelay4Status = HIGH;
digitalWrite(rightsignaredlRelay4, redlRelay4Status);
Serial.print("rightsignaredlRelay4 set to ");
Serial.println(redlRelay4Status);
}