Synchronization of a slider with a potentiometer in an ac dimmer

Hello everyone

Sorry for the bad English (Translated from Google)
I’m doing a degree project in my house that consists of home automation, I decided to work with blynk because it is a very useful tool, but I’m stuck in one last step to start installing the whole system.

I am using an Arduino Mega 2560 with an Ethernet Shield, for the regulation of the lights I have two dimmers ac bought from this site and I work perfectly with blynk on the cell phone, but I need to be able to adjust the lights from a potentiometer next to the app, I have a program that I have joined from different sites but it does not work the way I need, when I leave the potentiometer at zero, I can regulate the app, but when I increase the potentiometer, when I start to vary in the app it works and after a while it returns to the state that the potentiometer has.

My question is, is there any way to solve this, or could I use switches to shut down physically?

Thank you very much.

you should post your current code, than maybe someone can take a look, to find out what is the problem. without that, no one can help.

Posting the code would certainly help.
However, if you’re actually using a potentiometer (variable resistor) connected to an analogue pin of your Arduino then that’s probably not the best solution in this scenario.
A potentiometer that rotates through around 300 degrees will represent (say) zero brightness when fully anti-clockwise and full brightness when fully clockwise and medium brightness when somewhere in the middle. If it’s set to maximum brightness, then you use Blynk to reduce the brightness, the potentiometer will stay in the same position then you won’t have any more clockwise rotation available to increase the brightness.
The expensive solution is a motorised/servo potentiometer that rotates automatically when the brightness is changed in Blynk.
The simpler solution is a digital potentiometer linked to a rotary encoder (that can rotate endlessly).

Pete.

1 Like

i would say even a rotary encoder alone would be sufficient, no need for (digital) pot. you can save the values of each channel to eeprom, thus you will have the same settings on power loss / restart.

@Adalberto95 , if you take a look to the guy’s products on ebay, he has a much better version for the 4ch dimmer, which uses i2c communication and has dedicated mcu to run that awkward code on it - you just set once the values for each channel on the esp.

1 Like

May not the best solution by far, but he could track what value was changed last (physical vs digital) and use that value to set brightness then they could work independently if each other, and have a push button on blynk app to force the use of the value at the potentiometer

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include  <TimerOne.h>          // Avaiable from http://www.arduino.cc/playground/Code/Timer1
volatile int i=0;               // Variable to use as a counter volatile as it is in an interrupt
volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 3;                // Output to Opto Triac
int readValue;  // Use this variable to read Potentiometer
int writeValue; // Use this variable for writing to LED
int potValue ;
int brightness = 120;           // Dimming level (0-128)  0 = on, 128 = 0ff
int freqStep = 65;    // This is the delay-per-brightness step in microseconds.
const int analogPin = A0;
int value;


char auth[] = "d6ea45b6eb3d4f3d85ab3c09ad510f78";

#define W5100_CS  10
#define SDCARD_CS 4


void setup()
{
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
  attachInterrupt(0, zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) 
  Timer1.initialize(freqStep);                      // Initialize TimerOne library 
  Timer1.attachInterrupt(dim_check, freqStep);   
  Serial.begin(9600);
  Blynk.begin(auth);
}

BLYNK_WRITE(V1)//      slider brillo
{
 int brillo = param.asInt(); 
brightness=brillo;
}

void zero_cross_detect() {    
  zero_cross = true;               
  i=0;
  digitalWrite(AC_pin, LOW);       // turn off TRIAC (and AC)
}
// Turn on the TRIAC at the appropriate time
void dim_check() {
                 
  if(zero_cross == true) { 
              
    if(i>=brightness) {                     
      digitalWrite(AC_pin, HIGH); // turn on light       
      i=0;  // reset time step counter                         
      zero_cross = false; //reset zero cross detection
    } 
    else {
      i++; // increment time step counter                     
    }                                
  }                                  
}                               

void Potentiometer() // Potentiometer to led
{
  readValue = analogRead(analogPin);
  if(potValue != readValue){
    brightness = (120./1023.) * readValue; //Calculate Write Value for LED
    potValue = analogRead(analogPin);
    Blynk.virtualWrite(V1, brightness); //Send feedback to slider Widget
  }
  delay(100);
}

void loop()
{
  Blynk.run();
  Potentiometer();
    //value = analogRead(analogPin);
    //brightness = map(value, 0, 1023, 0, 120);
}

This is the code that I have used, I have united it from different places and the truth without the potentiometer works perfectly for me, but I need that physical modulation.

You are absolutely right with the analog potentiometer, however I thought I could work with it, since it works for me one step and the other does not, I am struck by the idea of the digital potentiometer, in that case it could vary without problems?

Thank you very much for your answer.

Very interesting the rotary encoder, I have been investigating it and I would think that it is the solution to my problem, I will get one and I will do a test, thank you very much, and regarding the guy’s products on ebay, I had not really seen them, but It cost me too much to get what I have, so I must work with these, however, I would like to set up my own home automation company, and that product is an improvement.

I would believe that if possible, but I have not found the solution to this problem.

Your code would need to be modified, but as it doesn’t work anyway then maybe you should work on that rather than your existing code.
Actually, the idea from @wanek of just using a rotary encoder is probably the easiest solution. You could also use two momentary physical buttons in place of the rotary encoder - one for brighter and the other for dimmer. Connect them to digital input pins an increment the brightness by say 10% per press of the button.

Pete.

1 Like

I would think it’s the best idea while I get the rotary encoder, I’m going to implement that and let them know how it worked, thank you very much for your answers.

the rotary encoders are dirt cheap, and most of them have a built in push button. you can use that to step between the channels: i.e. when you turn the encoder it increments / decrements the value of the dimmer on ch1, and if you push the button, it steps to ch2, etc. (there are plenty of quality code for encoders on the net)

you can use 4 leds - one for each channel, which represent the active channel selected by the encoder. thus, with a single encoder you can command as many channels as you want.

also, the encoder won’t interfere with the blynk sliders, unlike the pot. you can assign a variable for each slider, and change the values via the app sliders or encoder, it doesn’t matters.

1 Like

Very interesting, I found the solution to my problem thanks to you guys, I need your help in another case, how could I do it? I have to create another theme, or can I do it? It’s similar.

…if you mind, share with the community, it could be useful for someone in the future…

if it is related to this topic, continue here, if not, create a new topic, based on this guidelines: