diff options
| author | user@node5.net <user@node5.net> | 2025-12-06 22:48:05 +0100 |
|---|---|---|
| committer | user@node5.net <user@node5.net> | 2025-12-06 22:51:44 +0100 |
| commit | 05e781e63f7ffc67c9a84c497502cb1dce14b72a (patch) | |
| tree | 3daa5187f977a3748b8924ae2d0ab829fa99b589 /mcu/src/main.cpp | |
| parent | cef2c222dbed2420604558eb54a187c84e9adcb8 (diff) | |
Cleanup unused items from sjory3
Diffstat (limited to 'mcu/src/main.cpp')
| -rw-r--r-- | mcu/src/main.cpp | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/mcu/src/main.cpp b/mcu/src/main.cpp new file mode 100644 index 0000000..b943185 --- /dev/null +++ b/mcu/src/main.cpp @@ -0,0 +1,99 @@ +#include <MobaTools.h> + +const byte stepPin = 12; // PWM send many pulses to drive carosel +const byte dirPin = 13; // Direction + //const byte enablePin = 8; + + +//steps per position +int stepsToMove = 828; // Magic number +unsigned long stepDelay = 1000; // milliseconds + +MoToStepper stepper( 400, STEPDIR ); + +void setup() +{ + Serial.begin(115200); + Serial.println("ESP32 is ready"); + + pinMode(13, OUTPUT); + + stepper.attach( stepPin, dirPin ); + stepper.setSpeedSteps(20000); // = 75 steps/second (steps in 10 seconds) + stepper.setRampLen(500); + stepper.setZero(); + pinMode(2,OUTPUT); +} + +void loop() +{ + if (Serial.available()>0){ + //digitalWrite(2, HIGH); + String command_string = Serial.readStringUntil('\n'); //read communication until next line + Serial.println("OK"); + command_string.trim();//removes any spaces before and after + + int firstSpaceIndex = command_string.indexOf(' ');//find the first space + if(firstSpaceIndex != -1) + { + String commandName = command_string.substring(0, firstSpaceIndex); + + int secondSpaceIndex = command_string.indexOf(' ', firstSpaceIndex + 1); + if(secondSpaceIndex != -1){ + int pumpNumber = command_string.substring(firstSpaceIndex + 1, secondSpaceIndex).toInt(); + int pumpValue = command_string.substring(secondSpaceIndex + 1).toInt(); + + if(commandName == "Pump") + { + Pump(pumpNumber, pumpValue); + } + } else{ + String secondValue = command_string.substring(firstSpaceIndex + 1, command_string.length() + 1); + Serial.println("Second value: \"" + secondValue + "\""); + + if(commandName == "Position"){ + GoToPosition(secondValue.toInt() * stepsToMove); + } + if(commandName == "LED"){ + bool led_on = secondValue == "1"; + Serial.println("LED " + led_on ? "on" : "off"); + digitalWrite(2, led_on ? HIGH: LOW); + } + } + } + } + //digitalWrite(2, HIGH); + delay(1000); + //digitalWrite(2, LOW); +} + +void GoToPosition(int value){ + //execute code + Serial.print("Position command executed with value: "); + Serial.println(value); + stepper.moveTo(value); +} +void Pump(int number,int value){ + //Execute code + Serial.print("Pump: "); + Serial.println(number); + Serial.println(" value: "); + Serial.println(value); + +} +/* +stepper.moveTo(pos9); + while(stepper.distanceToGo() > 0); + Serial.write("current position :"); + delay(stepDelay); + stepper.moveTo(pos2); + while(stepper.distanceToGo() > 0); + delay(stepDelay); + stepper.moveTo(pos5); + while(stepper.distanceToGo() > 0); + delay(stepDelay); + stepper.moveTo(pos3); + while(stepper.distanceToGo() > 0); + + delay(stepDelay); +*/ |
