Hi all,
I have coded a program for my Star Tracker with ESP32 board. Using 24byj48 stepper motor. There are 2 push switches to control the speed. Now I want to control the speed from web interface. Please help me, as I am very new with ESP32. The code is as follows:
#include <mechButton.h>
#include <idlers.h>
#include <AccelStepper.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <Adafruit_SSD1306.h>
#include <HardwareSerial.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Define proper RST_PIN if required.
#define RST_PIN -1
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
#define BUTTON_PIN1 14 // Pin we'll hook a button to. The other side hooks to ground.
#define BUTTON_PIN2 12 // Pin we'll hook the other button to.
// #define BUTTON_PIN3 13 // Reserve pin
#define motorPin1 32 // IN1 on the ULN2003 driver
#define motorPin2 33 // IN2 on the ULN2003 driver
#define motorPin3 25 // IN3 on the ULN2003 driver
#define motorPin4 26 // IN4 on the ULN2003 driver
#define motorInterfaceType 8
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
double speedstep=0.7;
double currentSpeed = 634; // Create a variable for speed
float a= currentSpeed;
int mximumspeed=1000;
mechButton button1(BUTTON_PIN1); // Set button one to pin 2.
mechButton button2(BUTTON_PIN2); // Set button two to pin 3.
// mechButton button3(BUTTON_PIN3); // Set button one to pin 2.
// AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);
AccelStepper myStepper = AccelStepper(motorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4);
// Your standard sketch setup()
void setup() {
button1.setCallback(myCallback1); // Set up #1's callback. (Don't use Analog for buttons.)
button2.setCallback(myCallback2); // Set up #2's callback.
// button2.setCallback(myCallback3);
myStepper.setMaxSpeed(mximumspeed);
myStepper.setSpeed(currentSpeed);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
// Display static text
display.println("Welcome to");
display.setCursor(0, 20);
// Display static text
display.println("TrackUniv nano");
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(1, 1);
display.println("TrackUniv nano");
display.setCursor(0, 12);
display.println("=====================");
display.display();
display.setCursor(0, 20);
display.setTextSize(2);
display.println("Step: ");
display.setCursor(60, 20);
display.setTextSize(2);
display.println(currentSpeed, 0);
display.setCursor(0, 37);
display.setTextSize(2);
display.println("Speed: ");
display.setCursor(70, 37);
display.println(currentSpeed/a);
display.setCursor(73, 52);
display.setTextSize(1);
display.println("rev/day");
display.display();
}
// This is the guy that's called when the button1 changes state.
void myCallback1(void) {
if (!button1.trueFalse()) {
currentSpeed += speedstep;
myStepper.setSpeed(currentSpeed);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(1, 1);
display.println("TrackUniv nano");
display.setCursor(0, 12);
display.println("=====================");
display.display();
display.setCursor(0, 20);
display.setTextSize(2);
display.println("Step: ");
display.setCursor(60, 20);
display.setTextSize(2);
display.println(currentSpeed, 1);
display.setCursor(0, 37);
display.setTextSize(2);
display.println("Speed: ");
display.setCursor(70, 37);
display.println(currentSpeed/a);
display.setCursor(73, 52);
display.setTextSize(1);
display.println("rev/day");
display.display();
}
}
// This is the guy that's called when the button2 changes state.
void myCallback2(void) {
if (!button2.trueFalse()) {
currentSpeed -= speedstep;
myStepper.setSpeed(currentSpeed);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(1, 1);
display.println("TrackUniv nano");
display.setCursor(0, 12);
display.println("=====================");
display.display();
display.setCursor(0, 20);
display.setTextSize(2);
display.println("Step: ");
display.setCursor(60, 20);
display.setTextSize(2);
display.println(currentSpeed, 1);
display.setCursor(0, 37);
display.setTextSize(2);
display.println("Speed: ");
display.setCursor(70, 37);
display.println(currentSpeed/a);
display.setCursor(73, 52);
display.setTextSize(1);
display.println("rev/day");
display.display();
}
}
// void myCallback3(void) {
// if (!button3.trueFalse()) {
// currentSpeed += speedstep*1000;
// myStepper.setSpeed(currentSpeed);
// oled.clear(60,120,3,8);
// oled.setCursor(60, 3);
// oled.set2X();
// oled.print("Max");
// oled.clear(60,100,6,8);
// oled.setCursor(60, 6);
// oled.set2X();
// oled.print("Max");
// }
// }
// Your standard sketch loop()
void loop() {
idle(); // Let all the idlers have time to do their thing.
myStepper.runSpeed(); // Make the stepper go.
}
TIA ![]()