summaryrefslogtreecommitdiff
path: root/mcu/src/main.cpp
blob: b9431851321480e9ccda0a5c8b789bc1df2b9d1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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);
*/