Fan controller that controls nothing

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);
}

Hi, @B3ND3R
Welcome to the forum.

Please post your code in the post?

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.

Thanks... Tom.... :smiley: :+1: :coffee: :australia:

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

digitalRead(10);
if (digitalRead(LOW);
  counter=counter+1);

There are sintax errors.
May be

if (digitalRead(10) == LOW) {
  counter = counter + 1;
}

work for you. :wink:

1 Like

Hi, @B3ND3R

Can you please post this code also?

digitalRead(10);
if (digitalRead(LOW);

Needs to be.

if (digitalRead(10) == LOW) 

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:
PS Semicolon edited :+1: @MaximoEsfuerzo

Without semicolon. :wink:

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);
| ^
| ;

exit status 1

Compilation error: expected ';' before ')' token`

and I am running Arduino IDE 2.3.4

  • counter value is constantly re-initialized to 0 because is it local and not global
  • digitalRead(10); does set a variable to the state of the pin being read
  • if (digitalRead(LOW); counter=counter+1); doesn't make sense, look at what is inside the parenthesis

look this over

  • needs to use a valid PWM pin
  • decrements the speed from HIGH to Off
  • simply turns LED on when fan is on
// 3-speed PWM fan controller

const byte PinFan = 4;      // PWM pins are 3, 5, 6, 9, 10, 11
const byte PinLed = 8;
const byte PinBut = 10;

enum { LedOff = HIGH, LedOn = LOW };

byte butState;

int speed = 0;

// -----------------------------------------------------------------------------
void loop ()
{
    // adjust speed setting if button pressed
    if (isButPressed ())  {
        if (0 > --speed)
            speed = 3;
        Serial.print ("speed "); Serial.println (speed);
    }

    // set pwm value for speed setting
    switch (speed) {
    case 3:
        analogWrite (PinFan, 255);      // high
        digitalWrite (PinLed, LedOn);
        break;

    case 2:
        analogWrite (PinFan, 170);      // medium
        digitalWrite (PinLed, LedOn);
        break;

    case 1:
        analogWrite (PinFan,  85);      // low
        digitalWrite (PinLed, LedOn);
        break;

    case 0:
        analogWrite (PinFan, 0);        // off
        digitalWrite (PinLed, LedOff);
        break;
    }
}

// -----------------------------------------------------------------------------
bool
isButPressed ()
{
    byte but = digitalRead (PinBut);
    if (butState != but)  {     // state change
        butState  = but;
        delay (30);             // debunce

        return LOW == but;      // only return true if LOW
    }
    return false;
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    pinMode (PinFan, OUTPUT);
    pinMode (PinLed, OUTPUT);

    pinMode (PinBut, INPUT_PULLUP);
    butState = digitalRead (PinBut);
}

there's an extra parenthesis after the 1

You should find other pins to use instead of 8 and 10.
Gpio 6-11 are for flash memory.

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.

Does that translate that your posted code is not the "real code"...?

No that is the code I am using I only changed the pins accordingly for the new board that is the code I will be using on that board specifically.

How would I setup the counter variable I only need it to be in 4 different values but to change between those values as the code runs

i showed how in my code using the speed variable

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.

So I have tried copying your code but it keeps saying is but pressed is not declared where am I missing it being declared

please post the code you get this error from

As you wish.

const byte ONBOR = 8;
const byte FAN = 4;
const byte BUTN = 10;
byte ButnState;
int SPEED = 6;


void setup() {
  pinMode(8, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(10, INPUT_PULLUP);
  ButnState = digitalRead (BUTN);
  Serial.begin (9600);
}

void loop() {
bool 
isPressed ()
{
    byte BUT = digitalRead (BUTN);
  if (ButnState != BUT){
    ButnState = BUT;
    delay (30);
      return LOW == BUT;
  }
  return false;
  }
if (isPressed ()){
  if (0 > --SPEED)
    SPEED = 3;
  Serial.print ("Speed");
    Serial.println (SPEED);
}
/*  int counter=0;
if (digitalRead(10) == LOW) { counter = counter + 1; }
*/
  
switch (SPEED) {
  case 0:
    analogWrite(FAN, 0);
  case 1:
    analogWrite(FAN, 85);
      break;
  case 2:
    analogWrite(FAN, 170);
      break;
  case 3:
    analogWrite(FAN, 255);
      break;
}
digitalWrite(ONBOR, HIGH);
  delay(150);
digitalWrite(ONBOR, LOW);
  delay(150);
}