#include <Stepper.h>
#define ena 52
const int stepsPerRevolution = 1600;
Stepper motor1(stepsPerRevolution, 50, 51);
void setup() {
motor1.setSpeed(370); //370 ES EL MAXIMO
Serial.begin(115200);
Serial.setTimeout(0);
pinMode(ena, OUTPUT);
Serial.println("Initialize control");
digitalWrite(ena, HIGH);
}
void loop() {
int inByte = Serial.read();
switch (inByte) {
case 'a':
Serial.println("Derecha");
while (inByte) {
int x = Serial.read();
if (x == '1') {
Serial.println("SALIR");
break;
}
motor1.step(stepsPerRevolution);
}
break;
case 'b':
Serial.println("Izquierda");
while (inByte) {
int x = Serial.read();
if (x == '1') {
Serial.println("SALIR");
break;
}
motor1.step(-stepsPerRevolution);
}
break;
case 'c':
digitalWrite(ena, HIGH);
Serial.println("ON");
break;
case 'd':
digitalWrite(ena, LOW);
Serial.println("OFF");
break;
}
}
It is to control a Stepper Motor Via Serial. When I press "A", the stepper motor starts on the right, when I procure "B" start to the left, I stop it with "1". My problem is that it is not 1,600 steps of the constant "stepsPerRevolution", it is infinite.
My 2nd problem is that I want to be able to introduce the amount of steps that I want the engine Stepper from the serial monitor.
My Multi-tasking in Arduino has a detailed stepper motor example controlled by user inputs.
My Arduino Software Solutions has a variety of complete sketches for reading user input from Serial, including parsing of numbers in a more reliable way then Serial Basic.
Honestly I still do not understand how to do it (apologize, I am relatively new with Arduino). In Python it is something similar to the following:
int x = Serial.read();
int y = Serial.read();
if (x == '1') {
Serial.println("EXIT");
break;
}
motor1.step(y);
"y" it would be the number of steps you should travel. Is there any way to do it? That is, send two data and not only 1. Because the steps may be 200 for half turn or 400 for a full turn (or any other number).
That's what I have in mind, I do not know if it existed another simplest and convenient way to do it.
You're getting closer.
I think that you want to be sending and then parsing a message with both the direction and the number of steps.
For example "1,234\n".
You can follow the Serial Input Basics example number 5 and use strtok, to separate the lead character of the message at the comma and use atoi to convert the remaining text to a number.
Alternatively, but less generically, you can easily determine the first character in the receivedChars character array which is at receivedChars[0] and then you use atoi on the receivedChars after the comma with atoi(&receivedChars[2])
Thank you very much for your help. Everything works well from the serial monitor of Arduino. My other problem is when I try to do it from Python.
The Python code is: "ser.write (b'1,200 ')" or "ser.write (b'1,200\n')"
Note: this Python code send the data correctly because the Arduino LED indicates it (besides you trying it with the code that I started at the beginning).
From the serial monitor it works, as I said before, but, I do not know why it does not recognize it when I send it from Python.
steps = b"800"
x = input("Enter data: ")
if x == "a":
ser.write(b'1, %s' % steps)
x = input("Enter data: ")
Make sure that the right string is sent (and of course the right port needs to be open and you cannot have the arduino serial monitor opened at the same time)
receivedChars[ndx] = '\0' Do not receive "rc". But, when I change it by "rc", it receives the data from Python, but only when I enter the serial monitor of Python and press ENTER, I do not know why it is not automatically sent.
The code from Ribin2's tutorial is rock solid. The problem is not there, especially true since the code works from the Serial monitor.
The issue must be on the python side. What version of python are you running? Please provide the complete python code showing the opening of the serial port.