Hi !
Fisrt of all i apologize for my bad english. ( By the way I'm not sure if I'm on the good section )
My friend and I are building a Motorized Camera Crane. My friend is good at welding and stuff, and I'm studying electronics at school.
So i've already made some Stepper controllers with PLDs, but i wanted to use an arduino for this project ( easier to upgrade the whole thing than with PLDs )
The frame is near finished, and i'm stuck at programming it.
I don't Know what motor i'll use, unipolar or bipolar, but now i'm designing the speed control interface.
So ! I've got a potentiometer ( will be replaced by a joystick ) , a led to visualize the direction ( on = CW , OFF = CCW )
I've also got a buzzer to hear the sound ( and visualize the signal on my oscilloscope )
I want the the frequency to go up with the absolute value of my pot ( the direction led is actived by whether or not my pot is not on the center ( by using "if else" ) )
I don't want to use the STEPPER library, because it uses the "delay" fonction, and gives latency on the control of frequency. Maybe if i tweaked it ?
So i used the " blink without delay " sketch to create my frequency.
Here's the code :
float sensorVal = 0; // store the value coming from the sensor
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 7; // select the pin for the LED
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
void setup() {
// declare the ledPin as an OUTPUT
pinMode(ledPin, OUTPUT); //
}
void loop()
{
// read the value from the sensor:
sensorVal = analogRead(sensorPin); // Reading the joystick
sensorVal = map(sensorVal, 0, 1024, -500, 500); // setting the "0"
sensorVal = 2000/ sensorVal; // F=1/t with a multiplier to reduce the max freq
// Generating the temporary clock for the future stepper library
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > abs(sensorVal)+1) // Abs() gives me the absolute speed
{
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin, ledState); //End of the clock
}
}
It works like a charm at low speeds ( unlike with stepper lib )
but at high speeds, the frequency's not constant ( i can hear it and mesure it with the oscilloscope )
I tried to " microseconds() " but whithout good results ( like the frequency going really high and random when the pot is near 0 )
Thanks you in advance !
Pictures soon