DuaneDegn:
There are definitely things I'd do differently if I were writing this for myself but I think the changes I'd make would make the code harder to follow.
Hopefully this will be enough to give you the idea of how to check states and to check inputs even when the inputs won't be acted on immediately.
This is incredibly helpful! I've gone through and made a few changes to the priorities like you said and added a another state to it.
Now I'm trying to test the same code but with tact switches/push-buttons. I know the basic code for using a push button
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
However I'm having trouble implementing a push button into the code for the project itself. I wish to change the input to change the state from entering a byte in the serial monitor to pushing a button on the board. I tried the following but it's having no effect at all when I press the push button.
const byte NSG = 13; // N-S green light
const byte EWG = 12; // E-W green light
const byte NSR = 11; // N-S red light
const byte EWR = 10; // E-W red light
const byte NST = 9; // N-S turn light
const byte EWT = 8; // E-W turn light
const byte TRAIN = 7; // Train incoming light
const byte PUSHBUTTON = 2; // Push button for testing input
int buttonState = 0; // Variable for reading the pusbutton status
const unsigned long TWENTY_FOUR_SECONDS = 24000;
const unsigned long TEST_TIME = 10000;
//const unsigned long DEFAULT_TIME = TWENTY_FOUR_SECONDS; // Use as default when testing is done
const unsigned long DEFAULT_TIME = TEST_TIME; // Use while testing
const unsigned long TWO_NS_TURN_TIME = DEFAULT_TIME;
const unsigned long FOUR_NS_STRAIGHT_TIME = DEFAULT_TIME;
const unsigned long TWO_EW_TURN_TIME = DEFAULT_TIME;
const unsigned long FOUR_EW_STRAIGHT_TIME = DEFAULT_TIME;
const unsigned long TRAIN_INCOMING_TIME = DEFAULT_TIME;
const unsigned long NO_CARS_DEFAULT_TIME = DEFAULT_TIME / 2; // use a shorter time when there aren't cars waiting?
unsigned long activeInterval;
unsigned long systemTimer;
boolean twoNsTurnFlag = 0;
boolean fourNsStraightFlag = 0;
boolean twoEwTurnFlag = 0;
boolean fourEwStraightFlag = 0;
boolean trainincomingFlag = 0;
boolean timerActiveFlag = 0;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(NSG, OUTPUT);
pinMode(EWG, OUTPUT);
pinMode(NSR, OUTPUT);
pinMode(EWR, OUTPUT);
pinMode(NST, OUTPUT);
pinMode(EWT, OUTPUT);
pinMode(PUSHBUTTON, INPUT);
}
void loop()
{
if (timerActiveFlag)
{
checkTime();
}
else
{
checkFlags();
}
checkInput();
}
void checkTime()
{
if (millis() - systemTimer >= activeInterval)
{
timerActiveFlag = 0;
}
}
void checkInput()
{
buttonState = digitalRead(PUSHBUTTON);
if (Serial.available()>0)
{
byte incomingByte = Serial.read();
if (buttonState = HIGH)
{
fourEwStraightFlag = 1;
Serial.println("fourEwStraightFlag set");
}
/*else if (incomingByte == 'W')
{
twoNsTurnFlag = 1;
Serial.println("twoNsTurnFlag set");
}
//else if (incomingByte == 'Y')
{
twoEwTurnFlag = 1;
Serial.println("twoEwTurnFlag set");
}
else if (incomingByte == 'X')
{
fourNsStraightFlag = 1;
Serial.println("fourNsStraightFlag set");
}
else if (incomingByte == 'T')
{
trainincomingFlag = 1;
Serial.println("Train incoming set");
}*/
}
}
void checkFlags()
// Only call this function when "timerActiveFlag"
// is zero.
{
timerActiveFlag = 0; // not really needed
if (fourEwStraightFlag)
{
digitalWrite(NSG, LOW);
digitalWrite(EWG, HIGH);
digitalWrite(NSR, HIGH);
digitalWrite(NST, LOW);
digitalWrite(EWR, LOW);
digitalWrite(EWT, LOW);
activeInterval = FOUR_EW_STRAIGHT_TIME;
fourEwStraightFlag = 0;
timerActiveFlag = 1;
Serial.println("East/West Green");
}
else if (twoNsTurnFlag)
{
digitalWrite(NSG, LOW);
digitalWrite(EWG, LOW);
digitalWrite(NSR, HIGH);
digitalWrite(EWR, HIGH);
digitalWrite(NST, HIGH);
digitalWrite(EWT, LOW);
activeInterval = TWO_NS_TURN_TIME;
twoNsTurnFlag = 0;
timerActiveFlag = 1;
Serial.println("North/South Turn");
}
else if (twoEwTurnFlag)
{
digitalWrite(NSG, LOW);
digitalWrite(EWG, LOW);
digitalWrite(NSR, HIGH);
digitalWrite(EWR, HIGH);
digitalWrite(NST, LOW);
digitalWrite(EWT, HIGH);
activeInterval = TWO_EW_TURN_TIME;
twoEwTurnFlag = 0;
timerActiveFlag = 1;
Serial.println("East/West Turn");
}
else if (fourNsStraightFlag)
{
digitalWrite(NSG, HIGH);
digitalWrite(EWG, LOW);
digitalWrite(NSR, LOW);
digitalWrite(EWR, HIGH);
digitalWrite(NST, LOW);
digitalWrite(EWT, LOW);
activeInterval = FOUR_NS_STRAIGHT_TIME;
fourNsStraightFlag = 0;
timerActiveFlag = 1;
Serial.println("North/South Green");
}
else if (trainincomingFlag)
{
digitalWrite(NSG, LOW);
digitalWrite(EWG, LOW);
digitalWrite(NST, LOW);
digitalWrite(EWT, LOW);
digitalWrite(NSR, HIGH);
digitalWrite(EWR, HIGH);
delay (2000);
digitalWrite(NSR, LOW);
digitalWrite(EWR, LOW);
delay (2000);
digitalWrite(NSR, HIGH);
digitalWrite(EWR, HIGH);
delay (2000);
digitalWrite(NSR, LOW);
digitalWrite(EWR, LOW);
delay (2000);
digitalWrite(NSR, HIGH);
digitalWrite(EWR, HIGH);
activeInterval = TRAIN_INCOMING_TIME;
trainincomingFlag = 0;
timerActiveFlag = 1;
Serial.println("Train is incoming");
}
else
{
digitalWrite(NSG, HIGH);
digitalWrite(EWG, LOW);
digitalWrite(NSR, LOW);
digitalWrite(EWR, HIGH);
digitalWrite(NST, LOW);
digitalWrite(EWT, LOW);
timerActiveFlag = 1;
activeInterval = NO_CARS_DEFAULT_TIME;
Serial.println("no active flags, default state");
}
if (timerActiveFlag)
{
systemTimer = millis();
}
}
Any advice on how to implement a pushbutton input would be great.