I would like to control speed with a potentiometer but when i configure it with the diagram and script below make a noise and does not really work any help would be great
Thanks
Steven
const int dirPin = 2;
const int stepPin = 3;
int led2 = 7;
int led = 4;
int sw = 5;
int sw2 = 6;
int stepdelay;
// const int stepsPerRevolution = 800;
void setup()
{
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(sw, INPUT);
pinMode(sw2, INPUT);
}
void loop()
{
int val=analogRead(A0);
stepdelay = map(val,0,1023,-10,100);
// Set motor direction clockwise High = counter clockwise
//digitalWrite(dirPin, LOW); // moves to the counter closewise (Light Goes On)
//digitalWrite(dirPin, HIGH);// moves counter clockwise (Light Goes Off)
//digitalWrite(sw, HIGH);
//digitalWrite(3, LOW);
//delay(0);
//digitalWrite(3, LOW);
//delay(0);
if (digitalRead(sw2) == LOW) //direction and speed
digitalWrite(stepPin, HIGH);
digitalWrite(dirPin, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(stepPin, LOW);
digitalWrite(dirPin, HIGH);
delayMicroseconds(stepdelay);
if (digitalRead(sw2) == LOW) //led
digitalWrite(led2, HIGH);
if (digitalRead(sw2) == HIGH)
digitalWrite(led2, LOW);
if (digitalRead(sw) == LOW) //direction and speed
digitalWrite(stepPin, HIGH);
digitalWrite(dirPin, LOW);
delayMicroseconds(stepdelay);
digitalWrite(stepPin, LOW);
digitalWrite(dirPin, LOW);
delayMicroseconds(stepdelay);
if (digitalRead(sw) == LOW) //led
digitalWrite(led, HIGH);
if (digitalRead(sw) == HIGH)
digitalWrite(led, LOW);
}
Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
if (digitalRead(sw2) == LOW) //direction and speed
digitalWrite(stepPin, HIGH); // only this like executes conditionally
digitalWrite(dirPin, HIGH); // the rest of these lines execute every
delayMicroseconds(stepdelay); // time through loop()
digitalWrite(stepPin, LOW);
digitalWrite(dirPin, HIGH);
delayMicroseconds(stepdelay);
If you use the IDE autoformat tool to indent the code you will see that there may be curly brackets ({}) missing from some if blocks, like the one above. Only the very first line of code is executed conditionally after the if, not the whole block. Is that what you intended?
What do you expect a delay of -10 to do?
You are trying to step the motor way too fast. Try longer delays. No stepper will go to 5K steps per second from a dead stop. You will need acceleration to achieve high speeds. Like offered by the AccelStepper or MobaTools stepper libraries.
Did you set the coil current setting on the stepper driver?
Please hand draw your circuit and include component names, pin labels and all power supplies.
Your Fritzy unfortunately gives us little or none of this information.
Have you got the wires from the stepper connected the proper way to the driver PCB?
Do you have a DMM?
Can you please post a link to specs of your stepper motor?
Try longer pulse steps, milli-seconds not micro-seconds. Your step pulses may be too short to actually transfer any energy to the stepper windings.
What speed do you want from your stepper?
It looks like you want to turn in one direction with the 'led' pin HIGH if the 'sw' pin reads LOW and turn the other directionwith the 'led2' pin HIGH if the 'sw2' pin reads LOW.
That would be something like:
void loop()
{
int val = analogRead(A0);
// Map to 1000 to 5 steps per second
stepdelay = map(val, 0, 1023, 500, 100000);
if (digitalRead(sw) == LOW) //direction and speed
{
digitalWrite(dirPin, LOW);
digitalWrite(led, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepdelay);
}
else
digitalWrite(led, LOW);
if (digitalRead(sw2) == LOW) //direction and speed
{
digitalWrite(dirPin, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepdelay);
}
else
digitalWrite(led2, LOW);
}
I want to set pre determined speed with the potentiometer switch,
Then press one button for one direction or the other for the other direction.
I add the LED's to show the direction.
The switches are there to simulate limit switches when the carriage reaches the end it stops until then you can push the other button in the other direction and so on
works in my wokwi arduino simulator will try on the devices later
here is my project goal
I want to set pre determined speed with the potentiometer switch,
Then press one button for one direction or the other for the other direction.
I add the LED's to show the direction.
The switches are there to simulate limit switches when the carriage reaches the end it stops until then you can push the other button in the other direction and so on
A 5volt supply is not enough for most stepper drivers.
The A4988 needs a minimum of 8volt.
12volt is a practical value, and 24volt is better if you want torque at higher speed.
Post the model number (voltage/current) of your motor (not it's size) to make sure the driver and supply are right for that motor.
You should use a stepper driver library, so micro-stepping acceleration and deceleration are properly executed.
Leo..