[SOLVED] Blynk robot having issue with slider widget/servo

Hey, everyone!

I am having issues with widgets that send a range of data to my Particle Photon. Specifically, the slider widget and my servo.

I have the servo widget connected to a small slider, with a range of 0-180, and updates instantly instead of on release. This widget (and it’s simple code of “arms.write(param.asInt())”, where “arms” is the name of my servo) work like a charm. Until, that is, I attempt to engage the motors.

My robot has two motors driven by a dual h-bridge drive running in parallel to the 5v regulator that feeds the Photon and servo. They are controlled with the following code (which includes the servo code):

#include "blynk/blynk.h"

Servo arms;
int STBY = D6;
char auth[] = "myauthtoken";
//Motor A
int PWMA = D3; //Speed control 
int AIN1 = D5; //Direction
int AIN2 = D4; //Direction
//Motor B
int PWMB = D0; //Speed control
int BIN1 = D2; //Direction
int BIN2 = D1; //Direction

void setup() {
  pinMode(STBY, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
  arms.attach(A5);
  arms.write(0);
   Blynk.begin(auth);
}

BLYNK_WRITE(V0) {
    arms.write(param.asInt());
}

BLYNK_WRITE(V1) { //forward
    if (param.asInt() == 1) {
        digitalWrite(STBY, HIGH);
        move(0,255,0);
        move(1,255,1);
    }
    else {
        move(0,0,0);
        move(1,0,0);
        digitalWrite(STBY, LOW);
    }
}

BLYNK_WRITE(V2) { //backward
    if (param.asInt() == 1) {
        digitalWrite(STBY, HIGH);
        move(0,255,1);
        move(1,255,0);
    }
    else {
        move(0,0,0);
        move(1,0,0);
        digitalWrite(STBY, LOW);
    }
}

BLYNK_WRITE(V3) { //left
    if (param.asInt() == 1) {
        digitalWrite(STBY, HIGH);
        move(0,255,0);
        move(1,255,0);
    }
    else {
        move(0,0,0);
        move(1,0,0);
        digitalWrite(STBY, LOW);
    }
}

BLYNK_WRITE(V4) { //right
    if (param.asInt() == 1) {
        digitalWrite(STBY, HIGH);
        move(0,255,1);
        move(1,255,1);
    }
    else {
        move(0,0,0);
        move(1,0,0);
        digitalWrite(STBY, LOW);
    }
}

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

void move(int motor, int speed, int direction){
//Move specific motor at speed and direction
//motor: 0 for B 1 for A
//speed: 0 is off, and 255 is full speed
//direction: 0 clockwise, 1 counter-clockwise

  boolean inPin1 = LOW;
  boolean inPin2 = HIGH;

  if(direction == 1){
    inPin1 = HIGH;
    inPin2 = LOW;
  }

  if(motor == 1){
    digitalWrite(AIN1, inPin1);
    digitalWrite(AIN2, inPin2);
    analogWrite(PWMA, speed);
  }else{
    digitalWrite(BIN1, inPin1);
    digitalWrite(BIN2, inPin2);
    analogWrite(PWMB, speed);
  }
}```

Once one of the virtual buttons to control direction are pressed, even for half a moment, the servo will not respond again until I restart the Photon. Any help would be appreciated. Thanks!

Just a guess based on an earlier experience with an mbed board, for you to investigate further: you’re using three pins to perform analogWrite on. Pwm uses hardware timers. The number of remaining timers available can be limited, depending on how the rest of your firmware uses them. Good luck!

Hmm. If that is the case, is it possible to digital write the motor pins to HIGH and solve that?

Edit: Nope. The servo using one PWM output and the motors using digital output does not change a thing, it seems. Nothing else, presently, is using a PWM output. I have another project that does only one PWM to a servo from a Photon, with no other functions and using IFTTT instead of Blynk, with no problem. Maybe a quick spark.function is due to get whipped up to see if it is a hardware restriction.

Well, it seems that Blynk did not want to handle calling my motor controls with more than one parameter. So, I ripped apart the motor control function and re-wrote it, requiring only a single parameter. Once I did that, everything started working like a charm. :smiley:

Thanks for letting me puzzle it out, Blynk Community!

1 Like

Congratulations! Debugging issues may take some time, but always leads to greater knowledge. Nice to read you solved it.