**I wanted to add an updated code after I have finished it to help others in the future if they are ever looking for it.
EDIT AT END OF QUESTION AFTER ANSWER AND CODE UPDATE.**
I currently have an Uno with a CNCBoard Hat.
I am running a program on my RPi and sending a value through serial to the Arduino. I am receiving what I believe is a string, converting it to an int
using toInt()
(I found this solution somewhere). I then want to write that value to the stepper I have connected.
I know the stepper works, because if I run a simple test program such as this one:
const int stepX = 2;
const int dirX = 5;
const int stepY = 3;
const int dirY = 6;
const int stepZ = 4;
const int dirZ = 7;
const int enPin = 8;
void setup() {
// Sets the two pins as Outputs
pinMode(stepX, OUTPUT);
pinMode(dirX, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW);
digitalWrite(dirX, HIGH);
digitalWrite(dirY, LOW);
digitalWrite(dirZ, HIGH);
}
void loop() {
// Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for (int x = 0; x < 200; x++) {
digitalWrite(stepX,HIGH);
delayMicroseconds(1000);
digitalWrite(stepX, LOW);
delayMicroseconds(1000);
}
delay(1000); // One second delay
}
The stepper rotates as the program describes. Where 200 is the pulse count.
The program I am currently trying to use is:
// defines pins numbers
//Front left wheel
const int stepX = 2;
const int dirX = 5;
//Front right wheel
const int stepY = 3;
const int dirY = 6;
//Back left wheel
const int stepZ = 4;
const int dirZ = 7;
//Back right wheel
const int stepA = 12;
const int dirA = 13;
const int enPin = 8;
void setup() {
Serial.begin(9600);
// Sets the two pins as Outputs
pinMode(stepX, OUTPUT);
pinMode(dirX, OUTPUT);
pinMode(stepY, OUTPUT);
pinMode(dirY, OUTPUT);
pinMode(stepZ, OUTPUT);
pinMode(dirZ, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW);
digitalWrite(dirX, HIGH);
digitalWrite(dirY, LOW);
digitalWrite(dirZ, HIGH);
digitalWrite(dirA, HIGH);
}
void loop() {
String YpulseEncode;
if (Serial.available() > 0) {
YpulseEncode = Serial.readStringUntil('\n');
int yMove1 = YpulseEncode.toInt();
//XpulseCalc = Serial.read();
//Serial.print("YpulseCalc: ");
Serial.println(yMove1 * (-1));
//Serial.print("YpulseCalc +: ");
//Serial.println(YpulseCalc
//Serial.print("XpulseCalc: ");
//Serial.println(XpulseCalc);
delay(5000);
Serial.end();
if (yMove1 != 0) {
// Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for (int x = 0; x < (yMove1); x++) {
digitalWrite(stepX, HIGH);
delayMicroseconds(1000);
digitalWrite(stepX, LOW);
}
}
}
}
The number I am receiving from the RPi is -267. So when the serial monitor shows it, it is 267. I want to send the number 267 pulses to the stepper motor.
EDIT - UPDATED CODE
AccelStepper yStepper(AccelStepper::DRIVER, 2, 5);
// defines pins numbers
//Front left wheel
const int stepX = 2;
const int dirX = 5;
//Front right wheel
const int stepY = 3;
const int dirY = 6;
//Back left wheel
const int stepZ = 4;
const int dirZ = 7;
//Back right wheel
const int stepA = 12;
const int dirA = 13;
const int enPin = 8;
void setup() {
yStepper.setMaxSpeed(800);
yStepper.setAcceleration(400);
Serial.begin(9600);
// Sets the pins as Outputs
pinMode(stepX, OUTPUT);
pinMode(dirX, OUTPUT);
pinMode(stepY, OUTPUT);
pinMode(dirY, OUTPUT);
pinMode(stepZ, OUTPUT);
pinMode(dirZ, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW);
digitalWrite(dirX, HIGH);
digitalWrite(dirY, LOW);
digitalWrite(dirZ, HIGH);
digitalWrite(dirA, HIGH);
}
void loop() {
String YpulseEncode;
if (Serial.available() > 0) {
YpulseEncode = Serial.readStringUntil('\n');
int yMove1 = YpulseEncode.toInt();
if (yMove1 < 0) {
int yMoveNew = yMove1 * (-1);
//XpulseCalc = Serial.read();
//Serial.print("YpulseCalc: ");
Serial.println(yMove1 * (-1));
//Serial.print("YpulseCalc +: ");
//Serial.println(YpulseCalc
//Serial.print("XpulseCalc: ");
//Serial.println(XpulseCalc);
delay(2000);
// Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
//for (int x = 0; x < yMoveNew; x++) {
yStepper.move(yMoveNew);
yStepper.runToPosition();
//digitalWrite(stepX, HIGH);
delayMicroseconds(1000);
//digitalWrite(stepX, LOW);
//}
}
}
}
Serial.print(yMove1 * (-1));
as "267" in the serial monitor, your sketch receives "-267", as you think. -- But thefor
loop tries to count up from 0 to -267, giving you a lot of steps, untilx
overflows from the maximum integer to the minimum integer, which makes the conditionx < yMove1
true. (You don't need the parentheses.) Is this your problem?Serial.end()
inloop()
?