For Loop Code Stepper Motor

Hi all,

So, I am currently trying to drive a stepper motor to tune a guitar string. The stepper motor works fine and I can get it to move in a stepping sequence. The code measures the frequency of the guitar string, compares it to the desired frequency and drives the stepper motor. I have a mathematical relationship that tells me how many steps the motor needs to turn to tune the string.

I have a for loop to iterate the number of steps required to tune the string. So, if the number of steps calculated is for example -12, the motor needs to turn 12 steps in the other direction.

Here is the code for commanding the motor:

if (error < 0); { // Tuning up.
     float steps = ((error)/(0.57)); // Experimental relationship.
     int int_steps = steps; // Float conversion.
     Serial.print("\t");
     Serial.println(int_steps);
   
   for ( j <= 0; j == int_steps; j++) {
     Serial.print("\t");
     Serial.println(j);
     motor.step(j);
     delay(500);

Here is the entire code:

// Input pin for the frequency is pin 8.

#include <FreqMeasure.h>
#include <Stepper.h>

// Frequency variables
unsigned long sum = 0;
int count = 0;
int setpoint = 82;
int i = 0;

// Motor variables
int motorSteps = 48;
int dir_pin = 2; // Direction on pin 2.
int step_pin = 3; // Step on pin 3.
Stepper motor(motorSteps, dir_pin, step_pin);
int steps_int = 0;
int j = 0;




void setup() {
  
  Serial.begin(57600);
  FreqMeasure.begin();
  
}

void loop() {

  if (FreqMeasure.available()) { 
    sum = sum + FreqMeasure.read();
    count = count + 1; 
   
  if (count > 30) { // Number of samples per loop.
    int frequency = FreqMeasure.countToFrequency(sum/(unsigned long)count); // Calculated frequency.
    int error = frequency - setpoint; // Measurement of error.
    Serial.print(frequency);
    Serial.print("\t");
    Serial.print(error);
    
    
  if (error < 0) {
    i = 0;
}
    sum = 0;
    count = 0;
    i = i + 1;
    
   if (i==4) {
     Serial.print("Actual Freq");
     Serial.println(frequency);
     int actfreq = frequency; // Actual measured frequency.
}

   if (error < 0); { // Tuning up.
     float steps = ((error)/(0.57)); // Experimental relationship.
     int int_steps = steps; // Float conversion.
     Serial.print("\t");
     Serial.println(int_steps);
   
   for ( j <= 0; j == int_steps; j++) {
     Serial.print("\t");
     Serial.println(j);
     motor.step(j);
     delay(500);
     
 }

}

}

}

}

For the case of int_steps = -12, the for loop will set j = 0 and the motor will not turn. So, do I need to move it 24 steps so that int_steps = 12? In that case the motor will have tuned beyond the desired frequency. I know I am missing something simple but I just can't figure it out.

Any help would be greatly appreciated.

Thanks,

   for ( j <= 0; j == int_steps; j++) {

The first part of a for loop is an initialization section. j <= 0 is NOT initializing anything.

The second part is a while clause. It makes no sense to use == in a while clause involving a variable that you increment in the third part.

I did not look too far before I found this
      if (error < 0);The trailing semi-colon means that the test is effectively ignored.

PaulS:

   for ( j <= 0; j == int_steps; j++) {

The first part of a for loop is an initialization section. j <= 0 is NOT initializing anything.

The second part is a while clause. It makes no sense to use == in a while clause involving a variable that you increment in the third part.

So, I should say for ( j = 0; j = int_steps; j++) {

UKHeliBob:
I did not look too far before I found this
      if (error < 0);The trailing semi-colon means that the test is effectively ignored.

Thanks. I didn't know that the semi-colon would do that.

So, I should say

No. The middle clause is a while clause. Assigning a value to j in the while clause is wrong.

Even if you make it

for ( j = 0; j == int_steps; j++)

it's still wrong.

That statement says "starting with j equal 0, while j equals int_steps, do something and then increment j.". Does THAT seem like what you want? I don't think so.

It seems to me that you want:

for ( j = 0; j < int_steps; j++)

By the way, there is no excuse for making j global.