Need help in my fan speed control related project

Fan Speed Control Using RBDdimmer(MOC,BT136)

Hello everyone greeting to all,

I am working on a project related to fan speed control using zero-cross method.
i want to control fan speed using the manuals push button and with the remote.

can anyone help me with it here are my codes.

#include <RBDdimmer.h>
#include <IRremote.h>

#define outputPin  5
#define zerocross  12

int RECV_PIN = 3;

#define LAMPMAXVALUE 100

#define upbotton 8
#define downbotton 9

int outVal = 0;
int lastup = 0;
int lastdown = 0;

int up = 0;
int down = 0;

int dim_val = 0;
byte dim_valChange = 100 / 6;
bool setLamp = true;

IRrecv irrecv(RECV_PIN);
decode_results results;
dimmerLamp dimmer(outputPin);

void ir_remote() {
  if (irrecv.decode(&results)) {
    switch (results.value) {

      case 0xFF708F:
        {
          if (up == LOW)
          {
            dim_val = dim_val + dim_valChange;
            if (dim_val > 100)
            {
              dim_val = 100;
            }
          }
          lastup = up;
          break;
        }
      case 0xFF58A7:
        {
          if (down == LOW)
          {
            dim_val = dim_val - dim_valChange;
            if (dim_val <= 45)
            {
              dim_val = 0;
            }
          }
          lastdown = down;
          break;
        default : break;
        }
    }
    //Serial.println(results.value, HEX);
    irrecv.resume();
  }
}

bool setLampState(int dim_val)
{
  bool ret;
  if (dim_val >= 6) ret = true;
  else ret = false;
  return ret;
}

void checkUp()

{
  up = digitalRead(upbotton);
  if (up != lastup)
  {
    if (up == LOW)
    {
      dim_val = dim_val + dim_valChange;
      if (dim_val > 100)
      {
        dim_val = 100;
      }
    }
    lastup = up;
  }
  //Blynk.virtualWrite(V4, dim_val); //sending to Blynk
}


void checkDown()
{
  down = digitalRead(downbotton);
  if (down != lastdown)
  {
    if (down == LOW)
    {
      dim_val = dim_val - dim_valChange;
      if (dim_val <= 45)
      {
        dim_val = 0;
      }

    }
    lastdown = down;

  }

  //Blynk.virtualWrite(V4, dim_val); //sending to Blynk
}

void setup()
{
  Serial.begin(115200);
  irrecv.enableIRIn();
  delay(100);
  pinMode(upbotton, INPUT);
  pinMode(downbotton, INPUT);

  dimmer.begin(NORMAL_MODE, ON);

}

void loop() {
  dim_val = map(outVal, 0, 100, 0, 100); // mapped the value for dimmer
  dimmer.setPower(dim_val); 
  ir_remote();
  checkUp();
  checkDown();
  dimmer.setPower(dim_val);
  delay(100);
}

you haven't described any problems

but here are some thoughts

  • shouldn't the buttons be configured as INPUT_PULLUP
  • since you want to control the level both with IR and buttons, have separate functions to in/decrease dim_val called from the IR and button routines instead of duplicating the code.
  • the IR code for in/decreasing incorrectly sets lastup and lastdown
  • there no need to call setPower() twice in loop()
  • shouldn't dim_val = map(...) be called just before setPower()?
  1. Your first point is very clear, I am using push button so next time I will use pull-up function

  2. For manually control these functions are tested in my previous projects where I control dimmer using push button with blynk slider

  3. Sorry for my mistake I using twice in void loop

  4. Actually I don't know how to control this module with remote

And dear sir if you understand my project can you correct my codes

dim_val = map(outVal, 0, 100, 0, 100);

i don't believe outVal has a value, resulting in dim_val always being zero

consider

#define MyHW
#ifdef MyHW
enum { ON, NORMAL_MODE };
class dimmerLamp {
    int  pin;

  public:
    dimmerLamp (int pin)  { };
    void begin (int a, int b)  { };
    void setPower (int a)  { Serial.println (a); };
};

# define upbotton   A1
# define downbotton A2

#else
# include <RBDdimmer.h>
# include <IRremote.h>

# define  RECV_PIN  3
IRrecv irrecv (RECV_PIN);

# define upbotton 8
# define downbotton 9
#endif


#define outputPin  5
#define zerocross  12

#define LAMPMAXVALUE 100

int lastup   = 0;
int lastdown = 0;
int up       = 0;
int down     = 0;
int dim_val  = 0;

const byte dim_valChange = 100 / 6;

dimmerLamp dimmer (outputPin);

// -----------------------------------------------------------------------------
#ifdef RECV_PIN
decode_results results;

void ir_remote () {
    if (irrecv.decode (&results)) {
        switch (results.value) {
            case 0xFF708F:
            {
                if (up == LOW)
                {
                    dim_val = dim_val + dim_valChange;
                    if (dim_val > 100)
                    {
                        dim_val = 100;
                    }
                }
                lastup = up;
                break;
            }
            case 0xFF58A7:
            {
                if (down == LOW)
                {
                    dim_val = dim_val - dim_valChange;
                    if (dim_val <= 45)
                    {
                        dim_val = 0;
                    }
                }
                lastdown = down;
                break;
                default : break;
            }
        }
        //Serial.println (results.value, HEX);
        irrecv.resume ();
    }
}
#endif

// -----------------------------------------------------------------------------
void checkUp ()
{
    up = digitalRead (upbotton);
    if (up != lastup)
    {
        if (up == LOW)
        {
            dim_val = dim_val + dim_valChange;
            if (dim_val > 100)
            {
                dim_val = 100;
            }
        }
        lastup = up;
    }
    //Blynk.virtualWrite (V4, dim_val); //sending to Blynk
}

void checkDown ()
{
    down = digitalRead (downbotton);
    if (down != lastdown)
    {
        if (down == LOW)
        {
            dim_val = dim_val - dim_valChange;
            if (dim_val <= 45)
            {
                dim_val = 0;
            }
        }
        lastdown = down;
    }
    //Blynk.virtualWrite (V4, dim_val); //sending to Blynk
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (115200);
#ifdef RECV_PIN
    irrecv.enableIRIn ();
#endif

    pinMode (upbotton,   INPUT_PULLUP);
    pinMode (downbotton, INPUT_PULLUP);

    dimmer.begin (NORMAL_MODE, ON);
}

// -----------------------------------------------------------------------------
void loop () {
#ifdef RECV_PIN
    ir_remote ();
#endif
    checkUp ();
    checkDown ();

    dimmer.setPower (dim_val);
    delay (100);
}

Not Working, in Serial Monitor result keep showing 0,0,0 ,,,,,,,,

sorry

change to

#undef MyHW

Hi @jamsyogendra

What type of motor on your fan?
Depending on the type of motor, you cannot vary the speed with dimmer.

RV mineirin

AC load and we can say AC Voltage

repost the code using </>

#undef MyHW
#ifdef MyHW
enum { ON, NORMAL_MODE };
class dimmerLamp {
    int  pin;

  public:
    dimmerLamp (int pin)  { };
    void begin (int a, int b)  { };
    void setPower (int a)  {
      Serial.println (a);
    };
};

# define upbotton   A1
# define downbotton A2

#else
# include <RBDdimmer.h>
# include <IRremote.h>

# define  RECV_PIN  3
IRrecv irrecv (RECV_PIN);

# define upbotton 8
# define downbotton 9
#endif


#define outputPin  5
#define zerocross  12

#define LAMPMAXVALUE 100

int lastup   = 0;
int lastdown = 0;
int up       = 0;
int down     = 0;
int dim_val  = 0;

const byte dim_valChange = 100 / 6;

dimmerLamp dimmer (outputPin);

// -----------------------------------------------------------------------------
#ifdef RECV_PIN
decode_results results;

void ir_remote () {
  if (irrecv.decode (&results)) {
    switch (results.value) {
      case 0xFF708F:
        {
          if (up == LOW)
          {
            dim_val = dim_val + dim_valChange;
            if (dim_val > 100)
            {
              dim_val = 100;
            }
          }
          lastup = up;
          break;
        }
      case 0xFF58A7:
        {
          if (down == LOW)
          {
            dim_val = dim_val - dim_valChange;
            if (dim_val <= 45)
            {
              dim_val = 0;
            }
          }
          lastdown = down;
          break;
        default : break;
        }
    }
    //Serial.println (results.value, HEX);
    irrecv.resume ();
  }
}
#endif

// -----------------------------------------------------------------------------
void checkUp ()
{
  up = digitalRead (upbotton);
  if (up != lastup)
  {
    if (up == LOW)
    {
      dim_val = dim_val + dim_valChange;
      if (dim_val > 100)
      {
        dim_val = 100;
      }
    }
    lastup = up;
  }
  //Blynk.virtualWrite (V4, dim_val); //sending to Blynk
}

void checkDown ()
{
  down = digitalRead (downbotton);
  if (down != lastdown)
  {
    if (down == LOW)
    {
      dim_val = dim_val - dim_valChange;
      if (dim_val <= 45)
      {
        dim_val = 0;
      }
    }
    lastdown = down;
  }
  //Blynk.virtualWrite (V4, dim_val); //sending to Blynk
}

// -----------------------------------------------------------------------------
void setup ()
{
  Serial.begin (115200);
#ifdef RECV_PIN
  irrecv.enableIRIn ();
#endif

  pinMode (upbotton,   INPUT_PULLUP);
  pinMode (downbotton, INPUT_PULLUP);

  dimmer.begin (NORMAL_MODE, ON);
}

// -----------------------------------------------------------------------------
void loop () {
#ifdef RECV_PIN
  ir_remote ();
#endif
  checkUp ();
  checkDown ();

  dimmer.setPower (dim_val);
  delay (100);
}

compiling this code with no error but nothing happen anything or changing

have you pressed the up button?

yes
after this on monitor starting showing keep zero

8:01:06.148 -> 0
18:01:06.248 -> 0
18:01:06.348 -> 0
18:01:06.439 -> 0
18:01:06.533 -> 0
18:01:06.617 -> 0
18:01:06.749 -> 0
18:01:06.849 -> 0
18:01:06.950 -> 0
18:01:07.049 -> 0
18:01:07.150 -> 0
18:01:07.250 -> 0
18:01:07.351 -> 0
18:01:07.451 -> 0
18:01:07.551 -> 0
18:01:07.652 -> 0
18:01:07.705 -> 0
18:01:07.852 -> 0

are your buttons working correctly? can you figure this out?

also not working my push buttons and my uno tx led keep blinking

don't know what to tell you.

suggest you start adding Serial.prints() to figure out what the code is doing

start with a print in setup() to make sure the code got loaded

ok I understand lets discuss tomorrow when i analyze all things.

but now lots of love and thanx for supporting me

Some AC motors cannot change their speed at all ,
induction motors and synchronous motors.
Some motors require different speed control techniques .

RV mineirin

i already changed the speed of my fan in prev project using nodemcu with blynk server