Go Down

### Topic: speed control question. (Read 80 times)previous topic - next topic

#### dthammer181

##### Oct 02, 2019, 12:11 pm
Hello.  Complete newbie to Arduino.  I am working a project and trying to learn all I can.  I was able to come up with this simple sketch from putting together pieces from sketches I found online,  There will be a lot more when I'm done for sure.  For now I'd like to add some some different speeds to this sketch.  How do I go about doing that?  I have pasted the sketch below. Thank you.

const int dirPin = 2;  // Direction
const int stepPin = 3; // Step

// Motor steps per rotation
const int STEPS_PER_REV = 200;

void setup() {

// Setup the pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);

}

void loop() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 400 pulses for making one full cycle rotation
for(int x = 0; x < 10000; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000); // One second delay

digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < 10000; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
}

#### DangerToMyself

#1
##### Oct 02, 2019, 12:25 pm
In that sketch, you would need options for delayMicroseconds and a method for selecting those options.

So, you'd have something like

if medium is selected
x = 500
If slow is selected
x = 750

Then you would delayMicroseconds(x);

Of course selecting the speed is a whole other can of worms. You could opt for a button (or buttons), a potentiometer, base it on temperature, light brightness, etc etc.

Go Up