Created
March 23, 2022 12:37
-
-
Save gestadieu/49b747e70ec41674e3d672e62a480ad1 to your computer and use it in GitHub Desktop.
Basic to control a motor through Blynk (app or web)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud | |
// See the Device Info tab, or Template settings | |
#define BLYNK_TEMPLATE_ID "" | |
#define BLYNK_DEVICE_NAME "WemosD1 Motor Control" | |
#define BLYNK_AUTH_TOKEN "" | |
// Comment this out to disable prints and save space | |
#define BLYNK_PRINT Serial | |
// A4988 driver pins | |
const int motMS1Pin = D0; | |
const int motMS2Pin = D5; | |
const int motMS3Pin = D6; | |
const int motStepPin = D7; | |
const int motDirPin = D8; | |
int motorDirection = 1; | |
int motorSpeed = 10; | |
int stepDelay = 2000; // delay in microseconds | |
bool motorStatus = true; | |
bool isCalibrated = false; | |
#include <ESP8266WiFi.h> | |
#include <BlynkSimpleEsp8266.h> | |
char auth[] = BLYNK_AUTH_TOKEN; | |
// Your WiFi credentials. | |
// Set password to "" for open networks. | |
char ssid[] = ""; | |
char pass[] = ""; | |
BlynkTimer timer; | |
// V0 is the On/Off button for controlling the stepper motor (0 is off, 1 is on) | |
BLYNK_WRITE(V0) | |
{ | |
int v0 = param.asInt(); | |
// Serial.print("V0 is the On/Off Button value: "); | |
// Serial.println(v0); | |
motorStatus = v0; | |
} | |
// V1 is the direction for the stepper motor (0 is left, 1 is right) | |
BLYNK_WRITE(V1) { | |
int v1 = param.asInt(); | |
// Serial.print("V1 is the direction value: "); | |
// Serial.println(v1); | |
motorDirection = v1; | |
} | |
// V4 is the motor speed (value between 0 and 100) | |
BLYNK_WRITE(V4) { | |
int v4 = param.asInt(); | |
Serial.print("V4 is the speed value (slider): "); | |
Serial.println(v4); | |
stepDelay = map(v4, 0, 100, 5000, 700); | |
} | |
// This function is called every time the device is connected to the Blynk.Cloud | |
BLYNK_CONNECTED() | |
{ | |
// Change Web Link Button message to "Congratulations!" | |
// Blynk.setProperty(V3, "offImageUrl", ""); | |
// Blynk.setProperty(V3, "onImageUrl", ""); | |
// Blynk.setProperty(V3, "url", ""); | |
} | |
// This function sends Arduino's uptime every second to Virtual Pin 2. | |
void myTimerEvent() | |
{ | |
// You can send any value at any time. | |
// Please don't send more that 10 values per second. | |
Blynk.virtualWrite(V2, millis() / 1000); | |
} | |
void setup() | |
{ | |
// Debug console | |
Serial.begin(9600); | |
Blynk.begin(auth, ssid, pass); | |
// You can also specify server: | |
//Blynk.begin(auth, ssid, pass, "blynk.cloud", 80); | |
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080); | |
// Setup a function to be called every second | |
timer.setInterval(1000L, myTimerEvent); | |
pinMode(motMS1Pin, OUTPUT); | |
pinMode(motMS2Pin, OUTPUT); | |
pinMode(motMS3Pin, OUTPUT); | |
pinMode(motStepPin, OUTPUT); | |
pinMode(motDirPin, OUTPUT); | |
digitalWrite(motMS1Pin, HIGH); | |
digitalWrite(motMS2Pin, HIGH); | |
digitalWrite(motMS3Pin, HIGH); | |
} | |
void loop() | |
{ | |
Blynk.run(); | |
timer.run(); | |
// You can inject your own code or combine it with other sketches. | |
// Check other examples on how to communicate with Blynk. Remember | |
// to avoid delay() function! | |
digitalWrite(motDirPin, motorDirection); | |
if (motorStatus) { | |
digitalWrite(motStepPin, HIGH); | |
delayMicroseconds(stepDelay); | |
digitalWrite(motStepPin, LOW); | |
delayMicroseconds(stepDelay); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment