1

**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);
      //}
    }
  }
}
5
  • so, what is your question? ... do you have some problem that you are seeing?
    – jsotola
    Commented Jan 7, 2021 at 4:49
  • Well I assume what I am inputting into the "for(int x = 0; x < (yMove1); x++)" for yMove1 is not actually an int because it doesn't move the stepper. Commented Jan 7, 2021 at 4:52
  • Based on my code, does it seem to be sending an int? Commented Jan 7, 2021 at 4:54
  • Assuming that you have the Arduino IDE on the RPi, that you send the number (which one?) from its serial monitor, and that you see the output of Serial.print(yMove1 * (-1)); as "267" in the serial monitor, your sketch receives "-267", as you think. -- But the for loop tries to count up from 0 to -267, giving you a lot of steps, until x overflows from the maximum integer to the minimum integer, which makes the condition x < yMove1 true. (You don't need the parentheses.) Is this your problem? Commented Jan 7, 2021 at 8:23
  • 1
    Oh, and why do you call Serial.end() in loop()? Commented Jan 7, 2021 at 8:27

1 Answer 1

2

You have 2 problems, which are already mentioned in the comments:

  • You call Serial.end() after you received one line. That will stop the Serial hardware. Thus the code will only receive one line and then ignore any data from Serial. I don't think, that you want this. So just remove the Serial.end().

  • yMove1 is negative, while your for loop has the condition int x = 0; x < (yMove1); x++. But zero is always higher than a negative number. Thus the for does not run at all (the condition x < yMove1 is false, when yMove1 is negative). I don't quite understand, why you want to provide a negative number. You don't have any code for direction reversing. In the current state I would suggest just not sending a negative number. I cannot say more, because it is unclear, what exactly you want here.


Note:

  • The parentheses around yMove1 inside the for loop condition is not needed.
  • The if statement if (yMove1 != 0) is not needed. If yMove1 is zero, the for loop will just not run, because it starts with zero.
2
  • I'm sure there are other ways to do it and what I'm doing is probably not the right way but I was just testing this portion of the code and I only wanted it to send 1 value. Due to how I have my RPi code written, it will continue to send the number/ the arduino will continue to receive the number, so that's why I had serial.end(). And I do see now that my yMove1 would be negative. I am currently at work but will test it later with some new code and try fixing it. Thanks for the help! Commented Jan 7, 2021 at 15:03
  • Thanks again! I actually made quite a few changes tonight as once I figured out the receiving and running the stepper, I wanted to control the speed of the stepper as well. Commented Jan 8, 2021 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.