Go Down

Topic: cant pass my value from serial to function (Read 2343 times) previous topic - next topic

syrmic

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

Code: [Select]
#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

puller.move(1,0);
} }


and here the same code with serial library example

Code: [Select]
#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

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); }

need some help please

econjack

First, with your source code in the IDE source code window, press Ctrl-T. This will  automatically reformat your code into a common C coding style. The style you are using is VERY difficult to read.

Second, look up the basic syntax for a for loop:

   for (expression1; expression2; expression3)
   {    // start of loop body
        // loop body; statements controlled by the loop
   }    // End loop body

where:
   expression1 usually initializes the loop control variable
   expression2 is a conditional test to decide if another pass through the loop body is needed
   expression3 usually increments (decrements) the variable controlling the loop

Example:

   for (int i = 0; i < 10; i++)
   {
      Serial.print("i * i = ");
      Serial.println(i*i);
   }

Now look at your loop:

   for (int i=0;i

I see expression1, and perhaps the start of expression2, but it is probably incomplete. expression3 is totally missing. Work on that as a starting point.


Go Up