Motor StepCount adjustment via user inputData

Hi I hope I put my question on the right thread, else sorry :slightly_frowning_face:
I just went thru arduino experiment last week, new and still learning, i just got lucky to make my experiment run via Visual studio C#, my current problem is I’m confuse on how to create and where to create a formula to control my stepcount once a user input a number, would it be on the arduino or the visual studio IDE. At present my code method is within arduino IDE. My codes are working via specific methods and position only without inputData from user.Looking forward for someone to give me a reference to learn. Thank you in advanced. My code is below for arduino.

#include <L298N.h>
#include <Stepper.h>

// Define Constants
String readString;

// Stepper Motor1 Connections to A4988
const int M1_dirPin = 2;  // Direction
const int M1_stepPin = 3; // Step
// Motor1 steps per rotation
const int M1_STEPS_PER_REV = 1.8;

void setup() {
   // put your setup code here, to run once:

  Serial.begin(9600);
  Serial.println("Connected via serial"); // so I can keep track of what is loaded
  
// Setup the pins as Outputs for Stepper Motor1
  pinMode(M1_stepPin,OUTPUT); 
  pinMode(M1_dirPin,OUTPUT);



//<-------------- STEPPER MOTOR1 -------------->

void stm1A(){
  // Set motor direction clockwise
  digitalWrite(M1_dirPin,HIGH); 
  
 // Spin motor two rotations quickly 45 deg
  for(int x = 0; x < (M1_STEPS_PER_REV * 25); x++) {
    digitalWrite(M1_stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(M1_stepPin,LOW);
    delayMicroseconds(1000);
  }
}

void stm1B(){
  // Set motor direction clockwise
  digitalWrite(M1_dirPin,HIGH); 
  
 // Spin motor two rotations quickly 90 deg
  for(int x = 0; x < (M1_STEPS_PER_REV * 50); x++) {
    digitalWrite(M1_stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(M1_stepPin,LOW);
    delayMicroseconds(1000);
  }
}

void stm1C(){
  // Set motor direction clockwise
  digitalWrite(M1_dirPin,HIGH); 
  
 // Spin motor two rotations quickly 180 deg
  for(int x = 0; x < (M1_STEPS_PER_REV * 100); x++) {
    digitalWrite(M1_stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(M1_stepPin,LOW);
    delayMicroseconds(1000);
  }
}

void stm1D(){
  // Set motor direction clockwise
  digitalWrite(M1_dirPin,HIGH); 
  
 // Spin motor two rotations quickly 360 deg
  for(int x = 0; x < (M1_STEPS_PER_REV * 200); x++) {
    digitalWrite(M1_stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(M1_stepPin,LOW);
    delayMicroseconds(1000);
  }
}

void stm1E(){
  // Set motor direction counterclockwise
  digitalWrite(M1_dirPin,LOW); 
  
 // Spin motor two rotations quickly
  for(int x = 0; x < (M1_STEPS_PER_REV * 25); x++) {
    digitalWrite(M1_stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(M1_stepPin,LOW);
    delayMicroseconds(1000);
  }
}

void stm1F(){
  // Set motor direction counterclockwise
  digitalWrite(M1_dirPin,LOW); 
  
 // Spin motor two rotations quickly
  for(int x = 0; x < (M1_STEPS_PER_REV * 50); x++) {
    digitalWrite(M1_stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(M1_stepPin,LOW);
    delayMicroseconds(1000);
  }
}

void stm1G(){
  // Set motor direction counterclockwise
  digitalWrite(M1_dirPin,LOW); 
  
 // Spin motor two rotations quickly
  for(int x = 0; x < (M1_STEPS_PER_REV * 100); x++) {
    digitalWrite(M1_stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(M1_stepPin,LOW);
    delayMicroseconds(1000);
  }
}

void stm1H(){
  // Set motor direction counterclockwise
  digitalWrite(M1_dirPin,LOW); 
  
 // Spin motor two rotations quickly
  for(int x = 0; x < (M1_STEPS_PER_REV * 200); x++) {
    digitalWrite(M1_stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(M1_stepPin,LOW);
    delayMicroseconds(1000);
  }
}


void loop() {
  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char sdata = Serial.read();
    readString += sdata;
    Serial.println(readString);
    }
  
  //<-------------- STEPPER MOTOR1 -------------->
  
  if (readString == ("stm1A")){
    stm1A();
    readString="";
  }
  
  if (readString == ("stm1B")){  
    stm1B();
    readString="";
  }
  
  if (readString == ("stm1C")){  
    stm1C();
    readString="";
  }

  if (readString == ("stm1D")){  
    stm1D();
    readString="";
  }

  if (readString == ("stm1E")){  
    stm1E();
    readString="";
  }
  
  if (readString == ("stm1F")){  
    stm1F();
    readString="";
  }
  
  if (readString == ("stm1G")){  
    stm1G();
    readString="";
  }
  
  if (readString == ("stm1H")){  
    stm1H();
    readString="";
  }
  
}

Old code reposted

Having looked at your program I don't understand your question. Your program seems to receive several different messages and then calls different functions. What do you want to do that is different?

As a separate comment I notice that all your messages begin with "stm1" and the only difference between messages is the final letter. There is no useful information conveyed by the "stm1" part so just leave it out and only send the relevant letter 'A', 'B' , 'C' etc.

Associated with that comment it is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0. And have a look the examples in Serial Input Basics - simple reliable ways to receive data.

There is a simple user-input example in Planning and Implementing a Program

...R Stepper Motor Basics Simple Stepper Code

Robin2:
Having looked at your program I don’t understand your question. Your program seems to receive several different messages and then calls different functions. What do you want to do that is different?

Sorry about the confusion,my code has this as an example, the number 25 is my step count, I want to change this as an inputdata coming from users (from 1~200). Do i define this in arduino IDE as an int coming from serial input or can i cast a whole complete method from visual studio(which i’m not familiar with). my codes does work with defined methods only but with users input I’m not able to get the correct code structure.

// Spin motor two rotations quickly 45 deg
  for(int x = 0; x < (M1_STEPS_PER_REV * 25); x++) {
    digitalWrite(M1_stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(M1_stepPin,LOW);
    delayMicroseconds(1000);
  }

Robin2:
As a separate comment I notice that all your messages begin with “stm1” and the only difference between messages is the final letter. There is no useful information conveyed by the “stm1” part so just leave it out and only send the relevant letter ‘A’, ‘B’ , ‘C’ etc.

I just put stm1 as an identification for me since my experiments consist of 2 stepper motor and 1 dc motor controlled by uno I just removed the other motor codes but i can change for improvement and thanks for the capital info, i will take note of that on my revise code.

this is my gui sample

joelc:
Sorry about the confusion,my code has this as an example, the number 25 is my step count, I want to change this as an inputdata coming from users (from 1~200). Do i define this in arduino IDE as an int coming from serial input or can i cast a whole complete method from visual studio(which i’m not familiar with). my codes does work with defined methods only but with users input I’m not able to get the correct code structure.

I am still confused because I don’t understand how your numbers should integrate with the messages you are already sending. Maybe you need a different approach altogether.

I suspect that the simplest solution to your problem is to have your PC program always send a message to the Arduino that contains all the relevant parts. For example if you want to send the number-of-steps, the direction you could send a message like this <24,F> which would be interpreted as 24 steps in the Forward direction. If you need to control 2 motors then send <24,F,187,R> which would mean 24 steps forward for motor1 and 187 steps in reverse for motor2

The 3rd example and the parse example in Serial Input Basics illustrate how to receive data in that format.

If all that does not help then please give some examples of the things you want your stepper motors to be able to do.

…R

I think you are right, at least i have some validation from other people that the code structure that i want will have a problem with the current codes that I'm sending, I will read thru on the links that you provided and hopefully i can adapt and come up with the correct code structure within this week. I still need to learn a lot from arduino and coding.

I can disregard all my codes and do restructuring, my priority aim is that. (my motor is 1.8 deg) I want my stepper motor to move either forward or reversed with a controlled step count, the step count will be entered by the user in the GUI.

Thanks robin2, for accommodating my questions.

Think about your project as two separate parts.

In one part you have some code that can receive data and store the values in variables. This part has no idea of what the data will be used for.

The other part uses the values in the variables to make the motors move. This part has no idea where the data came from.

That way you can test each part separately.

...R

Here I go again and tried a different approach, and converted incoming ASCII to integer, I tried and tested on arduino serial monitor (newline, 9600 baud rate) and its working (3 motors) but during commands send thru visual studio c# the buttons needs to be double clicked before it respond and I notice that when i change to certain direction it will still move to the previous state before moving to the current command. What could be causing the weird response?

This is from the visual studio side

port.Write('\n'+"my command")

joelc:
Here I go again and tried a different approach,

You have not posted your latest Arduino code so how can we help?

…R

This is from the visual studio side

port.Write('\n'+"my command")

That redacted snippet is about as useful as tits on a bull.

Robin2:
You have not posted your latest Arduino code so how can we help?

…R

I updated my code on the beginning post but I’ll post it in here as well

#include <L298N.h>
#include <Stepper.h>
#include <A4988.h>

// Define Constants
String readString;

unsigned int integerValue=0;  // Max value is 65535
char incomingByte;

// Stepper Motor1 Connections to A4988
const int sm1_dirPin = 2;  // Direction
const int sm1_stepPin = 3; // Step
// Motor1 steps per rotation
const int sm1_step_rev = 1.8;
int sm1_enPin = 10;

// Stepper Motor2 Connections to A4988
const int sm2_dirPin = 4;  // Direction
const int sm2_stepPin = 5; // Step
// Motor2 steps per rotation
const int sm2_step_rev = 1.8;
int sm2_enPin = 11;

// DC Motor A
int enA = 9;
int in1 = 8;
int in2 = 7;

void setup() {
   // put your setup code here, to run once:

  Serial.begin(9600);
  Serial.println("Connected via serial"); // so I can keep track of what is loaded

// For DC Motor setup
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  
// Setup the pins as Outputs for Stepper Motor1
  pinMode(sm1_stepPin,OUTPUT); 
  pinMode(sm1_dirPin,OUTPUT);
  pinMode(sm1_enPin,OUTPUT);
  

// Setup the pins as Outputs for Stepper Motor2
  pinMode(sm2_stepPin,OUTPUT); 
  pinMode(sm2_dirPin,OUTPUT);
  pinMode(sm2_enPin,OUTPUT);
}

//digitalWrite(sm1_dirpin,HIGH); set to HIGH for CW Stepper Motor1
//digitalWrite(sm1_dirpin,LOW); set to LOW for CCW Stepper Motor1

//digitalWrite(sm2_dirpin,HIGH); set to HIGH for CW Stepper Motor1
//digitalWrite(sm2_dirpin,LOW); set to LOW for CCW Stepper Motor1


//<-------------- DC MOTOR -------------->

void dm1off(){
  // Turn off DC motor
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}

void dm1cw(){
  // Turn on DC motor A move CW
  analogWrite(enA, 200);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  // *secs before it moves to the second command.
  delay(500);
}

void dm1ccw(){
  // Turn on DC motor A move CCW
  analogWrite(enA, 200);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  // *secs before it moves to the second command.
  delay(500);
}

//<-------------- STEPPER MOTOR1 -------------->

void sm1cw(){
  // Set motor direction clockwise case 10785
    digitalWrite(sm1_dirPin,HIGH); 
}

void sm1ccw(){
  // Set motor direction counterclockwise case 42185
  digitalWrite(sm1_dirPin,LOW); 
}

void sm1on(){
  // Spin motor
    for(int x = 0; x < (sm1_step_rev * integerValue); x++) {
    digitalWrite(sm1_stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(sm1_stepPin,LOW);
    delayMicroseconds(1000);
  }
}

//<-------------- STEPPER MOTOR2 -------------->

void sm2cw(){
  // Set motor direction clockwise case 10885
    digitalWrite(sm2_dirPin,HIGH); 
}

void sm2ccw(){
  // Set motor direction counterclockwise case 43185
  digitalWrite(sm2_dirPin,LOW); 
}

void sm2on(){
  // Spin motor
    for(int x = 0; x < (sm2_step_rev * integerValue); x++) {
    digitalWrite(sm2_stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(sm2_stepPin,LOW);
    delayMicroseconds(1000);
  }
}



//<-------------- VOID LOOP -------------->

void loop() {
  if (Serial.available() > 0) {   // something came across serial
    integerValue = 0;         // throw away previous integerValue
    while(1) {            // force into a loop until 'n' is received
      incomingByte = Serial.read();
      if (incomingByte == '\n') break;   // exit the while(1), we're done receiving
      if (incomingByte == -1) continue;  // if no characters are in the buffer read() returns -1
      integerValue *= 10;  // shift left 1 decimal place
      // convert ASCII to integer, add, and shift left 1 decimal place
      integerValue = ((incomingByte - 48) + integerValue);
    }
      Serial.println(integerValue);   // Do something with the value
       if (integerValue > 0 && integerValue < 201){
            sm1on();
            sm2on();    
     }
  }

  switch(integerValue){
      case 50726: // turn off dc motor
           dm1off();
      break;
      
      case 57393: // move dc motor clockwise
           dm1cw();
      break;

      case 49513: // move dc motor counterclockwise
           dm1ccw();
      break;
    
      case 10785:  // move stepper motor1 clockwise
           sm1cw();
           digitalWrite(sm2_enPin,HIGH);
           digitalWrite(sm1_enPin,LOW);           
      break;
      
      case 42185:  // move stepper motor1 counterclockwise
          sm1ccw(); 
          digitalWrite(sm2_enPin,HIGH); 
          digitalWrite(sm1_enPin,LOW); 
      break;

      case 10885:  // move stepper motor2 clockwise
           sm2cw();  
           digitalWrite(sm1_enPin,HIGH);
           digitalWrite(sm2_enPin,LOW); 
      break;
      
      case 43185:  // move stepper motor2 counterclockwise
           sm2ccw(); 
           digitalWrite(sm1_enPin,HIGH);
           digitalWrite(sm2_enPin,LOW);  
      break;

  }
  
  delay(100);        // delay in between reads for stability
}

PaulS: ``` This is from the visual studio side

port.Write('\n'+"my command")



That redacted snippet is about as useful as tits on a bull.

can you enlighten mo on this statement. Thanks

joelc: I updated my code on the beginning post but I'll post it in here as well

NEVER EVER do that. It just makes a nonsense of the Thread for anyone coming to it new and regular readers never go back to the beginning.

Please re-instate the Original Post.

...R

Where are these funny numbers coming from and what do they represent

case 50726:

I can't see how you have restructured your code because I can't compare it with the earlier version.

...R

can you enlighten mo on this statement.

You need to know what a bull is? You need to know what tits are? You need to know why some sexes have tits and some don't need them?

Robin2:
Where are these funny numbers coming from and what do they represent

case 50726:

I can’t see how you have restructured your code because I can’t compare it with the earlier version.

…R

I have attached my old code:
I’m unsure from where the delay is coming from, when I tried using arduino serial monitor the code is working without any delays but using my gui created from visual studio the delay manifest.

DFZ_Control.ino (8.78 KB)

joelc: I have attached my old code:

You did not answer my question about the funny numbers. There is little point looking at the code if I don't know what it is trying to do.

...R

PaulS: You need to know why some sexes have tits and some don't need them?

Having raised calves such things on bulls would, in fact, be quite useful when the cow abandons the calf.

Given that you may want to consider a different analogy for "useless".

I resolve my problem on delayed respond by changing

This is from the visual studio side

port.Write('\n'+"my command")

to

port.WriteLine("my command")

this is to satisfy the below script from arduino side

void loop() {


  if (Serial.available() > 0) {   // something came across serial
    integerValue = 0;         // throw away previous integerValue
    while(1) {            // force into a loop until 'n' is received
      incomingByte = Serial.read();
      if (incomingByte == '\n') break;   // exit the while(1), we're done receiving
      if (incomingByte == -1) continue;  // if no characters are in the buffer read() returns -1
      integerValue *= 10;  // shift left 1 decimal place
      // convert ASCII to integer, add, and shift left 1 decimal place
      integerValue = ((incomingByte - 48) + integerValue);
    }
      Serial.println(integerValue);

     }
  }