0

i'm new here and stuck with my problem so i make my code running stepper motor with verbal speed and its working perfectly then i attended to upgrade my code to change my speed by serial monitor trying to change speed but always failed my my function cannot read the vale from serial port

this is my code working perfectly

#include "Msteps.h"   
#define Dir_pin 8
#define Stp_pin 9
#define En_pin 4
#define RDir_pin 12
#define RStp_pin 11
#define REn_pin 7
float angel=1.8;
float dim =5;
long _speed =100;
 int reseloutin =200;
  float Prpm=2;
Msteps puller (reseloutin, RDir_pin,RStp_pin,REn_pin);
long MoveEachOne=1;
void setup() {
  Serial.begin(115200); 
  sCmd.addCommand("P",     processCommand);  // Converts two arguments to integers and echos them back

     puller.setSpeedRPM(Prpm);


}
void loop() {

 for (int i=0;i<MoveEachOne;i++){

    puller.move(1,0);

  }
}

and here the same code with serial library example

#include <SerialCommand.h>
#include "Msteps.h"   
#define Dir_pin 8
#define Stp_pin 9
#define En_pin 4
#define RDir_pin 12
#define RStp_pin 11
#define REn_pin 7
float angel=1.8;
float dim =5;
long _speed =100;
 int reseloutin =200;
 //float rerpm=200;
 int Prpm=0;
SerialCommand sCmd;     // The demo SerialCommand object


Msteps puller (reseloutin, RDir_pin,RStp_pin,REn_pin);
long MoveEachOne=1;

void setup() {
  Serial.begin(115200); 
  sCmd.addCommand("P",     processCommand);  // Converts two arguments to integers and echos them back

     puller.setSpeedRPM(Prpm);


}

void loop() {
  sCmd.readSerial(); 
 for (int i=0;i<MoveEachOne;i++){
    // Roll.move(10,0);

    puller.move(1,0);

  }
}
void processCommand() {
  int aNumber;
  char *arg;

  Serial.println("We're in processCommand");
  arg = sCmd.next();
  if (arg != NULL) {
    aNumber = atoi(arg);    // Converts a char string to an integer
    Serial.print("First argument was: ");
    Serial.println(aNumber);
  }
  else {
    Serial.println("No arguments");
  }

  arg = sCmd.next();
  if (arg != NULL) {
    aNumber = atol(arg);
    Serial.print("Second argument was: ");
    Serial.println(aNumber);
  }
  else {
    Serial.println("No second argument");
  }
   Prpm = aNumber;
   Serial.print("prpm");
    Serial.println(Prpm);
}
  • start by reducing your code to only receive data from the serial port and send the same data back to the serial port – jsotola May 31 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.