My idea was to make a traffic light with the red, yellow and green LEDs. I decided to use a single switch button that cycles between modes as well as a potentiometer that changes the LED activated.
In order to make the button that cycles between modes, I got a lot of help from https://gist.github.com/navillus5/6699360
The 3 modes of my traffic light:
- All LEDS blinking
- Potentiometer activates either of the red, yellow, and green LEDs when dialed to a specific point
- Off
Overall I think I executed my idea well as everything I intended for the assignment works, however one small issue I ran into is that when switched to the 2nd mode, the LEDS are very dim. My best guess is that it has to do with the resistors but I haven’t tested out this theory as I don’t want to accidentally damage my LEDs.
My Code:
const int buttonPin = 3;
const int redLight = 9;
const int yellowLight = 6;
const int greenLight = 5;
unsigned long timer = 0;
bool onOff = LOW;
byte prevButtonState = LOW;
bool blinking = false;
byte buttonMode = 0;
bool traffic = false;
boolean currentState = LOW;
boolean lastState = LOW;
boolean stateChange = false;
int currentButton = 0;
int lastButton = 2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(redLight, OUTPUT);
pinMode(yellowLight, OUTPUT);
pinMode(greenLight, OUTPUT);
digitalWrite(redLight, LOW);
digitalWrite(yellowLight, LOW);
digitalWrite(greenLight, LOW);
pinMode(buttonPin, INPUT);
}
void loop() {
currentState = debounceButton();
stateChange = checkForChange(currentState, lastState);
currentButton = getButtonNumber(lastButton, currentState, stateChange);
lastState = currentState;
lastButton = currentButton;
// put your main code here, to run repeatedly:
int potValue = analogRead(A0);
int mappedPotValue = map(potValue, 0, 1023, 0, 255);
int constrainedPotValue = constrain(mappedPotValue, 0, 255);
Serial.println(currentButton);
// byte buttonState = digitalRead(buttonPin);
if (currentButton == 0) {
blinking = true;
// buttonMode = buttonMode + 1;
}
if (currentButton == 1) {
blinking = false;
traffic = true;
}
if (currentButton == 2) {
traffic = false;
}
if (traffic == true) {
if (constrainedPotValue > 170) {
digitalWrite(redLight, HIGH);
digitalWrite(yellowLight, LOW);
digitalWrite(greenLight, LOW);
}
if (constrainedPotValue <= 170 && constrainedPotValue > 85) {
digitalWrite(redLight, LOW);
digitalWrite(yellowLight, HIGH);
digitalWrite(greenLight, LOW);
}
if (constrainedPotValue <= 85) {
digitalWrite(redLight, LOW);
digitalWrite(yellowLight, LOW);
digitalWrite(greenLight, HIGH);
}
}
if (blinking == true) {
if (millis() > timer) {
onOff = !onOff;
timer = millis() + 250;
digitalWrite(redLight, onOff);
digitalWrite(yellowLight, onOff);
digitalWrite(greenLight, onOff);
}
} else {
digitalWrite(redLight, LOW);
digitalWrite(yellowLight, LOW);
digitalWrite(greenLight, LOW);
}
}
// FUNCTIONS
//debounce
boolean debounceButton()
{
boolean firstCheck = LOW;
boolean secondCheck = LOW;
boolean current = LOW;
firstCheck = digitalRead(buttonPin);
delay(50);
secondCheck = digitalRead(buttonPin);
if (firstCheck == secondCheck){
current = firstCheck;
}
return current;
}
//checks for change
boolean checkForChange(boolean current, boolean last)
{
boolean change;
if (current != last){
change = true;
}
else {
change = false;
}
return change;
}
//gets button num
int getButtonNumber(int button, boolean state, boolean change)
{
if (change == true && state == LOW){
button++;
if (button > 2){
button = 0;
}
Serial.println(button);
}
return button;
}
const int buttonPin = 3;
const int redLight = 9;
const int yellowLight = 6;
const int greenLight = 5;
unsigned long timer = 0;
bool onOff = LOW;
byte prevButtonState = LOW;
bool blinking = false;
byte buttonMode = 0;
bool traffic = false;
boolean currentState = LOW;
boolean lastState = LOW;
boolean stateChange = false;
int currentButton = 0;
int lastButton = 2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(redLight, OUTPUT);
pinMode(yellowLight, OUTPUT);
pinMode(greenLight, OUTPUT);
digitalWrite(redLight, LOW);
digitalWrite(yellowLight, LOW);
digitalWrite(greenLight, LOW);
pinMode(buttonPin, INPUT);
}
void loop() {
currentState = debounceButton();
stateChange = checkForChange(currentState, lastState);
currentButton = getButtonNumber(lastButton, currentState, stateChange);
lastState = currentState;
lastButton = currentButton;
// put your main code here, to run repeatedly:
int potValue = analogRead(A0);
int mappedPotValue = map(potValue, 0, 1023, 0, 255);
int constrainedPotValue = constrain(mappedPotValue, 0, 255);
Serial.println(currentButton);
// byte buttonState = digitalRead(buttonPin);
if (currentButton == 0) {
blinking = true;
// buttonMode = buttonMode + 1;
}
if (currentButton == 1) {
blinking = false;
traffic = true;
}
if (currentButton == 2) {
traffic = false;
}
if (traffic == true) {
if (constrainedPotValue > 170) {
digitalWrite(redLight, HIGH);
digitalWrite(yellowLight, LOW);
digitalWrite(greenLight, LOW);
}
if (constrainedPotValue <= 170 && constrainedPotValue > 85) {
digitalWrite(redLight, LOW);
digitalWrite(yellowLight, HIGH);
digitalWrite(greenLight, LOW);
}
if (constrainedPotValue <= 85) {
digitalWrite(redLight, LOW);
digitalWrite(yellowLight, LOW);
digitalWrite(greenLight, HIGH);
}
}
if (blinking == true) {
if (millis() > timer) {
onOff = !onOff;
timer = millis() + 250;
digitalWrite(redLight, onOff);
digitalWrite(yellowLight, onOff);
digitalWrite(greenLight, onOff);
}
} else {
digitalWrite(redLight, LOW);
digitalWrite(yellowLight, LOW);
digitalWrite(greenLight, LOW);
}
}
// FUNCTIONS
//debounce
boolean debounceButton()
{
boolean firstCheck = LOW;
boolean secondCheck = LOW;
boolean current = LOW;
firstCheck = digitalRead(buttonPin);
delay(50);
secondCheck = digitalRead(buttonPin);
if (firstCheck == secondCheck){
current = firstCheck;
}
return current;
}
//checks for change
boolean checkForChange(boolean current, boolean last)
{
boolean change;
if (current != last){
change = true;
}
else {
change = false;
}
return change;
}
//gets button num
int getButtonNumber(int button, boolean state, boolean change)
{
if (change == true && state == LOW){
button++;
if (button > 2){
button = 0;
}
Serial.println(button);
}
return button;
}
const int buttonPin = 3; const int redLight = 9; const int yellowLight = 6; const int greenLight = 5; unsigned long timer = 0; bool onOff = LOW; byte prevButtonState = LOW; bool blinking = false; byte buttonMode = 0; bool traffic = false; boolean currentState = LOW; boolean lastState = LOW; boolean stateChange = false; int currentButton = 0; int lastButton = 2; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(redLight, OUTPUT); pinMode(yellowLight, OUTPUT); pinMode(greenLight, OUTPUT); digitalWrite(redLight, LOW); digitalWrite(yellowLight, LOW); digitalWrite(greenLight, LOW); pinMode(buttonPin, INPUT); } void loop() { currentState = debounceButton(); stateChange = checkForChange(currentState, lastState); currentButton = getButtonNumber(lastButton, currentState, stateChange); lastState = currentState; lastButton = currentButton; // put your main code here, to run repeatedly: int potValue = analogRead(A0); int mappedPotValue = map(potValue, 0, 1023, 0, 255); int constrainedPotValue = constrain(mappedPotValue, 0, 255); Serial.println(currentButton); // byte buttonState = digitalRead(buttonPin); if (currentButton == 0) { blinking = true; // buttonMode = buttonMode + 1; } if (currentButton == 1) { blinking = false; traffic = true; } if (currentButton == 2) { traffic = false; } if (traffic == true) { if (constrainedPotValue > 170) { digitalWrite(redLight, HIGH); digitalWrite(yellowLight, LOW); digitalWrite(greenLight, LOW); } if (constrainedPotValue <= 170 && constrainedPotValue > 85) { digitalWrite(redLight, LOW); digitalWrite(yellowLight, HIGH); digitalWrite(greenLight, LOW); } if (constrainedPotValue <= 85) { digitalWrite(redLight, LOW); digitalWrite(yellowLight, LOW); digitalWrite(greenLight, HIGH); } } if (blinking == true) { if (millis() > timer) { onOff = !onOff; timer = millis() + 250; digitalWrite(redLight, onOff); digitalWrite(yellowLight, onOff); digitalWrite(greenLight, onOff); } } else { digitalWrite(redLight, LOW); digitalWrite(yellowLight, LOW); digitalWrite(greenLight, LOW); } } // FUNCTIONS //debounce boolean debounceButton() { boolean firstCheck = LOW; boolean secondCheck = LOW; boolean current = LOW; firstCheck = digitalRead(buttonPin); delay(50); secondCheck = digitalRead(buttonPin); if (firstCheck == secondCheck){ current = firstCheck; } return current; } //checks for change boolean checkForChange(boolean current, boolean last) { boolean change; if (current != last){ change = true; } else { change = false; } return change; } //gets button num int getButtonNumber(int button, boolean state, boolean change) { if (change == true && state == LOW){ button++; if (button > 2){ button = 0; } Serial.println(button); } return button; }