Processing + Arduino + Steppers issue

I made a little code to control my steppers from the arrow keys, but it is not working the way it should.

when it receives the char ‘1’ the stepper makes one revolution in one direction and one revolution in the other direction, 5 times. When send ‘2’ it takes 8 revolutions in a single direction.

i tried different things, like changing the direction of the stepper between the two if statements, but always the first if makes the back and forth motion and the second if statement makes 8 revolutions in a single direction.

The steppers and drivers work, because the stepper examples no not have any problems.

my arduino code:

char val;

#include <Stepper.h>
const int x = 200;
const int y = 75;
Stepper m1(x, 38, 42, 46, 50);
Stepper m2(x, 22, 24, 26, 28);

 void setup() {
   
   Serial.begin(9600); 
   m1.setSpeed(y);
   m2.setSpeed(y);
 
 }


 void loop() {
   if (Serial.available()) 
   { 
     val = Serial.read(); 
   }
   if (val == '1') 
   { 
     m2. step (-x);
     
     delay(10);
   
   } else if (val == '2'){
     
     m2. step (x);  
     delay(10);
 }
   
   
 }

and the processing code:

import processing.serial.*;

Serial myPort;  
String val;     

void setup()
{
  String portName = Serial.list()[1]; 
  myPort = new Serial(this, portName, 9600); 
}
  
  
void draw() {
  switch (keyCode) {
    case 37:
    if (keyPressed == true) {
    myPort.write('1'); 
  } 
  else {
    myPort.write('0');  }
           
    case 39:                        
       if (keyPressed == true) {
    myPort.write('2'); 
  } 
  else {
    myPort.write('0');   }
    break; 
    default: 
      break;
}}

when it receives the char '1' the stepper makes one revolution in one direction and one revolution in the other direction, 5 times. When send '2' it takes 8 revolutions in a single direction.

I can't see anything in either code that makes it do this - but I am not familiar with Processing.

Have you tried controlling the Arduino from the Serial Monitor so as to eliminate the possibilty of an error in your Processing code.

Have you tried making the motor move a fixed number of steps without any need for serial input?

When you have problems eliminate all the possible sources of error, start from the simplest thing and cautiously add extras.

...R

You REALLY need to learn to put each { on a new line AND to indent your code properly. That Processing code is just a mess. EVERY } belongs on a line ALL BY ITSELF.

You also need to look at sending a value from Processing ONLY when the state of the key press changes. Right now, you are sending a stream of data while the key is pressed and another stream of data when the key is released. You should be sending ONE character when a key BECOMES pressed and one when the key BECOMES released.

Robin2 the Processing code was to blame it was sending both '1' and '2' in " case '1' " , i do not know why.And as PaulS pointed out it was sending a stream of this.

This new code is working as long as i do not hold down the key. It is good enough for me.

PaulS you are right, but i don't know how to do that so i did this instead:

import processing.serial.*;

Serial myPort;  
char val;     

void setup()
{
  String portName = Serial.list()[1]; 
  myPort = new Serial(this, portName, 9600); 
}
  
  
void draw() {
  
    
  } 
  
  void  keyPressed() 
{
    if (key == '4') 
    {
     myPort.write('1'); 
     println('1');
    }
   if (key == '6') 
   {
     myPort.write('2'); 
     println('2');
    }
}

void  keyReleased()

   {
   myPort.write('3');
   println('3');
   }

I guess you could write your Processing code so it only sends a 1 if the previous character was 2 and vice versa.

...R

Original Processing code lacked a break between every switch-case.

Original code has an uninitialised variable called val, its initial value
should have been defined.

Testing a boolean variable for equality with true is redundant:

  if (var == true)   
  { .. }

// same as
  if (var)
  { .. }

Now i am trying to control the speed of the steppers, but i don’t think i am doing it the right way:

char val;
  int a=1;
  int z ;
#include <Stepper.h>
 int x = 200;
const int y = 75;
Stepper m1(x, 38, 42, 46, 50);
Stepper m2(x, 22, 24, 26, 28);

 void setup() {
   z=y*a;
   Serial.begin(9600); 
   m1.setSpeed(z);
   m2.setSpeed(z);
 
 }


 void loop() {

    if (Serial.available()) 
   { // If data is available to read,
     val = Serial.read(); // read it and store it in val
   }
   if (val == '1')
    {
    m2. step (x);
    }
   
  if(val == '2')
   { 
   m2. step (-x);  
   }
  if(val == '4')
  {
    a++;
  }
  if(val == '5')
  {
    a--;
  }
  delay(10); 
}

thusarix:
Now i am trying to control the speed of the steppers, but i don't think i am doing it the right way:

Can you explain how you are trying to do it?

...R

Stepper motor basics
Simple stepper code

Robin2:
Can you explain how you are trying to do it?

the theory is :

i set m1.setSpeed(z)and m2.setSpeed(z) and the integer z is equal to integer y plus integer a.

When i receive the ‘4’ char the a integer should increment by one and when i receive the char ‘5’ the a integer should decrement by one.

this is the last code that is not working:

char val;
  int b=0;
  int a=1;
#include <Stepper.h>
 int x = 200;
 int y = 75;
Stepper m1(x, 38, 42, 46, 50);
Stepper m2(x, 22, 24, 26, 28);
 int z =y+b;
 void setup() {
  
   Serial.begin(9600); 
   m1.setSpeed(z);
   m2.setSpeed(z);
   
 
 }


 void loop() {

    if (Serial.available()) 
   { // If data is available to read,
     val = Serial.read(); // read it and store it in val
   }
   if(val == '4')
  {
    a++;
  }
  a=b;
  if(val == '5')
  {
    a--;
  }
  a=b;
   if (val == '1')
    {
    m2. step (x);
    }
   
  if(val == '2')
   { 
   m2. step (-x);  
   }
  
  delay(10); 
  
}

Whatever you do to variable a in the if statement, is thrown away afterwards by your assignment a=b;

  if(val == '4')
  {
    a++;
  }
  a=b;

It's sort of like saying, "If it's Tuesday today, I'll cook your meatloaf... on second thought, you get ham".

Removing the "a=b" lin does not help. I tought that if i do not save the new a value after the increment, the loop will set it back to the global value.

I tought that if i do not save the new a value after the increment, the loop will set it back to the global value.

No. Even if it was true, the value to be saved is not the value on the left side of the equal sign.

Enough with the one letter names. They don't mean squat. leftMotorVal, speed, etc. convey information, a, b, x, y, and z do not.

If i want to write a letter first i start to learn to write and after that learn to format the writing the proper way. I found quite a few threads here in which a newbie ask a question and instead of getting an answer or at least a push in the right direction he gets a rant about the format of his code. I get that this code hurts your eyes,i am sorry about that. Thank you for your advice and in the future i will try to write the code the way it "should" be written.

Anyway as i said removing that line did no solve the code. Any thoughts about my problem?

The structure of the code is an essential part of its nature, its the most fundamental
way we "read" code and understand it. Bad indentation is basically unreadable to
experienced programmers, and its called "obfuscated code"

thusarix:
he gets a rant about the format of his code.

The reason code fails to work is because there is something wrong with it.

It is much easier to see what is wrong if the variables have meaningful names and if the code is formatted to show the different parts (FOR, IF etc) clearly. We do not suggest a style because we are closet artists.

This code from Reply #8

 void loop() {
   if (Serial.available()) 
   { // If data is available to read,
     val = Serial.read(); // read it and store it in val
   }
   if(val == '4')
  {
    a++;
  }
  a=b;
  if(val == '5')
  {
    a--;
  }
  a=b;
   if (val == '1')
    {
    m2. step (x);
    }
  if(val == '2')
   { 
   m2. step (-x);  
   }
  delay(10); 
}

can't possibly do anything useful.

Part of it changes the value of variables a and b (whatever they are supposed to represent) and the other part of it uses a variable called x.

It's like having two cups of coffee, adding milk to one of them and then wondering why you burn your mouth with the other cup.

If you use meaningful variable names this sort of issue will be perfectly obvious.

...R

Robin2:
The reason code fails to work is because there is something wrong with it.

Now you are making fun of me.

Maybe the indentation is at fault.

Yes there is something wrong with it. Can you tell me what?

My last try:

char val;
  
#include <Stepper.h>
 int stepsPerRevolution = 200;
 int rpmMotor = 75;
 int incrementor = 0;
Stepper m1(stepsPerRevolution, 38, 42, 46, 50);
Stepper m2(stepsPerRevolution, 22, 24, 26, 28);
 
 void setup() {
  
   Serial.begin(9600); 
   m1.setSpeed(rpmMotor);
   m2.setSpeed(rpmMotor);
   
 
 }


 void loop() {

    if (Serial.available()) // If data is available to read,
   { 
     val = Serial.read(); // read it and store it in val
    
   }
   
   if (val == '1')
    {
    m2. step (stepsPerRevolution);
    }
  
  if(val == '2')
   { 
   m2. step (-stepsPerRevolution);  
   }
    for(val = Serial.read();val == '4';incrementor++ )
  {
    rpmMotor = rpmMotor + incrementor;
  }
  for(val = Serial.read();val == '5';incrementor--)
  {
    rpmMotor = rpmMotor + incrementor;
  }
  
   
  delay(10); 
  
}
    for(val = Serial.read();val == '4';incrementor++ )
  {
    rpmMotor = rpmMotor + incrementor;
  }

Explain, in English, what this for loop is doing, and why you are misusing a for loop this way.

Because if i had somebody to tell me:"look do this, because of this" i would do that...but if nobody it telling me nothing .I type the code that i think, with my limited understanding of programming, is allowing me to get the job done. So here i am typing code and praying that it will work... this is more of a journal of my failures than a thread to find a solution it appears.

At least i am learning how to format my failing code. How does it look? Better?

Because if i had somebody to tell me:"look do this, because of this" i would do that...but if nobody it telling me nothing .I type the code that i think, with my limited understanding of programming, is allowing me to get the job done.

It's understandable that you are trying things. But, you do not appear to have an understanding of even the simplest things. Like, what a for loop is supposed to do. It is designed to repeat a block of code a fixed number of times. There are cases where a for loop is used to repeat a block of code until some condition is met, but that is better done using a while loop.

Your code says "Starting at the value read from the serial port, while that value is 4, do this, and then increment the value of incrementor.

Your loop will never end, if it gets started, because val never changes once the loop starts.

So, I'll ask you again to explain in English, NOT IN CODE, what you are trying to do. It is obvious that you are having trouble converting what you want to do into code. We could help you with that IF we has any idea what you were trying to do.

“Your loop will never end, if it gets started, because val never changes once the loop starts”
Why would it never end? my processing code it sending char ‘4’ when i press the up key and whenever i release any key it sends char ‘3’ , so val does not stay the same.

#include <Stepper.h>
char val;
int rpmMoto = 75;
int stepsPerRevolution = 200;
Stepper m1(stepsPerRevolution, 38, 42, 46, 50);
Stepper m2(stepsPerRevolution, 22, 24, 26, 28);
 
 void setup() {
   Serial.begin(9600); 
   m2.setSpeed(rpmMotor);
   
               }
 void loop() 
   {
    if (Serial.available()) // If data is available to read,
   { 
     val = Serial.read(); // read it and store it in val
   }
   
   if (val == '1')
    {
    m2. step (stepsPerRevolution);
    }
  
  if(val == '2')
   { 
   m2. step (-stepsPerRevolution);  
   }
    if(val == '4' )
  {
    rpmMotor++;
  }
   if(val == '5' )
  {
    rpmMotor--;
  }
  delay(10); 
  
}

I am trying to increment the rpmMotor value each time a ‘4’ char is received and decrement it when a ‘5’ char is received.

Now my motor in running at 75 rotations per minute(that is what rpmMotor is), after i receive the ‘4’ / ‘5’ char from the processing code i want the rotations per minute of the motor to increase/decrease by 1.

Please explain to me why the rpmMotor value does not increment with this code.