Hi.
I try to learn to program for arduino a little bit. This time it is to create traffic lights: red led, yellow led and green led. We define 2 cycles with the program. The short cycle is for the yellow light. The long cycle is controlled by the rotary encoder for the time lengh: red and green lights.
I kind of understand 95% of the code but not completely. Maybe you can help me with that.
The questions is in the commentary next to the lines.
Thx a lot.
I try to learn to program for arduino a little bit. This time it is to create traffic lights: red led, yellow led and green led. We define 2 cycles with the program. The short cycle is for the yellow light. The long cycle is controlled by the rotary encoder for the time lengh: red and green lights.
I kind of understand 95% of the code but not completely. Maybe you can help me with that.
The questions is in the commentary next to the lines.
Thx a lot.
Code:
const int redPin= 7; //red led attach to
const int yellowPin =8 ; //yellow led attach to
const int greenPin= 9; //green led attach to
const int clkPin= 2;
const int dtPin= 3;
const int swPin= 6; //the number of the button
int encoderVal = 0;
int state = 0;
int shortPeriod = 1000;
int longPeriod = 1000; // why 1000?????
int targetCount = shortPeriod;
int count = 0;
void setup()
{
pinMode(clkPin, INPUT);
pinMode(dtPin, INPUT);
pinMode(swPin, INPUT);
digitalWrite(swPin, HIGH);
pinMode(redPin, OUTPUT); //set the redPin as an output
pinMode(yellowPin, OUTPUT); //set the yellowPin as an output
pinMode(greenPin, OUTPUT); //set the greenPin as an output
Serial.begin(9600); // start serial port at 9600 bps:
}
void loop()
{
count++;
int change = getEncoderTurn();
longPeriod = longPeriod + change * 1000;
if (digitalRead(swPin) == LOW)
{
setLights(HIGH, HIGH, HIGH); '' // why are they all HIGH???
}
else
{
if (count == targetCount) // Not sure about this.
{
setState();
count = 0;
}
}
delay(1);
}
void setState(void)
{
if (state == 0)
{
setLights(HIGH, LOW, LOW);
targetCount = longPeriod;
state = 1;
}
else if (state == 1)
{
setLights(HIGH, HIGH, LOW);
targetCount = shortPeriod;
state = 2;
}
else if (state == 2)
{
setLights(LOW, LOW, HIGH);
targetCount = longPeriod;
state = 3;
}
else if (state == 3)
{
setLights(LOW, HIGH, LOW);
targetCount = shortPeriod;
state = 0;
}
}
int getEncoderTurn(void)
{
static int oldA = HIGH;
static int oldB = HIGH;
int result = 0; // what is result??
int newA = digitalRead(clkPin);
int newB = digitalRead(dtPin);
if (newA != oldA || newB != oldB)
{
// something has changed
if (oldA == HIGH && newA == LOW)
{
result = (oldB * 2 - 1); // I'm lost for this line??
}
}
oldA = newA;
oldB = newB;
return result;
}
// the function to set the led with the specified state(on or off),HIGH is on, and LOW is off
void setLights(int redState, int yellowState, int greenState)
{
digitalWrite(redPin, redState);
digitalWrite(yellowPin, yellowState);
digitalWrite(greenPin, greenState);
}
Attachments
Last edited by a moderator: