Memorise slider position

Hello,

I have two NodeMCU boards controlling 2 led strips in 2 different rooms. In order to save some space ajd to make it look cleaner in the Blynk app, instead of using 2 sets of sliders for controlling the colors, I used one set and the segmented switch widget, to control both led strips eith the sane sliders. One of them controls pins d1,d2,d3 while the other cintrols d5,d6,d7. In the segmented switch function, case 1, room=0 and case 2, room=1(0 for living room, 1 for bedroom). I’m using virtual pins for the sliders. What would be the easiest way to remember the sliders positions so when I change between rooms on the segmented switch widget, the sliders would go to the previous position?

As you can see, I choose one value for the living room an another fir the bedroom, the calue stays the same when I change back to living room, even tough the changes take effect only after touching the sliders

I would use virtual sync and virtual write in your segmented switch function.

Pseudo code:

living room -
r - v1
g - v2
b - v3

bedroom -
r - v4
g - v5
b - v6

slider values
r - v7
g - v8
b - v9

If segmented switch is set to living room

call virtual sync to get the value from the server for each r,g,b
use virtual write to set v7 to value of v1
use virtual write to set v8 to value of v2
use virtual write to set v9 to value of v3

If segmented switch is set to living room

call virtual sync to get the value from the server for each r,g,b
use virtual write to set v7 to value of v4
use virtual write to set v8 to value of v5
use virtual write to set v9 to value of v6

Edit: You many not need the virtual sync but it may be better to get the latest values?

I’ll post the code I’m using as soon as I get home. I have an idea about how to do it but I’d like to know if it’s prone to work as it’s kind of a hassle to get to the MCUs and upload the code. Also I’d like to specify that I use different codes for each mcu, None of the codes contain both led strips, they just work for ine led strip, I just changed the pins from d1,d2,d3 to d5,d6,d7, so nine of them contain all 6 digital pins, if that makes any sense

Not 100% sure what you meant but post your code and we can work out what needs to be changed :slight_smile:

@JustBertC the codes

Living Room:

  switch (param.asInt())
  {
    case 1: { // Living Room
        room=0;
        break;
      }
    case 2: { // Bedroom
        room=1;
        break;
      }
  }
}

BLYNK_WRITE(V17){
  rosu=param.asInt();
  if(room==0){
    analogWrite(red,rosu);
  }
}

BLYNK_WRITE(V15){
  verde=param.asInt();
  if(room==0){
    analogWrite(green,verde);
  }
}

BLYNK_WRITE(V16){
  albastru=param.asInt();
  if(room==0{
    analogWrite(blue,albastru);
  }
} 

Where:
red=d0
green=d1
blue=d3

Bedroom:

  switch (param.asInt())
  {
    case 1: { // Living Room
        room=0;
        break;
      }
    case 2: { // Bedroom
        room=1;
        break;
      }
  }
}

BLYNK_WRITE(V17){
  rosu=param.asInt();
  if(room==1){
    analogWrite(red,rosu);
  }
}

BLYNK_WRITE(V15){
  verde=param.asInt();
  if(room==1){
    analogWrite(green,verde);
  }
}

BLYNK_WRITE(V16){
  albastru=param.asInt();
  if(room==1){
    analogWrite(blue,albastru);
  }
} 

Where:
red=d5
green=d6
blue=d7

I tought about something like this:

  switch (param.asInt())
  {
    case 1: { // Living Room
        room=0;
       rosu=redmem;
       verde=greenmem;
       albastru=bluemem;
        break;
      }
    case 2: { // Bedroom
        room=1;
        break;
      }
  }
}

BLYNK_WRITE(V17){
  rosu=param.asInt();
  if(room==0){
    analogWrite(red,rosu);
redmem=rosu;
  }
}

BLYNK_WRITE(V15){
  verde=param.asInt();
  if(room==0){
    analogWrite(green,verde);
greenmem=verde;
  }
}

BLYNK_WRITE(V16){
  albastru=param.asInt();
  if(room==0){
    analogWrite(blue,albastru);
bluemem=albastru;
  }
} 

Where the variables greenmem,redmem,bluemem are the stored values/last values used in the selected case. Would this work?

You could do something like this: ?

( I changed your room numbers so they match the segemented switch for ease :wink:

Living room:

int r;
int g;
int b;

BLYNK_WRITE(Vx) //segmented switch
{
  room = param.asInt();
  switch (room)
  {
    case 1:
      Blynk.virtualWrite(V17, r);
      Blynk.virtualWrite(V15, g);
      Blynk.virtualWrite(V16, b);
      break;
    case 2:
      
      break;
  }
}

BLYNK_WRITE(V17)
{
  if (room == 1) {
    r = param.asInt();
    analogWrite(red, r);
  }
}

BLYNK_WRITE(V15)
{
  if (room == 1) {
    g = param.asInt();
    analogWrite(green, g);
  }
}

BLYNK_WRITE(V16)
{
  if (room == 1) {
    b = param.asInt();
    analogWrite(blue, b);
  }
}

Bedroom:

int r;
int g;
int b;

BLYNK_WRITE(Vx) //segmented switch
{
  room = param.asInt();
  switch (room)
  {
    case 1:

      break;
    case 2:
      Blynk.virtualWrite(V17, r);
      Blynk.virtualWrite(V15, g);
      Blynk.virtualWrite(V16, b);
      break;
  }
}

BLYNK_WRITE(V17)
{
  if (room == 2) {
    r = param.asInt();
    analogWrite(red, r);
  }
}

BLYNK_WRITE(V15)
{
  if (room == 2) {
    g = param.asInt();
    analogWrite(green, g);
  }
}

BLYNK_WRITE(V16)
{
  if (room == 2) {
    b = param.asInt();
    analogWrite(blue, b);
  }
}

@JustBertC in that case, would I still need the ‘room’ variable? As I’ve seen you haven’t atributed any value to ‘room’ in the segmented switch function

//or does the variable receive the 1 and 2 values as in ‘case 1’ and ‘case 2’?

@dragos
Yes you would still need room as a variable - I set it here in the segmented switch function:

BLYNK_WRITE(Vx) //segmented switch
{
  room = param.asInt(); // <<< room variable set here, either 1 or 2 depending on switch position
  switch (room)
  {
    case 1:

      break;
    case 2:
      Blynk.virtualWrite(V17, r);
      Blynk.virtualWrite(V15, g);
      Blynk.virtualWrite(V16, b);
      break;
  }
}

@JustBertC I was not sure how it worked but I understand now, thank you. I will try making the changes and post the feedback asap

1 Like

@JustBertC works just fine. Thanks:)

1 Like

If it’s a pain to upload new code use OTA. I just started a while ago and I’m sold! The fun of IOT is changing code afterall👍🏼

2 Likes

I second that!

I was put off a bit at first and had some teething issues with ports etc and python but the ArduinoOTA library is fantastic to use! (And easy too!)

1 Like

I agree, especially if you give your OTA devices friendly names.

Pete.

2 Likes

I’ve read more about OTA, seems like it is a nice thing to have as most of my circuits are mounted in cases that are hidden, and even tough they are powered by usb which would make it easy to upload the code, I use usb cables that don’t have data transfer… so each time I want to add a new code I ned to open up the case and so on.

Could I get more details about this process? How easy is it to implement to an already existing code? @PeteKnight if I’m not mistaken, I’ve read through one of your posts where you said there were about 3-4 lines of code (without using any serial print)

Start by reading this:
https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html#arduino-ide

Install Python as described in the link, and use the example sketch.

Pete.

@dragos Yes there isn’t much to it. As you have said, no more than 10 lines (depending what you want to customise)

The biggest pain for me was python (well not technically python but my antivirus that closed all the ports to python - created an exception and it’s all wonderful now)

Read the link that @PeteKnight posted, that’s all I did - didn’t need to search anywhere else.

@dragos Personally I was a little confused by some of the code in that article. However the python setup worked as per the post. I will try to post my code here in a day or two.

So I’ve managed to make it work with the basicOTA sketch, I’m just not sure how to implement it to an already existing code. I’m currently trying this code and it seems to work:

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

const char* ssid = "--";
const char* password = "--";

void setup() {

    ArduinoOTA.onError([](ota_error_t error) { ESP.restart(); });
  ArduinoOTA.setHostname("NodeMCU Test");
  ArduinoOTA.begin();
}

void loop() {
  ArduinoOTA.handle();
}

This is what I start with as a basic sketch for use with Blynk (using Wemos D1 mini):

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h> // <<< ADD THIS

#include <WiFiUdp.h> // <<< ADD THIS
#include <ArduinoOTA.h> // <<< ADD THIS

char auth[] = "xxxxx";
char ssid[] = "xxxxx";
char pass[] = "xxxxx";

void setup() {
  WiFi.begin(ssid, pass); 
  Blynk.config(auth);
  Blynk.connect();
  
  ArduinoOTA.setHostname("Wemos D1 mini"); // <<< ADD THIS
  ArduinoOTA.begin(); // <<< ADD THIS
}

void loop() {
  Blynk.run();
  ArduinoOTA.handle(); // <<< ADD THIS
}

@JustBertC I did something simillar, works great!

image

3 Likes