Syncing a Segmented Switch with a physical button on my breadboard

I was over complicating things. I needed the if conditions to be in a loop for when the switch changed. So I created the whatToDo function and the second timer (I didn’t know you could put multiple things on one timer. Thats good future knowledge). I completely forgot I had my void loop I could just put these if statements in. This works they way I need it to, and it is less complicated.

Your talking about putting the switchCase = 1 and so on for each case, but this is already happening with this part of my code:

BLYNK_WRITE(V1) {
  switchState = param.asInt();
  switch (param.asInt())
  {

Setting it equal to the param.asInt() does this without having to put it in each case.

Here the full code with the if conditions in the void loop:

#define BLYNK_PRINT Serial


#include <SoftwareSerial.h>
SoftwareSerial SerialBLE(10, 11); // RX, TX

#include <BlynkSimpleSerialBLE.h>


BlynkTimer timer1;
void checkPhysicalButton();


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

const int offMode = 4;
const int onMode = 5;
const int runMode = 6;
const int startMode = 7;

const int buttonPin = 2;
int buttonState = 0;
int switchState;

  


BLYNK_WRITE(V1) {
  switchState = param.asInt();
  switch (param.asInt())
  {
    case 1: // Item 1
      Serial.println("Off selected");
      break;     
    case 2: // Item 2
      Serial.println("On selected");
      break;
    case 3: // Item 3
      Serial.println("Run selected");
      break;
    case 4: // Item 4
      Serial.println("Start selected");
      break;
    default:
      Serial.println("Unknown item selected");
  }
}


void checkPhysicalButton()
{
  buttonState = digitalRead(buttonPin);
  
  if(buttonState == HIGH)
  {
    ++switchState;           //add to your button status every press

    if(switchState > 4)
    {
      switchState = 1;   //resets the counter if it is larger than the number of button segments
    }
    Blynk.virtualWrite(V1,switchState);
    Blynk.syncVirtual(V1);                //try this not sure if it will trigger the switch case in the Vbutton fucnction or not
   }
}



void setup()
{
  // Debug console
  Serial.begin(9600);

  SerialBLE.begin(9600);
  Blynk.begin(SerialBLE, auth);

  pinMode(offMode, OUTPUT);
  pinMode(onMode, OUTPUT);
  pinMode(runMode, OUTPUT);
  pinMode(startMode, OUTPUT);

  pinMode(buttonPin, INPUT);

  timer1.setInterval(200L, checkPhysicalButton);
  

}

void loop()
{
  
  Blynk.run();
  timer1.run();


  if (switchState == 1){
      Serial.println("Off selected");
      digitalWrite(offMode, HIGH);
      digitalWrite(onMode, LOW);
      digitalWrite(runMode, LOW);
      digitalWrite(startMode, LOW);
  }
       
  if (switchState == 2){
      Serial.println("On selected");
      digitalWrite(offMode, LOW);
      digitalWrite(onMode, HIGH);
      digitalWrite(runMode, LOW);
      digitalWrite(startMode, LOW);
  }
      
  if (switchState == 3){
      Serial.println("Run selected");
      digitalWrite(offMode, LOW);
      digitalWrite(onMode, LOW);
      digitalWrite(runMode, HIGH);
      digitalWrite(startMode, LOW);
  }
      
  if (switchState == 4){
      Serial.println("Start selected");
      digitalWrite(offMode, LOW);
      digitalWrite(onMode, LOW);
      digitalWrite(runMode, LOW);
      digitalWrite(startMode, HIGH);
  }

}

Sorry but the other way is better. Good programming practice in the long run is to have almost nothing in the loop. Also think it through… the loop runs hundreds of times a second or should, every loop switch state has a value. It will be writing to the digital pins and a digital write steals time. The previous method just called the do something loop ONCE when there was a change. You are right on the updating switch state but the other pointers still stand.

Yes, as Dave says, that isn’t the way to go.
Read this:

Pete.

from my experience bluetooth is not very reliable with blynk

I totally agree. I’d be very happy to see Bluetooth connectivity dropped from future releases of Blynk. I don’t think it has a real place in an IoT system.

But, I don’t think that the use of BLE is the cause of the problem here.

Pete.

ha ha. I agree with you totally…you are the man Pete, merry Christmas to you

1 Like