Added Blynk, Now physical buttons dont work and motors run constantly

I added Blynk to my existing code. It seemed to work when I only had a single button assigned to A0. Then I added A1 & A2 which resulted in all kinds of odd things. Like, the physical buttons don’t work and motors run constantly, then switch directions, then again… I am pretty new to code and I’m really at a loss to figure this out. Any help is appreciated.
I tried to post my code below but the forum isn’t letting me for some reason. It says “Sorry, new users can only put 2 links in a post.” I can find any links in my code…

“Posting” your code is as simple as copying the text from your IDE and pasteing it here… then adding in some formatting in order to make it properly viewable…

Blynk - FTFC

1 Like

more info would be helpful:

what mcu, what motors, etc
maybe a basic schrmatic…

1 Like

Heres my code. I’m using a Particle Photon with a L298 motor driver. When I add the blynk code things are all messed up. The DC motor runs through a loop constantly about a second after plugging in. The physical stop button will stop the motor only for the duration that i hold the button down. When I let go of the stop button the motor starts going again.

I have a feeling the issue will be obvious to someone more familiar w/ code.

//SYSTEM_MODE(SEMI_AUTOMATIC);  // your code will execute without attempting to connect to WiFi or cloud

//-----BLYNK

#define BLYNK_PRINT Serial  // Set serial output for debug prints
//#define BLYNK_DEBUG       // Uncomment this to see detailed prints

#include <blynk.h>

char auth[] = "-----";

//-------

// Global variables to set motor timing
//    Each funcion (opening, closing) has a max run time and two phases, before middle limit switch (1) and after (2)
//    Each phase has a time that the speed ramps up and a beginnig speed and end speed.
//    Times are in milliseconds.  Speeds are in PWM value from 0 - 255.
const int openingMaxRunTime = 1000*9;  //12 seconds
const int openingRampTime1 = 2500;  //2.5 seconds
const int openingStartSpeed1 = 40;  //25 (PWM Value from 0 - 255)
const int openingEndSpeed1 = 80; //90
const int openingRampTime2 = 2000; //1500
const int openingStartSpeed2 = 80; //254
const int openingEndSpeed2 = 255; //255

const int closingMaxRunTime = 1000*9; //1000*12
const int closingRampTime1 = 2000; //500
const int closingStartSpeed1 = 100; //205
const int closingEndSpeed1 = 255;  //255
const int closingRampTime2 = 1500;  //1000
const int closingStartSpeed2 = 150; //180
const int closingEndSpeed2 = 110; //128

const int closingTimeAfterLimitSwitch = 1000;  //Time after closed limit switch hit before motors are stopped

const int demoOpenDelay = 5000;  //5 second delay after opening
const int demoCloseDelay = 5000;  //5 second delay after closing

//Set to 2 if controller uses 2 pins to control motor direction (i.e. HIGH,LOW for Forward), 
//  set to 1 if motor controller uses 1 pin (i.e. HIGH for forward, LOW for reverse)
const int motorControllerPins = 2;  

// connect motor controller pins to Arduino digital pins
// motor one and two connected to same pins
int enA = D0;
int in1 = D2;  // if using 1 pin controller, use D2 and leave D3 open
int in2 = D3;
// limit switches
int lr = A3;  //Close/fully retracted limit switch
int ls = A4;  //Limit switch used to trigger speed change
int le = A5;  //Open/fully extended limit switch
// buttons
int bo = A0;  //opening button
int bs = A1;  //stop button
int bc = A2;  //close button
int bd = D5;  //demo button

// Setup button
int setupButton = D6;

int allStop = 0; //Global variable to stop all actions
int working = 0; //Global variable in process of performing action

void setup()
{
  // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(lr, INPUT_PULLUP);
  pinMode(ls, INPUT_PULLUP);
  pinMode(le, INPUT_PULLUP);
  pinMode(bo, INPUT_PULLUP);
  pinMode(bs, INPUT_PULLUP);
  pinMode(bc, INPUT_PULLUP);
  pinMode(bd, INPUT_PULLUP);
  pinMode(setupButton, INPUT_PULLUP);
  
  controlMotors(0,0);

   // Debug console
  Serial.begin(9600);

  delay(5000); // Allow board to settle
  Blynk.begin(auth);
}

void loop() {

  Blynk.run();
  
  allStop = 0;
  if (digitalRead(setupButton)==LOW)
    Particle.connect();
  if(digitalRead(bo)==LOW)
    openMount();
  if(digitalRead(bc)==LOW)
    closeMount();
  if(digitalRead(bd)==LOW)
    demoButtonPushed();    
  delay(10);
}

void demoButtonPushed() {
    while(!allStop){
        if(!allStop) openMount();
        for(int x=0; x<demoOpenDelay/20 && digitalRead(bs); x++)
            delay(20);
        if(!allStop) closeMount();
        for(int x=0; x<demoCloseDelay/20 && digitalRead(bs); x++)
            delay(20);
    }
}
// this code is not run.  it was used when an interrupt routine ran on stop button pushed.
void stopButtonPushed() {
    controlMotors(0,0);
    allStop = 1;
}

void openMount() {
    //Particle.publish("opening");
    int startTime = millis();
    int startTime2 = 0;
    int mySpeed = 0;
    while(!allStop && (millis()-startTime)<openingMaxRunTime && digitalRead(le) == HIGH) {
        if(digitalRead(ls)==LOW) {
            if(millis()-startTime<openingRampTime1)
            {
                if(openingEndSpeed1>openingStartSpeed1)
                    mySpeed = openingStartSpeed1+(millis()-startTime)/(openingRampTime1/(openingEndSpeed1-openingStartSpeed1));
                else
                    mySpeed = openingStartSpeed1-(millis()-startTime)/(openingRampTime1/(openingStartSpeed1-openingEndSpeed1));
            }
            else
                mySpeed = openingEndSpeed1;
            controlMotors(1, mySpeed);
        }
        else {
            if(startTime2 == 0) startTime2 = millis();
            if(millis()-startTime2<openingRampTime2)
           {
                if(openingEndSpeed2>openingStartSpeed2)
                    mySpeed = openingStartSpeed2+(millis()-startTime2)/(openingRampTime2/(openingEndSpeed2-openingStartSpeed2));
                else
                    mySpeed = openingStartSpeed2-(millis()-startTime2)/(openingRampTime2/(openingStartSpeed2-openingEndSpeed2));
            }
            else
                mySpeed = openingEndSpeed2;
            controlMotors(1, mySpeed);
        }
        if(digitalRead(bs)==LOW){
          allStop = 1;
          controlMotors(0,0);
        }
        delay(10);
    }
    controlMotors(0,0);
}

void closeMount() {
    //Particle.publish("closing");
    int startTime = millis();
    int startTime2 = 0;
    int mySpeed = 0;
    while(!allStop && (millis()-startTime)<closingMaxRunTime && digitalRead(lr) == HIGH) {
        if(digitalRead(ls)==HIGH) {
            if(millis()-startTime<closingRampTime1)
            {
                if(closingEndSpeed1>closingStartSpeed1)
                    mySpeed = closingStartSpeed1+(millis()-startTime)/(closingRampTime1/(closingEndSpeed1-closingStartSpeed1));
                else
                    mySpeed = closingStartSpeed1-(millis()-startTime)/(closingRampTime1/(closingStartSpeed1-closingEndSpeed1));
            }
            else
                mySpeed = closingEndSpeed1;
            controlMotors(2, mySpeed);
        }
        else {
            if(startTime2 == 0) startTime2 = millis();
            if(millis()-startTime2<closingRampTime2)
            {
                if(closingEndSpeed2>closingStartSpeed2)
                    mySpeed = closingStartSpeed2+(millis()-startTime2)/(closingRampTime2/(closingEndSpeed2-closingStartSpeed2));
                else
                    mySpeed = closingStartSpeed2-(millis()-startTime2)/(closingRampTime2/(closingStartSpeed2-closingEndSpeed2));
            }
            else
                mySpeed = closingEndSpeed2;
            controlMotors(2, mySpeed);
        }
        if(digitalRead(bs)==LOW){
          allStop = 1;
          controlMotors(0,0);
        }
        delay(10);
    }
    delay(closingTimeAfterLimitSwitch);
    controlMotors(0,0);
}

// controlMotors
// Direction 0 = brake, 1 = forward, 2 = reverse
// Speed 0 - 255
void controlMotors(int Direction, int Speed) {
    if(Direction == 1){
        digitalWrite(in1, HIGH);
        if(motorControllerPins==2)
            digitalWrite(in2, LOW);
    }
    else if(Direction == 2){
        digitalWrite(in1, LOW);
        if(motorControllerPins==2)
            digitalWrite(in2, HIGH);
    }
    else {
        digitalWrite(in1, HIGH);
        digitalWrite(in2, HIGH);
    }
    analogWrite(enA, Speed);
}

I haven’t gone through your code per se, but it sounds like you are using a pin that goes HIGH on boot, or your code set it HIGH by default… and returns it there after a LOW signal is released.

When adding the Blynk libraries, some fundamental changes in coding practices need to be observed…

  • Keep the void loop() clear of most everything except Blynk.run() and any timers or OTA files required.

  • Use said Timers and Blynk functions (i.e. BLYNK_WRITE(vPIN)), and possibly interrupts, to scan for hardware and App inputs as well as monitoring and controlling hardware.

  • When programming moving objects, hardcode for ALL STOP unless specifically receiving a constant command otherwise. Never rely on just the incoming command to stop any motors.

1 Like