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!