Hi...i have the first code which works perfectly. it makes the stepper motor spin one CW revolution which is set at 10,000 steps. Then it simply changes direction and make a CCW revolution.
But...the second code under, i want it to simply spin one way if i input the value 1, spin the other direction if i input 2 and stop if i enter the value 0.....but it just doesn't move at all.
Code #1, simple code.
#define dirPin 2
#define stepPin 3
void setup() {
Serial.begin(115200);
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// Set the spinning direction CW/CCW:
digitalWrite(dirPin, HIGH);
}
void loop() {
for (int i = 0 ; i <= 10000; i++) {
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
}
for (int i = 0 ; i <= 10000; i++) {
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
}
}
code #2, serial input code.
#define dirPin 2
#define stepPin 3
String incomingByte;
void setup() {
Serial.begin(9600);
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// Set the spinning direction CW/CCW:
digitalWrite(dirPin, HIGH);
}
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.readString();
Serial.print(incomingByte);
}
if (incomingByte == "1") {
for (int i = 0 ; i <= 10000; i++) {
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
}
}
if (incomingByte == "2") {
for (int i = 0 ; i <= 10000; i++) {
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
}
}
if (incomingByte == "0") {
digitalWrite(stepPin, LOW);
}
}
In serial monitor you have line endings set to Both NL & CR or Newline or Carriage return. That means that when you send type '1' and enter you are sending "1/n/r" or "1\n" or "1\r". "1\n\r", "1\n" or "1\r" is not equal to "1". Add the line incomingByte.trim(); to trim the white space (\n, \r) from your input or change line endings to none.
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.readString();
Serial.print(incomingByte);
incomingByte.trim(); // add this line
}
if (incomingByte == "1") {
Serial.print('1');
add serial debugoutput to your code to see what is really going on in your code
// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// Take it for granted at the moment scroll down to void setup
// start of macros dbg and dbgi
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope
#define dbgi(myFixedText, variableName,timeInterval) \
do { \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
} while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *
#define dirPin 2
#define stepPin 3
String incomingByte;
void setup() {
Serial.begin(9600);
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// Set the spinning direction CW/CCW:
digitalWrite(dirPin, HIGH);
}
void loop() {
if (Serial.available() > 0) {
dbg("1:",Serial.available());
incomingByte = Serial.readString();
Serial.print("#");
Serial.print(incomingByte);
Serial.println("#");
}
if (incomingByte == "1") {
Serial.println("incomingByte == 1");
for (int i = 0 ; i <= 10000; i++) {
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
}
}
if (incomingByte == "2") {
Serial.println("incomingByte == 2");
for (int i = 0 ; i <= 10000; i++) {
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
}
}
if (incomingByte == "0") {
Serial.println("incomingByte == 0");
digitalWrite(stepPin, LOW);
}
}