So I am trying to make a fan speed controller using a Freenove ESP32 Wrover board. I want to be able to control the fan speed using PMW and have a squental push button going from OFF-LOW-MED-HIGH and cycling back to OFF. I am able to do a flashing light just out of testing, but the following code only give me a flashing onboard LED and a solid extarnal LED. The button does nothing. Roger001.ino (1.2 KB)
void setup() {
pinMode(8, OUTPUT);
pinMode(4, OUTPUT);
pinMode(10, INPUT_PULLUP);
}
void loop() {
int counter=0;
digitalRead(10);
if (digitalRead(LOW);
counter=counter+1);
switch (counter) {
case 1:
(counter==1);
analogWrite(4, 85);
break;
case 2:
(counter==2);
analogWrite(4, 170);
break;
case 3:
(counter==3);
analogWrite(4, 255);
break;
default:
analogWrite(4, 0);
if (counter>=4)
{counter=0;}
break;
}
digitalWrite(8, HIGH);
delay(150);
digitalWrite(8, LOW);
delay(150);
}
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
I will have to draw up the diagram later but it is as simple as I can I am using a LED in place of where the fan would be and I have the button wired as a pull-up circuit with a resistor
So I updated the code to if (digitalRead(10) == LOW) { counter = counter + 1); }
and I get the following error
`ino:15:24: error: expected ';' before ')' token
15 | counter = counter + 1);
| ^
| ;
My bad Ithe code I sent I was originally using a ESP32C3 DEV I changed the pins accordingly for the Freenove board. Which was a second board to verify it was not a hardware issue. I did look up what pins could do PMW and the digital I/O with pullup/pulldown capabilities.
So that is the bool and if statement is how it is done correct.
Just trying to work it though so I understand for next time. The bool give a on-off input as a 1 or 0 what's the reason for the short delay of 30 millisecond. I am not quite understanding the if statement if you could elaborate on that for me.
bsides the various errors in your code, simply incrementing a counter whenever the buttow is LOW when pressed will
increment is 100s of time within the short time that it is pressed, and
there's no upper limit checked for to wrap from 0-4
isutPressed() returns true once when the button changes state and becomes LOW. This avoid incrementing some 1000s of times
and when pressed, it decrements speed, reseting it to 3, High speed, so that from 0 it goes to the High speed first, then subsequent button presses go to Medium, Low and Off.