5
u/Mandojim Mar 03 '23
That first blinking light project is what gets you hooked. We’ve all been there. Have fun!
1
u/mechmind Mar 03 '23
Great job! Now make a button that speeds up the count by double every press.
So proud of you!
1
Mar 03 '23
I have a rotary encoder with a button that I got for a future project and this seems like a good way to learn something about coding a button!👍
1
u/mechmind Mar 04 '23
Yes, we'll that's next level. But I think you might be ready!
int pin1 = 2; // Connect pin 1 of the encoder to pin 2 of the Arduino int pin2 = 3; // Connect pin 2 of the encoder to pin 3 of the Arduino int pinButton = 4; // Connect the push button of the encoder to pin 4 of the Arduino
int encoderValue = 0; // Variable to store the encoder value
void setup() { Serial.begin(9600); // Start serial communication pinMode(pin1, INPUT); // Set pin 1 as an input pinMode(pin2, INPUT); // Set pin 2 as an input pinMode(pinButton, INPUT); // Set the button as an input }
void loop() { if (digitalRead(pin1) == HIGH) { // If pin 1 is high if (digitalRead(pin2) == LOW) { // And pin 2 is low encoderValue++; // Increment the encoder value Serial.println(encoderValue); // Print the encoder value to the serial monitor } }
if (digitalRead(pin1) == LOW) { // If pin 1 is low if (digitalRead(pin2) == HIGH) { // And pin 2 is high encoderValue--; // Decrement the encoder value Serial.println(encoderValue); // Print the encoder value to the serial monitor } }
if (digitalRead(pinButton) == HIGH) { // Check if the button has been pressed Serial.println("Button Pressed"); // Print "Button Pressed" to the serial monitor } }
1
1
1
1
1
5
u/jimbomescolles Mar 03 '23 edited Mar 03 '23
Nice !
My next challenge :
Replace the blue LED by an orange/yellow one and create a traffic light.
Add adjustable timing for the green and red states, as well as the intermediate orange (before red) or orange-red (before green).