// defines pins numbers const int stepPin1 = 4; //PD4 const int dirPin1 = 3; //PD3 const int stepPin2 = 6; //PD6 const int dirPin2 = 5; //PD5 //const int stepPin3 = 8; //PB1 //const int dirPin3 = 7; //PB0 //const int stepPin4 = 10; //PB3 //const int dirPin4 = 9; //PB2 const int enablePin1 = 17; //PC3 const int enablePin2 = 15; //PC1 // const int enablePin3 = 14; //PC0 //const int enablePin4 = 16; //PC2 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; void setup() { // Sets the two pins as Outputs Serial.begin(4800); // pinMode(1, INPUT); // pinMode(0, INPUT); 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; val1 = 0; val2 = 0; val3 = 0; if (id == 9 && motor == 1) { digitalWrite(enablePin1, LOW); digitalWrite(dirPin1, LOW); for (int x = 0; x < steps; x++) { digitalWrite(stepPin1, HIGH); delayMicroseconds(2500); digitalWrite(stepPin1, LOW); delayMicroseconds(2500); } id = 0; motor = 0; steps = 0; digitalWrite(enablePin1, HIGH); digitalWrite(dirPin1, LOW); } else if (id == 9 && motor == 2){ digitalWrite(enablePin2, LOW); digitalWrite(dirPin2,LOW); for(int x = 0; x < steps; x++) { digitalWrite(stepPin2,HIGH); delayMicroseconds(2500); digitalWrite(stepPin2,LOW); delayMicroseconds(2500); } id = 0; motor = 0; steps = 0; digitalWrite(enablePin2, HIGH); //digitalWrite(dirPin2,HIGH); } /* 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);*/ } }