Anyone can help me in my code related to fan speed control

Hello guys I am working on a project, which is 4 relays with 1 fan speed control(live feedback also)

I want to add one more condition in my project, related to fan speed control,

first is one more button for a fan on/off and one regulator switch for manually control of fan speed can someone help me,

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <RBDdimmer.h>           
BlynkTimer timer;

#define DEBUG_SW 0

#define S1 34
#define R1 18

#define S2 35
#define R2 19

#define S3 36
#define R3 21

#define S4 39
#define R4 22

#define outputPin  27  
#define zerocross  25 

dimmerLamp dimmer(outputPin, zerocross); 

int MODE = 0;

int outVal = 0; 
int dim_val; 

char ssid[] = "Yogendra";
char pass[] = "Jamsariya";

char auth[] = ".......................";  //blynk app auth 


int switch_ON_Flag1_previous_I = 0;
int switch_ON_Flag2_previous_I = 0;
int switch_ON_Flag3_previous_I = 0;
int switch_ON_Flag4_previous_I = 0;

BLYNK_WRITE(V0)  {  
  outVal = param.asInt(); 
  dim_val = map(outVal, 0, 1023, 0, 100); 
  dimmer.setPower(dim_val);               
  //Blynk.virtualWrite(V1, dim_val); 
}

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); 
  digitalWrite(R1, pinValue);
}

BLYNK_WRITE(V2)
{
  int pinValue = param.asInt(); 
  digitalWrite(R2, pinValue);
}

BLYNK_WRITE(V3)
{
  int pinValue = param.asInt(); 
  digitalWrite(R3, pinValue);
}

BLYNK_WRITE(V4)
{
  int pinValue = param.asInt(); 
  digitalWrite(R4, pinValue);
}

void with_internet()
{
  if (digitalRead(S1) == LOW)
  {
    if (switch_ON_Flag1_previous_I == 0 )
    {
      digitalWrite(R1, LOW);
      if (DEBUG_SW) Serial.println("Relay1- ON");
      Blynk.virtualWrite(V1, 0);
      switch_ON_Flag1_previous_I = 1;
    }
    if (DEBUG_SW) Serial.println("Switch1 -ON");

  }
  if (digitalRead(S1) == HIGH )
  {
    if (switch_ON_Flag1_previous_I == 1)
    {
      digitalWrite(R1, HIGH);
      if (DEBUG_SW) Serial.println("Relay1 OFF");
      Blynk.virtualWrite(V1, 1);
      switch_ON_Flag1_previous_I = 0;
    }
    if (DEBUG_SW)Serial.println("Switch1 OFF");
  }


  if (digitalRead(S2) == LOW)
  {
    if (switch_ON_Flag2_previous_I == 0 )
    {
      digitalWrite(R2, LOW);
      if (DEBUG_SW)  Serial.println("Relay2- ON");
      Blynk.virtualWrite(V2, 0);
      switch_ON_Flag2_previous_I = 1;
    }
    if (DEBUG_SW) Serial.println("Switch2 -ON");

  }
  if (digitalRead(S2) == HIGH )
  {
    if (switch_ON_Flag2_previous_I == 1)
    {
      digitalWrite(R2, HIGH);
      if (DEBUG_SW) Serial.println("Relay2 OFF");
      Blynk.virtualWrite(V2, 1);
      switch_ON_Flag2_previous_I = 0;
    }
    if (DEBUG_SW)Serial.println("Switch2 OFF");
    //delay(200);
  }

  if (digitalRead(S3) == LOW)
  {
    if (switch_ON_Flag3_previous_I == 0 )
    {
      digitalWrite(R3, LOW);
      if (DEBUG_SW) Serial.println("Relay3- ON");
      Blynk.virtualWrite(V3, 0);
      switch_ON_Flag3_previous_I = 1;
    }
    if (DEBUG_SW) Serial.println("Switch3 -ON");

  }
  if (digitalRead(S3) == HIGH )
  {
    if (switch_ON_Flag3_previous_I == 1)
    {
      digitalWrite(R3, HIGH);
      if (DEBUG_SW) Serial.println("Relay3 OFF");
      Blynk.virtualWrite(V3, 1);
      switch_ON_Flag3_previous_I = 0;
    }
    if (DEBUG_SW)Serial.println("Switch3 OFF");
    //delay(200);
  }

  if (digitalRead(S4) == LOW)
  {
    if (switch_ON_Flag4_previous_I == 0 )
    {
      digitalWrite(R4, LOW);
      if (DEBUG_SW) Serial.println("Relay4- ON");
      Blynk.virtualWrite(V4, 0);
      switch_ON_Flag4_previous_I = 1;
    }
    if (DEBUG_SW) Serial.println("Switch4 -ON");

  }
  if (digitalRead(S4) == HIGH )
  {
    if (switch_ON_Flag4_previous_I == 1)
    {
      digitalWrite(R4, HIGH);
      if (DEBUG_SW) Serial.println("Relay4 OFF");
      Blynk.virtualWrite(V4, 1);
      switch_ON_Flag4_previous_I = 0;
    }
    if (DEBUG_SW)Serial.println("Switch4 OFF");
    //delay(200);
  }

}

void without_internet()
{

  digitalWrite(R1, digitalRead(S1));
  digitalWrite(R2, digitalRead(S2));
  digitalWrite(R3, digitalRead(S3));
  digitalWrite(R4, digitalRead(S4));

}


void checkBlynk() { // called every 3 seconds by SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false) {
    MODE = 1;
  }
  if (isconnected == true) {
    MODE = 0;
  }
}

void setup()
{
  // Debug console
  if (DEBUG_SW) Serial.begin(9600);
  
  pinMode(S1, INPUT);
  pinMode(R1, OUTPUT);

  pinMode(S2, INPUT);
  pinMode(R2, OUTPUT);

  pinMode(S3, INPUT);
  pinMode(R3, OUTPUT);

  pinMode(S4, INPUT);
  pinMode(R4, OUTPUT);

  WiFi.begin(ssid, pass);
  timer.setInterval(3000L, checkBlynk); // check if connected to Blynk server every 3 seconds
  Blynk.config(auth);//, ssid, pass);
  dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)
}

void loop()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    if (DEBUG_SW) Serial.println("Not Connected");
  }
  else
  {
    if (DEBUG_SW) Serial.println(" Connected");
    Blynk.run();
  }

  timer.run(); // Initiates SimpleTimer
  if (MODE == 0)
    with_internet();
  else
    without_internet();
}

Yes, many here can help you. But why do you need help? You got this far, what is stopping you from making the changes?

after so many changes , i don’t get results that i wants.

So you have given up and now you want someone else to do if l it for you? You will not learn much that way.

Post your best attempt and explain what happens when it runs.

after 3 years, this is my first question here , I don’t post questions here, I always try to do solve myself,

but this is a little bit complex bcz of rbdimmer lib,

and i think this is the last according to my experiences, dear sir , if you can , please

So the above code contains your attempt to add the new controls? Which are they? Some comments in your code might help. What happens when the code runs? How is it wrong?

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <RBDdimmer.h>           
BlynkTimer timer;


#define DEBUG_SW 0


#define S1 34
#define R1 18

#define S2 35
#define R2 19

#define S3 36
#define R3 21

#define S4 39
#define R4 22

#define outputPin  27  // PWM pin for dimmer
#define zerocross  25 // for boards with CHANGEBLE input pins

dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards


//#define LED1 26
//#define LED2 25
//#define LED3 27
//#define Buzzer 21



int MODE = 0;

int outVal = 0; // Intialisation value for dimmer
int dim_val; // dimmer value to dim the appliance


// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Yogendra";
char pass[] = "Jamsariya";

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "vZiJt_9YbN3KX2vlrankQFraW0LtGq9q";


int switch_ON_Flag1_previous_I = 0;
int switch_ON_Flag2_previous_I = 0;
int switch_ON_Flag3_previous_I = 0;
int switch_ON_Flag4_previous_I = 0;

BLYNK_WRITE(V0)  {  // Set to your virtual pin  
  outVal = param.asInt(); // Get State of Virtual Button
  dim_val = map(outVal, 0, 1023, 0, 100); // mapped the value for dimmer
  dimmer.setPower(dim_val);               // Set dimmer power
  //Blynk.virtualWrite(V1, dim_val); //sending to Blynk
}

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  digitalWrite(R1, pinValue);
  // process received value
}

BLYNK_WRITE(V2)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V2 to a variable
  digitalWrite(R2, pinValue);
  // process received value
}

BLYNK_WRITE(V3)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
  digitalWrite(R3, pinValue);
  // process received value
}

BLYNK_WRITE(V4)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V4 to a variable
  digitalWrite(R4, pinValue);
  // process received value
}

void with_internet()
{
  if (digitalRead(S1) == LOW)
  {
    if (switch_ON_Flag1_previous_I == 0 )
    {
      digitalWrite(R1, LOW);
      if (DEBUG_SW) Serial.println("Relay1- ON");
      Blynk.virtualWrite(V1, 0);
      switch_ON_Flag1_previous_I = 1;
    }
    if (DEBUG_SW) Serial.println("Switch1 -ON");

  }
  if (digitalRead(S1) == HIGH )
  {
    if (switch_ON_Flag1_previous_I == 1)
    {
      digitalWrite(R1, HIGH);
      if (DEBUG_SW) Serial.println("Relay1 OFF");
      Blynk.virtualWrite(V1, 1);
      switch_ON_Flag1_previous_I = 0;
    }
    if (DEBUG_SW)Serial.println("Switch1 OFF");
  }


  if (digitalRead(S2) == LOW)
  {
    if (switch_ON_Flag2_previous_I == 0 )
    {
      digitalWrite(R2, LOW);
      if (DEBUG_SW)  Serial.println("Relay2- ON");
      Blynk.virtualWrite(V2, 0);
      switch_ON_Flag2_previous_I = 1;
    }
    if (DEBUG_SW) Serial.println("Switch2 -ON");

  }
  if (digitalRead(S2) == HIGH )
  {
    if (switch_ON_Flag2_previous_I == 1)
    {
      digitalWrite(R2, HIGH);
      if (DEBUG_SW) Serial.println("Relay2 OFF");
      Blynk.virtualWrite(V2, 1);
      switch_ON_Flag2_previous_I = 0;
    }
    if (DEBUG_SW)Serial.println("Switch2 OFF");
    //delay(200);
  }

  if (digitalRead(S3) == LOW)
  {
    if (switch_ON_Flag3_previous_I == 0 )
    {
      digitalWrite(R3, LOW);
      if (DEBUG_SW) Serial.println("Relay3- ON");
      Blynk.virtualWrite(V3, 0);
      switch_ON_Flag3_previous_I = 1;
    }
    if (DEBUG_SW) Serial.println("Switch3 -ON");

  }
  if (digitalRead(S3) == HIGH )
  {
    if (switch_ON_Flag3_previous_I == 1)
    {
      digitalWrite(R3, HIGH);
      if (DEBUG_SW) Serial.println("Relay3 OFF");
      Blynk.virtualWrite(V3, 1);
      switch_ON_Flag3_previous_I = 0;
    }
    if (DEBUG_SW)Serial.println("Switch3 OFF");
    //delay(200);
  }

  if (digitalRead(S4) == LOW)
  {
    if (switch_ON_Flag4_previous_I == 0 )
    {
      digitalWrite(R4, LOW);
      if (DEBUG_SW) Serial.println("Relay4- ON");
      Blynk.virtualWrite(V4, 0);
      switch_ON_Flag4_previous_I = 1;
    }
    if (DEBUG_SW) Serial.println("Switch4 -ON");

  }
  if (digitalRead(S4) == HIGH )
  {
    if (switch_ON_Flag4_previous_I == 1)
    {
      digitalWrite(R4, HIGH);
      if (DEBUG_SW) Serial.println("Relay4 OFF");
      Blynk.virtualWrite(V4, 1);
      switch_ON_Flag4_previous_I = 0;
    }
    if (DEBUG_SW)Serial.println("Switch4 OFF");
    //delay(200);
  }

}

void without_internet()
{

  digitalWrite(R1, digitalRead(S1));
  digitalWrite(R2, digitalRead(S2));
  digitalWrite(R3, digitalRead(S3));
  digitalWrite(R4, digitalRead(S4));

}


void checkBlynk() { // called every 3 seconds by SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false) {
    MODE = 1;
   
  }
  if (isconnected == true) {
    MODE = 0;
   
  }
}

void setup()
{
  // Debug console
  if (DEBUG_SW) Serial.begin(9600);
  
  pinMode(S1, INPUT);
  pinMode(R1, OUTPUT);

  pinMode(S2, INPUT);
  pinMode(R2, OUTPUT);

  pinMode(S3, INPUT);
  pinMode(R3, OUTPUT);

  pinMode(S4, INPUT);
  pinMode(R4, OUTPUT);

  WiFi.begin(ssid, pass);
  timer.setInterval(3000L, checkBlynk); // check if connected to Blynk server every 3 seconds
  Blynk.config(auth);//, ssid, pass);
  dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)

}

void loop()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    if (DEBUG_SW) Serial.println("Not Connected");
  }
  else
  {
    if (DEBUG_SW) Serial.println(" Connected");
    Blynk.run();
  }

  timer.run(); // Initiates SimpleTimer
  if (MODE == 0)
    with_internet();
  else
    without_internet();
}

so the current situation of my project I successfully control 4 channel relays by a mobile app(blynk) and also with fan by mobile this is the first part of my project

if we talk about the second part of my project
i also get live feedback with or we can say live status of manual switch in app

but problem with fan dimmer, i am control dimmer by mobile which is done,
but i also want to control fan dimmer using potainsialmeter with live states

so anything idea how can i? do

Do you mean potentiometer?

I don’t see how a potentiometer can work. If you change the dimmer control on the phone, the pot will not update its position.

Perhaps a rotary encoder could work. Then, if the encoder is adjusted, the dimmer setting on the phone app can be updated. If the dimmer setting on the phone app is updated, there is no need to update the encoder position.

I think you are write so can we use 2 push button instead of pot,

Yeah, I think that would work also.

So if the new up button is pressed, you could change the dimming level and update the Blynk slider value with:

if (dim_val < 100) dim_val++;
dimmer.setPower(dim_val);
outVal = map(dim_val, 0, 100, 0, 1023);
Blynk.virtualWrite(V1, outVal);

whole codes will change :roll_eyes:

Why?

add 2 more buttons one for up (speed) and one for down(speed}
then void setup, then again loop for these buttons, this will more complecated.

More complicated than using a pot? What if it is? We already agreed that a pot would not work.

Stop begging. Start trying. Then I will help further.

1 Like

Ok see you tomorrow with new ways

Hi PaulRB,
I try this after this how to add function for up Speed and down speed

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <RBDdimmer.h> 

BlynkTimer timer;

void checkPhysicalButton();

int relay1State = LOW;
int pushButton1State = HIGH;

int relay2State = LOW;
int pushButton2State = HIGH;

int relay3State = LOW;
int pushButton3State = HIGH;

int relay4State = LOW;
int pushButton4State = HIGH;

int relay5State = LOW;
int pushButton5State = HIGH;

#define AUTH "---------------------------"    
#define WIFI_SSID "------"                   
#define WIFI_PASS "------"                   

#define SERVER "blynk-cloud.com "             
#define PORT 8442

#define RELAY_PIN_1      12   
#define RELAY_PIN_2      16   
#define RELAY_PIN_3       4   
#define RELAY_PIN_4       5
#define RELAY_PIN_5      12   

#define PUSH_BUTTON_1     2   
#define PUSH_BUTTON_2    14   
#define PUSH_BUTTON_3    13   
#define PUSH_BUTTON_4     1
#define PUSH_BUTTON_5     2 //for dimmer off 
#define PUSH_BUTTON_6     2 //for speed up
#define PUSH_BUTTON_7     2 //for speed up

#define VPIN_BUTTON_1    V1 //relay1
#define VPIN_BUTTON_2    V2 //relay2
#define VPIN_BUTTON_3    V3 //relay3
#define VPIN_BUTTON_4    V4 //relay4
#define VPIN_BUTTON_5    V5 //dimmer on/off

#define outputPin  27  // PWM pin for dimmer
#define zerocross  25 // for boards with CHANGEBLE input pins

dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards

int outVal = 0; // Intialisation value for dimmer
int dim_val; // dimmer value to dim the appliance

BLYNK_CONNECTED() {

Blynk.syncVirtual(VPIN_BUTTON_1); // Request the latest state from the server
Blynk.syncVirtual(VPIN_BUTTON_2);
Blynk.syncVirtual(VPIN_BUTTON_3);
Blynk.syncVirtual(VPIN_BUTTON_4);
Blynk.syncVirtual(VPIN_BUTTON_5); //dimmer on of

}

BLYNK_WRITE(V0)  {  // Set to your virtual pin  
  outVal = param.asInt(); // Get State of Virtual Button
  dim_val = map(outVal, 0, 1023, 0, 100); // mapped the value for dimmer
  dimmer.setPower(dim_val);               // Set dimmer power
  //Blynk.virtualWrite(V1, dim_val); //sending to Blynk
}

BLYNK_WRITE(VPIN_BUTTON_1) {    // When App button is pushed - switch the state 
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
}

BLYNK_WRITE(VPIN_BUTTON_2) {
  relay2State = param.asInt();
  digitalWrite(RELAY_PIN_2, relay2State);
}
BLYNK_WRITE(VPIN_BUTTON_3) {
  relay3State = param.asInt();
  digitalWrite(RELAY_PIN_3, relay3State);
}
BLYNK_WRITE(VPIN_BUTTON_4) {
  relay4State = param.asInt();
  digitalWrite(RELAY_PIN_4, relay4State);
}

BLYNK_WRITE(VPIN_BUTTON_5) {
  relay5State = param.asInt();
  digitalWrite(RELAY_PIN_5, relay5State);
}



void checkPhysicalButton()
{
  if (digitalRead(PUSH_BUTTON_1) == LOW) {
    // pushButton1State is used to avoid sequential toggles
    if (pushButton1State != LOW) {

      // Toggle Relay state
      relay1State = !relay1State;
      digitalWrite(RELAY_PIN_1, relay1State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
    }
    pushButton1State = LOW;
  } else {
    pushButton1State = HIGH;
  }

  if (digitalRead(PUSH_BUTTON_2) == LOW) {
    // pushButton2State is used to avoid sequential toggles
    if (pushButton2State != LOW) {

      // Toggle Relay state
      relay2State = !relay2State;
      digitalWrite(RELAY_PIN_2, relay2State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
    }
    pushButton2State = LOW;
  } else {
    pushButton2State = HIGH;
  }

  if (digitalRead(PUSH_BUTTON_3) == LOW) {
    // pushButton3State is used to avoid sequential toggles
    if (pushButton3State != LOW) {

      // Toggle Relay state
      relay3State = !relay3State;
      digitalWrite(RELAY_PIN_3, relay3State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
    }
    pushButton3State = LOW;
  } else {
    pushButton3State = HIGH;
  }

  if (digitalRead(PUSH_BUTTON_4) == LOW) {
    // pushButton4State is used to avoid sequential toggles
    if (pushButton4State != LOW) {

      // Toggle Relay state
      relay4State = !relay4State;
      digitalWrite(RELAY_PIN_4, relay4State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
    }
    pushButton4State = LOW;
  } else {
    pushButton4State = HIGH;
  }
  if (digitalRead(PUSH_BUTTON_5) == LOW) {
    // pushButton4State is used to avoid sequential toggles
    if (pushButton5State != LOW) {

      // Toggle Relay state
      relay5State = !relay5State;
      digitalWrite(RELAY_PIN_5, relay5State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_5, relay5State);
    }
    pushButton5State = LOW;
  } else {
    pushButton5State = HIGH;
  }
}

void setup()
{

  Serial.begin(115200);
  Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS,"blynk-cloud.com", 8442);
  dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)
  dimmer.setPower(50);

  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_1, relay1State);


  pinMode(RELAY_PIN_2, OUTPUT);
  pinMode(PUSH_BUTTON_2, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_2, relay2State);


  pinMode(RELAY_PIN_3, OUTPUT);
  pinMode(PUSH_BUTTON_3, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_3, relay3State);


  pinMode(RELAY_PIN_4, OUTPUT);
  pinMode(PUSH_BUTTON_4, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_4, relay4State);
  
  pinMode(RELAY_PIN_5, OUTPUT);
  pinMode(PUSH_BUTTON_5, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_5, relay5State);

  timer.setInterval(500L, checkPhysicalButton); //Setup a function to be called every 100 ms
  
}

void loop()
{
  Blynk.run();
  timer.run();
}

I am not seeing your attempt to add code for changing the speed. I see new code for buttons to change relays, but nothing for changing speed. Please add your attempt to add buttons for changing speed.

ok but the problem is i don’t know how to add button for changing speed in this manner, i can only on/ off