My first simple sketch don't work.
i want control led pwm , 2 push buttons. if keep button pressed should brighter "more button" , less bright "less button". nothing happens.
code: [select]
#define led 9 // pwm
#define more 2 // two
#define less 4 // buttons
byte val = 0; // variable reading pwm status
void setup() {
pinmode(led, output); // declare led pwm output
pinmode(more, input); // declare pushbutton input
pinmode(less, input); // declare pushbutton 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 want show value on lcd
hi spruft,
your sketch isn't using value of buttons, try changing:
digitalread(more); // read button
if (more == high) { [glow] // more pin number, not value digitalread()[/glow]
val = val + 5; // more pwm
analogwrite(led, val);
}
to:
if (digitalread(more) == high) {
val = val + 5; // more pwm
analogwrite(led, val);
}
also, code wrap, when value exceeds 255 wrap around , start increment 0. if not want can add code ignores increments / decrements if value increase above 255 or below 0
your sketch isn't using value of buttons, try changing:
digitalread(more); // read button
if (more == high) { [glow] // more pin number, not value digitalread()[/glow]
val = val + 5; // more pwm
analogwrite(led, val);
}
to:
if (digitalread(more) == high) {
val = val + 5; // more pwm
analogwrite(led, val);
}
also, code wrap, when value exceeds 255 wrap around , start increment 0. if not want can add code ignores increments / decrements if value increase above 255 or below 0
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > My first simple sketch don't work.
arduino
Comments
Post a Comment