After trying to figure out how to integrate the two switches together, I came up with the idea of using the potentiometer to toggle between three different LEDs – red for a bad face, yellow for a medium face and green for a happy face. This would lead to some sort of customer service device to give feedback on how you feel.
So far I have been understanding the wiring pretty well but to connect the coding with making what I want to physically happen has been slightly difficult. It feels difficult for my brain to wrap around. I was doing okay with the coding part of the first half of the semester, and the wiring physical part we’re learning now – but I’m a little slow to combine them.
In terms of creating this project – It was relatively easy to get the button to work (to turn on the blue LED) and to get the potentiometer connected to the three colored LEDs. With a little help from Juan, I was able to connect the two together and make it such that wen the button is pressed, the blue LED turns on and so does the potentiometer LEDs, and when clicked again, the customer service ‘device’ turns off.
const int redLight = 9; const int yellowLight = 10; const int greenLight = 11; const int blueOnLight = 4; const int button = 5; const int LED_number = 3; int ledState = LOW; int lastButtonState; int currentButtonState; void setup() { pinMode(redLight, OUTPUT); pinMode(yellowLight, OUTPUT); pinMode(greenLight, OUTPUT); pinMode(blueOnLight, OUTPUT); pinMode(button, INPUT_PULLUP); Serial.begin(9600); currentButtonState = digitalRead(button); } void loop() { int potValue = analogRead(A0); int ledChoice = potValue / (1024 / LED_number); //for checking if circuit is on or off lastButtonState = currentButtonState; currentButtonState = digitalRead(button); if(lastButtonState == HIGH && currentButtonState == LOW) { // toggle state of LED ledState = !ledState; // control LED arccoding to the toggled state digitalWrite(blueOnLight, ledState); } //for customer service lights if (ledState == HIGH){ if (ledChoice > LED_number - 1) { ledChoice = LED_number - 1; } else if (ledChoice == 0) { digitalWrite(redLight, HIGH); digitalWrite(yellowLight, LOW); digitalWrite(greenLight, LOW); } else if (ledChoice == 1) { digitalWrite(redLight, LOW); digitalWrite(yellowLight, HIGH); digitalWrite(greenLight, LOW); } else { digitalWrite(redLight, LOW); digitalWrite(yellowLight, LOW); digitalWrite(greenLight, HIGH); } } else { digitalWrite(redLight, LOW); digitalWrite(yellowLight, LOW); digitalWrite(greenLight, LOW); } Serial.println(potValue); }
Without further ado, I present you with the customer service device: