/*
* MotorKnob
*
* A stepper motor follows the turns of a potentiometer
* (or other sensor) on analog input 0.
*
* http://www.arduino.cc/en/Reference/Stepper
* This example code is in the public domain.
*/
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <ESPDash.h>
#include <Ticker.h>
#include <Stepper.h>
#include <AsyncElegantOTA.h>
Ticker periodicTicker;
// change this to the number of steps on your motor
const int stepsPerRevolution = 4096; // change this to fit the number of steps per revolution
// ULN2003 Motor Driver Pins
#define IN1 19
#define IN2 18
#define IN3 5
#define IN4 17
// initialize the stepper library
Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);
// the previous reading from the analog input
int previous = 0;
String hostname = "ESP32";
const char* ssid = "MatLab";
const char* password = "L4b@dnvgl1363";
unsigned long slideVal = 0;
AsyncWebServer server(80);
ESPDash dashboard(&server);
/*
Dashboard Cards
Slider Card
Valid Arguments: (ESPDash dashboard, Card Type, const char* name, const char* symbol (optional), int min, int max)
*/
Card slider(&dashboard, SLIDER_CARD, "Stepper Slider", "", 0, stepsPerRevolution);
void updateCards() {
/*
We provide our attachCallback with a lambda function to handle incomming data
`value` is the boolean sent from your dashboard
*/
/* Attach Slider Callback */
slider.attachCallback([&](int value){
slideVal = value;
/* Print our new slider value received from dashboard */
Serial.println("Slider Triggered: "+String(value));
/* Make sure we update our slider's value and send update to dashboard */
slider.update(value);
dashboard.sendUpdates();
});
}
void setup() {
pinMode(2,OUTPUT);
Serial.begin(115200);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
WiFi.setHostname(hostname.c_str()); //define hostname
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(2 ,LOW);
delay(750);
Serial.println("Connecting to WiFi.");
delay(750);
Serial.println("Connecting to WiFi..");
delay(750);
Serial.println("Connecting to WiFi...");
}
Serial.println(WiFi.localIP());
server.begin();
digitalWrite(2 ,HIGH);
periodicTicker.attach_ms(5000, updateCards);
// set the speed in RPM
myStepper.setSpeed(1);
}
void loop() {
// get the sensor value
// Få byttet analogread med dashboard verdi
int val = -(slideVal/8);
// move a number of steps equal to the change in the
// sensor reading
myStepper.step(val - previous);
// remember the previous value of the sensor
previous = val;
}