I'm trying to make a traffic signal controller for a single light. I haven't made much with an Arduino. I am a newbee for sure. Any help will be appreciated.
It seems to get stuck on the red light when the potentiometer is turned up to anything above zero. When the pot* is at zero the light cycle correctly with a 30 second delay.
int redLight = 13;
int yellowLight = 12;
int greenLight = 11;
int potPin = A0;
int potValue = 0;
int delayTime = 0;
void setup()
{
pinMode(redLight, OUTPUT);
pinMode(yellowLight, OUTPUT);
pinMode(greenLight, OUTPUT);
digitalWrite(redLight,HIGH);
digitalWrite(yellowLight,HIGH);
digitalWrite(greenLight,HIGH);
}
void loop(){
potValue = analogRead(potPin);
delayTime = map(potValue, 0, 1023, 30000, 180000);
digitalWrite(redLight,HIGH);
delay(delayTime);
digitalWrite(redLight,LOW);
digitalWrite(greenLight,LOW);
delay(delayTime);
digitalWrite(greenLight,HIGH);
digitalWrite(yellowLight,LOW);
delay(3000);
digitalWrite(yellowLight,HIGH);
}
What was wrong was, I used an integer to hold the value for the delay time. An integer yields a range of -32,768 to 32,767. I wanted the time delay to vary from 30 seconds as the minimum to a maximum 3 minutes. In milliseconds this would be 30000 to 180000, which wouldn't fit in the integer.
Thank you ballscrewbob for the help.
See the final code below
int redLight = 13;
int yellowLight = 12;
int greenLight = 11;
int val = 0;
unsigned long rest;
void setup()
{
pinMode(redLight, OUTPUT);
pinMode(yellowLight, OUTPUT);
pinMode(greenLight, OUTPUT);
digitalWrite(redLight,LOW);
digitalWrite(yellowLight,HIGH);
digitalWrite(greenLight,HIGH);
}
void loop(){
val = analogRead(A0);
rest = map(val, 0, 1023, 30000, 180000);
delay(100);
digitalWrite(redLight,HIGH);
delay(rest);
digitalWrite(redLight,LOW);
digitalWrite(greenLight,LOW);
delay(rest);
digitalWrite(greenLight,HIGH);
digitalWrite(yellowLight,LOW);
delay(3000);
digitalWrite(yellowLight,HIGH);
}