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,