I want to control a LED with PWM and two push buttons. If you keep a button pressed it should be brighter with the "more button" and less bright with the "less button". Now nothing happens.
#define LED 9 // PWM
#define more 2 // two
#define less 4 // buttons
byte val = 0; // variable for reading the pwm status
void setup() {
pinMode(LED, OUTPUT); // declare LED as PWM output
pinMode(more, INPUT); // declare pushbutton as input
pinMode(less, INPUT); // declare pushbutton as input
}
void loop(){
digitalRead(more); // read button
if (more == HIGH) {
val = val + 5; // more pwm
analogWrite(LED, val);
}
digitalRead(less);
if (less == HIGH) {
val = val - 5; // less pwm
analogWrite(LED, val);
}
delay(100);
//Later I want to show the value on a LCD
Your sketch isn't using the value of the buttons, try changing:
digitalRead(more); // read button
if (more == HIGH) { // more is the pin number, not the value from digitalRead()
val = val + 5; // more pwm
analogWrite(LED, val);
}
to:
if (digitalRead(more) == HIGH) {
val = val + 5; // more pwm
analogWrite(LED, val);
}
Also, your code will wrap, that is when the value exceeds 255 it will wrap around and start increment from 0. If that is not what you want you can add code that ignores increments / decrements if the value would increase above 255 or below 0
Thank you mem! It kind of works now.
I put Serial.println(val, DEC); at the end of the sketch and I noticed the value was floating around a bit and It didn't work to raise the value by holding the switch in (I don't have a switch, just wired the 5V through a 10KOhm to the pin). I had to poke it in and out and then "something" happened.
void loop(){
if (digitalRead(more) == HIGH) { // read value of button
val = val + 1; // more pwm
analogWrite(LED, val);
}
delay(10);
if (digitalRead(less) == HIGH) {
val = val - 1; // less pwm
analogWrite(LED, val);
}
Serial.println(val, DEC);
delay(10);
}
Edit: Maby I should stick the buttons to analog in and only listen to values over 4.90V? I'll give it a try :o
Then that is your problem, get a switch fitted or just a wire that you dab on to the ground.
You won't need a pull up resistor if you do a digital write HIGH - AFTER you initialise that pin to an input in the setup.
Dabing to ground will give you a zero, un dabing will give you a one.
#define LED 6 // PWM
int analog = 3; // button
int analogPin = 1; // button
byte val = 0; // variable for reading the pwm status
void setup() {
pinMode(LED, OUTPUT); // declare LED as PWM output
pinMode(3, INPUT); // declare pushbutton as input
pinMode(1, INPUT); // declare pushbutton as input
Serial.begin(9600);
}
void loop(){
if (analogRead(1) > 1000) { // read value of button
val = val + 5; // more pwm
analogWrite(LED, val);
}
if (analogRead(3) > 1000) {
val = val - 5; // less pwm
analogWrite(LED, val);
}
Serial.println(val, DEC);
delay(30);
BUT, I have to poke (dab?) the wire to the ground and each time the value increase about ~20. Shouldn't it just continue raising the value each time it goes through the loop and never stop?
If you want to implement a pushbutton, using an analog input doesn't make sense. You just want a PRESSED or NOT PRESSED result. Use a digital input. (Even if your "pushbutton" is a bit of wire that you stab, dab, touch, prod or poke against the breadboard.)
To wire a button to a digital input, you have two basic choices: wire your own pullup/pulldown resistor, or ask the Arduino controller to use its own pullup resistor. Grumpy_Mike was suggesting you try the second choice, since it was not clear if you were using any resistor at all. Without a pullup or pulldown resistor, the signal may "float" when the button is not pushed.
To ask the Arduino to use its own pullup resistors, try this setup:
void setup() {
pinMode(LED, OUTPUT); // declare LED as PWM output
pinMode(more, INPUT); // declare pushbutton as input
pinMode(less, INPUT); // declare pushbutton as input
digitalWrite(more, HIGH); // use internal pullup feature
digitalWrite(less, HIGH); // use internal pullup feature
Serial.begin(9600);
}
Now your switch or button should be wired between the GROUND and the INPUT pin of your choice. When pressed, the input value will be LOW due to the connection to ground. When not pressed, the input value will be HIGH due to the pullup resistor.
Yes! It's alive! Thank you Halley. I have to look up what this internal pull up resistor is.
I changed to if (digitalRead(more) == LOW) and it works.
Now it would be good to make the byte value not wrap.
Edit: Hmm something isn't right. Both switches makes the value go up?
#define LED 6 // PWM
int more = 12; // button
int less = 13; // button
byte val = 0; // variable for reading the pwm status
void setup() {
pinMode(LED, OUTPUT); // declare LED as PWM output
pinMode(more, INPUT); // declare pushbutton as input
pinMode(less, INPUT); // declare pushbutton as input
digitalWrite(more, HIGH); // use internal pullup feature
digitalWrite(less, HIGH); // use internal pullup feature
Serial.begin(9600);
}
void loop(){
if (digitalRead(more) == HIGH) { // read value of button
val = val + 5; // more pwm
analogWrite(LED, val);
}
if (digitalRead(less) == HIGH) {
val = val - 5; // less pwm
analogWrite(LED, val);
}
Serial.println(val, DEC);
delay(100);
}
Edit: It works now!! Probably something with the brackets.
if (digitalRead(more) == HIGH) // read value of button
{
if(val != 255)
{
val = val + 1;
analogWrite(LED, val); // write value
}
}
HAPPY! ;D this is just the beginning!
Edit:
if ((digitalRead(more) == HIGH) && (val != 255)) // read value of button
{
val = val + 1;
analogWrite(LED, val); // write value
Serial.println(val, DEC);
}
This is even better! And I put Serial.println inside the if statement so it isn't flooding.