diff options
Diffstat (limited to 'Arduino2024')
| -rw-r--r-- | Arduino2024/NewMotor/NewMotor.ino | 121 |
1 files changed, 59 insertions, 62 deletions
diff --git a/Arduino2024/NewMotor/NewMotor.ino b/Arduino2024/NewMotor/NewMotor.ino index 4c53fdb..b943185 100644 --- a/Arduino2024/NewMotor/NewMotor.ino +++ b/Arduino2024/NewMotor/NewMotor.ino @@ -1,80 +1,77 @@ #include <MobaTools.h> -const byte stepPin = 9; -const byte dirPin = 8; -//const byte enablePin = 8; +const byte stepPin = 12; // PWM send many pulses to drive carosel +const byte dirPin = 13; // Direction + //const byte enablePin = 8; //steps per position -long pos1 = 0; -long pos2 = 828; -long pos3 = 1656; -long pos4 = 2484; -long pos5 = 3312; -long pos6 = 4140; -long pos7 = 4968; -long pos8 = 5796; -long pos9 = 6624; -long pos10 = 7452; - -int stepsToMove = 828; +int stepsToMove = 828; // Magic number unsigned long stepDelay = 1000; // milliseconds MoToStepper stepper( 400, STEPDIR ); void setup() { - Serial.begin(115200); - Serial.println("ESP32 is ready"); + Serial.begin(115200); + Serial.println("ESP32 is ready"); - pinMode(13, OUTPUT); + pinMode(13, OUTPUT); - stepper.attach( stepPin, dirPin ); - stepper.setSpeedSteps(20000); // = 75 steps/second (steps in 10 seconds) - stepper.setRampLen(500); - stepper.setZero(); + 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(13, HIGH); - String command = Serial.readStringUntil('/n'); //read communication until next line - command.trim();//removes any spaces before and after + 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 firstSpaceIndex = command.indexOf(' ');//find the first space - if(firstSpaceIndex != -1) - { - String commandName = command.substring(0, firstSpaceIndex); - - int secondSpaceIndex = command.indexOf(' ', firstSpaceIndex + 1); - if(secondSpaceIndex != -1){ - int pumpNumber = command.substring(firstSpaceIndex + 1, secondSpaceIndex).toInt(); - int pumpValue = command.substring(secondSpaceIndex + 1).toInt(); + 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{ - int positionValue = command.substring(firstSpaceIndex + 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(positionValue); - } + 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(13, HIGH); - delay(1000); - digitalWrite(13, 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 @@ -86,17 +83,17 @@ void Pump(int number,int 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); -*/
\ No newline at end of file + 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); +*/ |
