the project is to make a dimmable led, by highing and lowering the analogwrite but whenether i try to high it goes down quickly
the code:
int ledpin=3;
int buttpin1=12;
int buttpin2=8;
int buttval1;
int buttval2;
int totalval=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ledpin, OUTPUT);
pinMode(buttpin1, INPUT);
pinMode(buttpin2, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttval1=digitalRead(buttpin1);
buttval2=digitalRead(buttval2);
if (buttval1==0){
totalval=totalval+5;
}
if (buttval2==0){
totalval=totalval-5;
}
Serial.print(totalval);
if (totalval>255){
totalval=255;
}
if (totalval<0){
totalval=0;
}
analogWrite(ledpin, totalval);
}
ps:be as soon as possible!
How do you have the buttons connected? Do you have external pull-up resistors?
Also, loop() runs very fast... you may want to consider only changing the value of totalval every X milliseconds.
In the first line you read signal from buttpin1 pin, but in second from buttval2...
Shouldn't it be buttpin2 instead?
2 Likes
ohhh, so sorry my fault!
didnt see that
Did the issue fixed? if so, please mark the thread as solved
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.