I am working on creating a project using an Arduino UNO and an HC-08 BLE module and I’m currently running the Blynk app (most recent version) on an iPhone 6 running iOS 11. The goal is to use a stepper motor to raise and lower a large map to hide a TV when the TV is not in use.
There is one physical momentary switch attached to the Arduino and one button widget configured in the Blynk app to act as a switch. The goal is to have both the physical button and the app switch accomplish the same task. Pressing either button will either roll up or unroll a map, depending on the current state of the map (i.e. HIGH/LOW).
At this point I’ve got most of the code working and responding as one would expect including the Blynk app mirroring the current state of the map and playing nicely with the physical button. However, there is one minor issue that is driving me crazy and I’d love some help.
If I press the button in the app to either raise or lower the map, the appropriate code executes as one would expect; however, if the Blynk button is pressed multiple times while the stepper motor is either raising or lowering the map, then the map will be raised and lowered over and over, for as many times as the button was pushed. I know I am probably missing something obvious here, but how would I go about having both Blynk and the Arduino ignore button presses made while the stepper motor is executing either a raise or lower action.
I’ve searched the documentation and these forums for an answer and haven’t found it, though in fairness I’m pretty new to this whole coding things so I could be missing something completely obvious. If that is the case I sincerely apologize and thank you for your assistance.
#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>
#include <Stepper.h>
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 3, 4, 5, 6);
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "redacted";
SoftwareSerial SerialBLE(10, 11); // RX, TX
// Set your LED and physical button pins here
const int hallPin = 7;
int hallState = 0;
const int button = 2;
int buttonState;
int mapState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
BlynkTimer timer;
void checkPhysicalButton(); //set up blynk app and what happens when blynk switch is pressed
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
Blynk.virtualWrite(V2, mapState);
}
// When app button is pressed move the map either up or down, depending on its previous location
BLYNK_WRITE(V2){
if (param.asInt() == 1 && mapState == LOW){
Blynk.setProperty(V2, "color", "#808080");
Blynk.setProperty(V2, "onLabel", "Unavailable");
Blynk.setProperty(V2, "offLabel", "Unavailable");
myStepper.step(-1300);
for (int i=0; i<61; i++){
myStepper.step(-5);
myStepper.setSpeed(60-i);
}
mapState = HIGH;
Blynk.setProperty(V2, "onLabel", "Lower");
Blynk.setProperty(V2, "offLabel", "Raise");
Blynk.setProperty(V2, "color", "#ED9D00");
myStepper.setSpeed(60);
}
else if (param.asInt() == 0 && mapState == HIGH){
Blynk.setProperty(V2, "color", "#808080");
Blynk.setProperty(V2, "onLabel", "Unavailable");
Blynk.setProperty(V2, "offLabel", "Unavailable");
myStepper.step(1300);
for (int i=0; i<61; i++){
myStepper.step(5);
myStepper.setSpeed(60-i);
}
mapState = LOW;
Blynk.setProperty(V2, "onLabel", "Lower");
Blynk.setProperty(V2, "offLabel", "Raise");
Blynk.setProperty(V2, "color", "#ED9D00");
myStepper.setSpeed(60);
}
}
void checkPhysicalButton(){ //Set what happens when the physical button is pressed
buttonState = digitalRead(button);
if (buttonState == LOW && millis() - lastDebounceTime > debounceDelay && mapState == HIGH){
Serial.println("Lowering");
myStepper.step(1300);
for (int i=0; i<61; i++){
myStepper.step(5);
myStepper.setSpeed(60-i);
}
mapState = LOW;
Blynk.virtualWrite(V2, mapState);
myStepper.setSpeed(60);
lastDebounceTime = millis();
}
else if (buttonState == LOW && millis() - lastDebounceTime > debounceDelay && mapState == LOW){
Serial.println("Raising");
myStepper.step(-1300);
for (int i=0; i<61; i++){
myStepper.step(-5);
myStepper.setSpeed(60-i);
}
mapState = HIGH;
Blynk.virtualWrite(V2, mapState);
myStepper.setSpeed(60);
lastDebounceTime = millis();
}
}
void setup()
{
// Debug console
Serial.begin(9600);
SerialBLE.begin(9600);
Blynk.begin(SerialBLE, auth);
Serial.println("Waiting for connections...");
pinMode(hallPin, INPUT);
pinMode(button, INPUT_PULLUP);
// set the speed of the stepper motor at 30 rpm:
myStepper.setSpeed(30);
// home the map/stepper motor
while (digitalRead(hallPin) == HIGH)
{
myStepper.step(-1);
}
myStepper.setSpeed(10);
myStepper.step(100);
myStepper.setSpeed(60);
// Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalButton);
}
void loop()
{
Blynk.run();
timer.run();
}