/*
*/
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
#define ledRed 6
#define ledYellow 7
#define ledGreen 8
#define button 9
int buttonState;
int preButtonState;
int count = 0;
void setup() {
lcd.begin(16,2);
pinMode(ledRed, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(button, INPUT_PULLUP);
lcd.print("The colour is");
lcd.setCursor(0,1);
}
void loop() {
buttonState = digitalRead(button);
if (buttonState != preButtonState){
if (buttonState == LOW){
count++;
if(count == 1){
digitalWrite(ledRed, HIGH);
digitalWrite(ledYellow, LOW);
digitalWrite(ledGreen, LOW);
lcd.print("Red");
} else if (count == 2){
digitalWrite(ledRed, LOW);
digitalWrite(ledYellow, HIGH);
digitalWrite(ledGreen, LOW);
lcd.print("Yellow");
} else if (count == 3){
digitalWrite(ledRed, LOW);
digitalWrite(ledYellow, LOW);
digitalWrite(ledGreen, HIGH);
lcd.print("Green");
} else if (count == 4) {
digitalWrite(ledRed, LOW);
digitalWrite(ledYellow, LOW);
digitalWrite(ledGreen, LOW);
lcd.print("Off");
count = 0;
}
}
}
delay(50);
preButtonState = buttonState;
}
I'm trying to practice my coding skills, with some examples.
The code is suppose to switch on different led lights with each press of the button.
Been trying to make it work, but somehow the code does not seems to work.
I think it have something to do with the count++ as it seems that the counter do not work.
But could not find any solutions.
Hope that someone here can guild me.
arduarn
November 29, 2017, 11:28am
2
So one side of your button switch is connected to digital pin 9 and the other side to GND, right?
arduarn
November 29, 2017, 11:49am
4
Well, try printing some debug text to serial.
try adding Serial.print as advised, and don't forget to move the cursor back to your desired print start location on your LCD:
/*
*/
#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define ledRed 6
#define ledYellow 7
#define ledGreen 8
#define button 9
int buttonState;
int preButtonState;
int count = 0;
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(ledRed, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(button, INPUT_PULLUP);
lcd.print("The colour is");
lcd.setCursor(0, 1);
}
void loop()
{
buttonState = digitalRead(button);
if (buttonState != preButtonState)
{
if (buttonState == LOW)
{
count++;
Serial.print(F("Press Count:\t"));
Serial.println(count);
lcd.setCursor(0, 1);
if (count == 1)
{
digitalWrite(ledRed, HIGH);
digitalWrite(ledYellow, LOW);
digitalWrite(ledGreen, LOW);
lcd.print("Red");
}
else if (count == 2)
{
digitalWrite(ledRed, LOW);
digitalWrite(ledYellow, HIGH);
digitalWrite(ledGreen, LOW);
lcd.print("Yellow");
}
else if (count == 3)
{
digitalWrite(ledRed, LOW);
digitalWrite(ledYellow, LOW);
digitalWrite(ledGreen, HIGH);
lcd.print("Green");
}
else if (count == 4)
{
digitalWrite(ledRed, LOW);
digitalWrite(ledYellow, LOW);
digitalWrite(ledGreen, LOW);
lcd.print("Off");
count = 0;
}
}
}
delay(50);
preButtonState = buttonState;
}
what are the chances .....
Finally found what is wrong .......
all three of my led are burnt out....
Hi
Are you using series current limit resistors with the LEDs?
https://www.sparkfun.com/tutorials/219
Tom...
I'm using 220ohm resistors.
Maybe it's the quality of the components I have.
I got those from a course that I took.
Technic168:
I'm using 220ohm resistors.
Maybe it's the quality of the components I have.
I got those from a course that I took.
are they wired in the right direction?