So this is my code that takes an input from the matlab and opens it up in serial monitor, This then goes through some calculation in arduino and if the steps calculated is more than 10 then an LED will turn on.
I also want the values inputted from serial to move the stepper motors, and they will be moving with different speeds and different steps.Also they are different stepper motors, one with accuracy of about 0.018 degree per step and another accuracy of 0.056.
[1] How can I implement the movement of the stepper motors given these conditions?
[2] I have attempted something in the last few lines of my code to move the motors? Is this wrong, or will it work?
(Also i only want it to run once and wait for the next input from serial monitor input, I dont want it to loop forever on the same step value)
I hope you can understand this.
CODE
#include <Stepper.h>
int ledPin=13;
String readString, data1,data2;
Stepper stepper1(6000, 2,3);
Stepper stepper2(20000,8,9);
void setup()
{
pinMode(13,OUTPUT);
Serial.begin(9600);
stepper1.setSpeed(100);
stepper2.setSpeed(100);
}
void loop() {
while (Serial.available()) {
delay(10);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //see what was received
data1 = readString.substring(1, 7);
data2 = readString.substring(7, 14);
double intPan = data1.toFloat();
double intTilt = data2.toFloat();
double DegreesPan= (intPan/1000)-360;
double DegreesTilt= (intTilt/1000)-360;
double Steps1= DegreesPan/(0.018*2);
double Steps2= DegreesTilt/(0.018*2);
if(Steps1>10){
digitalWrite(13, HIGH);
}
else{
digitalWrite(13, LOW);
}
stepper1.step(Steps1);
stepper2.step(Steps2);
readString = "";
}
}