// defines pins numbers const int stepPin1 = 4; const int dirPin1 = 3; const int stepPin2 = 6; const int dirPin2 = 5; const int stepPin3 = 8; const int dirPin3 = 7; const int stepPin4 = 10; const int dirPin4 = 9; const int enablePin1 = 17; const int enablePin2 = 15; const int enablePin3 = 14; const int enablePin4 = 16; String val; int motor = 0; int steps = 0; char instring[20]; // Room for 19 characters plus terminating null int spos = 0; // Location in the array int val1 = 0; int val2 = 0; int val3 = 0; int id = 9; //int water = 0; //float h3po4 = 0; void setup() { // Sets the two pins as Outputs Serial.begin(9600); pinMode(stepPin1,OUTPUT); pinMode(dirPin1,OUTPUT); pinMode(stepPin2,OUTPUT); pinMode(dirPin2,OUTPUT); pinMode(stepPin3,OUTPUT); pinMode(dirPin3,OUTPUT); pinMode(stepPin4,OUTPUT); pinMode(dirPin4,OUTPUT); digitalWrite(enablePin1, HIGH); digitalWrite(enablePin2, HIGH); digitalWrite(enablePin3, HIGH); digitalWrite(enablePin4, HIGH); } void loop() { if (Serial.available()) { char *bita, *bitb, *bitc; int c = Serial.read(); switch (c) { case '\r': break; // ignore case '\n': spos = 0; bita = strtok(instring, ","); bitb = strtok(NULL, ","); bitc = strtok(NULL, ","); if (bita && bitb && bitc) { val1 = atoi(bita); val2 = atoi(bitb); val3 = atoi(bitc); } // Three values collected in val1, val2 and val3 break; default: if (spos < 18) { instring[spos++] = c; instring[spos] = 0; } } } id = val1; motor = val2; steps = val3; if (id == 9 && motor == 1){ digitalWrite(enablePin1, LOW); digitalWrite(dirPin1,HIGH); for(int x = 0; x < steps; x++) { digitalWrite(stepPin1,HIGH); delayMicroseconds(2500); digitalWrite(stepPin1,LOW); delayMicroseconds(2500); } digitalWrite(enablePin1, HIGH); digitalWrite(dirPin1,LOW); } else if (id == 9 && motor == 2){ digitalWrite(enablePin2, LOW); digitalWrite(dirPin2,HIGH); for(int x = 0; x < steps; x++) { digitalWrite(stepPin2,HIGH); delayMicroseconds(2500); digitalWrite(stepPin2,LOW); delayMicroseconds(2500); } digitalWrite(enablePin2, HIGH); digitalWrite(dirPin2,LOW); } else if (id == 9 && motor == 3){ digitalWrite(enablePin3, LOW); digitalWrite(dirPin3,HIGH); for(int x = 0; x < steps; x++) { digitalWrite(stepPin3,HIGH); delayMicroseconds(2500); digitalWrite(stepPin3,LOW); delayMicroseconds(2500); } digitalWrite(enablePin3, HIGH); digitalWrite(dirPin3,LOW); } else if (id == 9 && motor == 4){ digitalWrite(enablePin4, LOW); digitalWrite(dirPin4,HIGH); for(int x = 0; x < steps; x++) { digitalWrite(stepPin4,HIGH); delayMicroseconds(2500); digitalWrite(stepPin4,LOW); delayMicroseconds(2500); } digitalWrite(enablePin4, HIGH); digitalWrite(dirPin4,LOW); } id = 0; motor = 0; steps = 0; }