#define DIR_PIN 3 #define STEP_PIN 2 #define EN_PIN 4 #define RIGHT_PIN 8 #define LEFT_PIN 7 const int stepsPerRevolution = 32260 ; // = 4033*8 - how much microrotations it does const int length = 805; // mm int microrotatePermm = stepsPerRevolution / length; // microrotatePermm - one microrotation for 1mm int microstep = 1600; // number of microsteps per full revolution int microrotatePer1mms = 1000000 / microrotatePermm ; // in 1mm/sec microrotations number int microrotatePerPartmms = microrotatePer1mms / 2 ; // second/2 / microstep int speed = 120; // 50 mm/sec speed start int botton_Left = digitalRead(LEFT_PIN); int botton_Right = digitalRead(RIGHT_PIN); String data; char d1; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(EN_PIN, OUTPUT); digitalWrite(EN_PIN, HIGH); // Deactivate driver (LOW active) pinMode(DIR_PIN, OUTPUT); digitalWrite(DIR_PIN, LOW); // LOW or HIGH pinMode(STEP_PIN, OUTPUT); digitalWrite(STEP_PIN, LOW); digitalWrite(EN_PIN, LOW); // Activate driver // Inch ka chka Maria pinMode(RIGHT_PIN, INPUT_PULLUP); pinMode(LEFT_PIN, INPUT_PULLUP); } void play () { botton_Left = digitalRead(LEFT_PIN); botton_Right = digitalRead(RIGHT_PIN); digitalWrite(DIR_PIN, HIGH); for (int i =1; i <= 1000000; i++) { digitalWrite(STEP_PIN, HIGH); delayMicroseconds(microrotatePerPartmms / speed); digitalWrite(STEP_PIN, LOW); delayMicroseconds(microrotatePerPartmms / speed); botton_Left = digitalRead(LEFT_PIN); botton_Right = digitalRead(RIGHT_PIN); if (botton_Left == 0){ digitalWrite(DIR_PIN,LOW); // break; } if (botton_Right == 0) { digitalWrite(DIR_PIN, HIGH); // break; } if (Serial.available()){ data = Serial.readString(); d1 = data.charAt(0); if(d1 == 's'){ break; } } } } void autoHome() { digitalWrite(DIR_PIN, HIGH); for (int i =1; i <= 1000000; i++) { digitalWrite(STEP_PIN, HIGH); delayMicroseconds(microrotatePerPartmms / speed); digitalWrite(STEP_PIN, LOW); delayMicroseconds(microrotatePerPartmms / speed); botton_Left = digitalRead(LEFT_PIN); // int botton_Right = digitalRead(RIGHT_PIN); if (botton_Left == 0){ break; } if (Serial.available()){ data = Serial.readString(); d1 = data.charAt(0); if(d1 == 's'){ break; } } } } void position(int pos) { autoHome(); digitalWrite(DIR_PIN,LOW); int stepForPos = stepsPerRevolution * pos / 4; for (int i = 1; i <= stepForPos; i++) { digitalWrite(STEP_PIN, HIGH); delayMicroseconds(microrotatePerPartmms / speed); digitalWrite(STEP_PIN, LOW); delayMicroseconds(microrotatePerPartmms / speed); if (Serial.available()){ data = Serial.readString(); d1 = data.charAt(0); if(d1 == 's'){ break; } } } } // int p = 0; void loop() { // if (p == 0) { // checking if the position is working without C# // position(3); // p = p + 1; //} // put your main code here, to run repeatedly: if (Serial.available()) { data = Serial.readString(); d1 = data.charAt(0); if(d1 == 'p'){ play(); } else if (d1 == 'a') { autoHome(); } else if (d1 == 't') { // speed String posStr = data.substring(1); // taken value of the string starting from the 2nd element int recievedSpeed = posStr.toInt(); // recieved speed from C#, converting to an integer if (recievedSpeed == 0) { speed = 10; } else if (recievedSpeed == 1) { speed = 50; } else if (recievedSpeed == 2) { speed = 120; } // speed = recievedSpeed; // speed changing } else if (d1 == 'b') { // position String posStr = data.substring(1); int pos = posStr.toInt(); position(pos); } } }