BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Reset Blynk Slider Widget to zero

#1

Arduino Nano, iOs, BLE-HM10

I have been trying to figure out how to reset a Blynk slider switch to zero when a segmented switch is pressed. This is a controller for an RC car. I have a 3 button segmented switch to change direction and stop. There is a slider switch to change the motor speed. On the press of the switch, any button, I want to set the motor speed to zero and set the slider to zero. At this point my code sets the motor speed to zero but it does not change the slider. I have searched endlessly for a solution, I think I am following the examples appropriately but I can’t get it to work. Any ideas?


void setup(){
  
  // Debug console
  Serial.begin(9600);
  
  startBLYNK_LCD();
  
  pwm.begin();
  pwm.setPWMFreq(FREQUENCY); //Set PWM frequency of PC8695 to 50Hz for the Servo

  pinMode(PWM, OUTPUT); //Set Pin 9 to Output
  pinMode(DIR, OUTPUT); //Set Pin 6 to Output

  pinMode(winchDIR1, OUTPUT); //Set Pin 10 to Output
  pinMode(winchDIR2, OUTPUT); //Set Pin 11 to Output
  
  pinMode (A0, INPUT); //Set voltage read pin A0 to input
  
}
void loop(){
  
  Blynk.run();
  
  sendBatteryVoltage();
  }

/**************** V1 BLYNK Read JOYSTICK position and adjust steering Servo Position***********/
BLYNK_WRITE(V1) {
  int joystick_Pos, pulse_Width;
  int x_position = param[0].asInt();   //uses joystick x position for steering left/right
  int y_position = param[1].asInt();  //not used
  joystick_Pos = map(x_position, 0, 255, SERVOMIN, SERVOMAX); //map joystick Blynk values to servo values
  pulse_Width = int(float(joystick_Pos)/1000000 * FREQUENCY * 4096);//calculates incremental steering value between 0-180
  pwm.setPWM(pca9685_Pin, 0, pulse_Width); //PCA9685 Channel#(pins),PWM On, PWM Off) 0=left, 90=straight, 180=right
}
/**************** END V1 BLYNK Read JOYSTICK position and adjust steering Servo Position***********/

/********** V6 Read Blynk SLIDER SWITCH and Convert to MOTOR SPEED Variables**********/
BLYNK_WRITE(V6) { 
  motorSpeed = param.asInt(); // assigning incoming value from pin V6 to a variable for Motor Speed (0-255)
                              // this value is used in the stateMachine function
  if (switchState == 1){
        analogWrite(PWM, motorSpeed); // Send PWM signal to motor
        digitalWrite(DIR, HIGH);
    }   
    else if (switchState == 2){
        analogWrite(PWM, 0); // Send PWM signal to motor
        digitalWrite(DIR, LOW);  
    }
    else if (switchState == 3){
        analogWrite(PWM, motorSpeed); // Send PWM signal to motor
        digitalWrite(DIR, LOW);
    }                
  Serial.print("V6 Slider value is: ");
  Serial.println(motorSpeed);
}
/********** END V6 Read Blynk SLIDER SWITCH and Convert to MOTOR SPEED Variables**********/

/************* V7 BLYNK THREE SEGMENT SWITCH Vehicle Direction *************/
BLYNK_WRITE(V7) {
  switchState = (param.asInt()); // Get Switch state value 1 = Forward, 2 = Neutral, 3= Reverseswitch (switchState){  //From BLYNK_WRITE(V3) switchState = 1, 2 or 3 on Blynk App
   Blynk.virtualWrite(V6, 0); 
    switch (switchState) {
    case 1:  // FORWARD       
        analogWrite(PWM, 0); // Send PWM signal to motor
        Blynk.virtualWrite(V6, 0);
        //Blynk.syncVirtual(V6); 
        break;
      
    case 2:  // NEUTRAL      
        analogWrite(PWM, neutral);  // Set motor speed to 0
        Blynk.setProperty(V6, "color", "#D3435C"); 
        Blynk.virtualWrite(V6, 0);
        //Blynk.syncVirtual(V6);
        break;
      
    case 3:  // REVERSE
        analogWrite(PWM, 0); // Send PWM signal to motor
        Blynk.virtualWrite(V6, 0);
        //Blynk.syncVirtual(V6);
        break;
  }
}

/*************END V7 BLYNK THREE SEGMENT SWITCH Vehicle Direction*************/

0 Likes

#2

Are you sure?? Similar questions have been asked before, and sending data to a widget is a very common process in Blynk.

It is as simple of using the Blynk.virtualWrite(V6, 0); command in the appropriate spot in your code.

You could even set it to 0 with that command then follow up with a Blynk.syncVirtual(V6); to trigger the server, as if you had manually set it to 0 in the App, and thus causing your BLYNK_WRITE(V6) function to respond with that “new” value.

EDIT - the sync command may not work with BT, as it requires the device to be directly connected to the Server… but the other command most certainly will.

0 Likes

#3

I deleted the project from my phone. I then set it up again with a new auth code and widgets. It works now as it is supposed to. I’m not sure why that made a difference but it did.
Thanks!

0 Likes

#4

BTW, whatever this function is (it and other pre-setup is missing from your post) is trying to run thousands of times a second in your void loop() … this is NOT a good thing with Blynk programs.

0 Likes