From d31d2a6bfc160fb4f7f4af9766f2ecf15299bdbf Mon Sep 17 00:00:00 2001
From: Thomas Flayols <thomas.flayols@gmail.com>
Date: Mon, 9 May 2022 16:12:51 +0200
Subject: [PATCH] Change motor library, ovewrite the manual push button gui
 (temporarly)

---
 pousseseringue-arduino.cpp | 56 ++++++++++++++------------------------
 1 file changed, 21 insertions(+), 35 deletions(-)

diff --git a/pousseseringue-arduino.cpp b/pousseseringue-arduino.cpp
index f50c850..0bc1184 100644
--- a/pousseseringue-arduino.cpp
+++ b/pousseseringue-arduino.cpp
@@ -3,13 +3,14 @@
 // Feather LCD
 // Feather motor controler
 
+#pragma once // necessary for arduino IDE environment ino source file
 
 #if 0
 #define STASSID         "laas-capture"
 #define STAPSK          ""
 #else
 #define STASSID         "laas-welcome"
-#define STAPSK          "WifiLAAS2022" // this password is wrong
+#define STAPSK          "WifiLAAS2018" // this password is wrong
 #endif
 
 #define NAME            "PousseSeringue"
@@ -49,27 +50,26 @@
 
 
 
-
-
 #include <ESP8266WiFi.h>        // internal, wifi library
 #include <DNSServer.h>          // internal, access point mode
 #include <SSD1306AsciiWire.h>   // https://github.com/greiman/SSD1306Ascii
-#include <A4988.h>              // https://github.com/laurb9/StepperDriver
+#include <AccelStepper.h>
 
 #include "Debouncer.h"          // local, debouncer, short counter, long detector
 #include "common.h"
 
 
 
-
 enum class Modes { TURN, TURNSetup, RPM, RPMSetup, INFO };
 
 
-
 millis_t nowMs; // to update on main loop(): "nowMs = millis();"
 
 Debouncer<nowMs> bA, bB, bC;
-A4988 a4988(MOTOR_STEPS, DIR, STEP, SLEEP);
+
+AccelStepper stepper(AccelStepper::DRIVER, STEP,DIR);
+
+
 SSD1306AsciiWire oled;
 constexpr int DNS_PORT = 53;
 DNSServer dnsServer;
@@ -91,6 +91,8 @@ int stepDurationMs = STEP_DURATION_DEFAULT_MS;
 float stepsPerDuration2rpm () { return 60.0 * stepsPerDuration / ((MOTOR_STEPS * MICROSTEPS_CONF) * (stepDurationMs / 1000.0)); }
 int rpming = 0; // -1, 0 or +1
 
+
+
 const char* oledMode ()
 {
     switch (mode)
@@ -210,7 +212,7 @@ void buttons (int shortp, int longp)
 
 void setup()
 {
-    Serial.begin(74880);
+    Serial.begin(9600);
     Serial.setDebugOutput(true);
     Wire.begin();
     Wire.setClock(400000L);
@@ -222,10 +224,11 @@ void setup()
     oled.clear();
     oled.println("Booting...\n");
     
-    a4988.begin(STEPPER_RPM); // some value is necessary
-    a4988.setEnableActiveState(LOW);
-    a4988.setMicrostep(MICROSTEPS_CONF);
-    a4988.enable();
+    stepper.setMaxSpeed(10000);
+    stepper.setAcceleration(10000);
+    stepper.moveTo(200);
+
+
 
     uint8_t mac[6];
     WiFi.macAddress(mac);
@@ -295,31 +298,14 @@ void loop()
     }
 
     // Stepper
-    // MOVE THIS BLOCK INTO A TIMER/ISR
-    //      (it is randomly slowed down by prints / web / wifi)
-    //      (or? use A4988 RPM mode, related: a4988.begin(STEPPER_RPM);)
-    if (lastMoveMs - nowMs > (millis_t)stepDurationMs)
+
+    if (stepper.distanceToGo() == 0)
     {
-        if (rpming && move < MOTOR_STEPS * MICROSTEPS_CONF && move > -(MOTOR_STEPS * MICROSTEPS_CONF))
-        {
-            move += rpming * MOTOR_STEPS * MICROSTEPS_CONF;
-            //Serial.printf("feeding %d\n", move);
-        }        
-
-        if (move >= stepsPerDuration)
-        {
-            move -= stepsPerDuration;
-            a4988.move(stepsPerDuration);    // forward revolution
-        }
-        if (move <= -stepsPerDuration)
-        {
-            move += stepsPerDuration;
-            a4988.move(-stepsPerDuration);   // backward revolution
-        }
-
-        lastMoveMs += stepDurationMs;
+        stepper.moveTo(-stepper.currentPosition());
+        Serial.println("Rotating Motor in opposite direction...");
     }
-
+    stepper.run();
     webLoop();
+    
     dnsServer.processNextRequest(); // AP redirection
 }
-- 
GitLab