Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Need help with step...
 
Share:
Notifications
Clear all

[Solved] Need help with stepper motor code

2 Posts
2 Users
0 Likes
1,131 Views
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

  Want to add a speed control to this code but having trouble add code and wiring  

#define DISTANCE 100

int StepCounter = 0;
int Stepping = false;

void setup() {
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);

  pinMode(2, INPUT);
  pinMode(3, INPUT);
}

void loop() {
  if (digitalRead(3) == LOW && Stepping == false) {
    digitalWrite(8, LOW);
    Stepping = true;
  }
  if (digitalRead(2) == LOW && Stepping == false) {
    digitalWrite(8, HIGH);
    Stepping = true;
  }

  if (Stepping == true) {
    digitalWrite(9, HIGH);
    delay(1);
    digitalWrite(9, LOW);
    delay(1);

    StepCounter = StepCounter + 1;

    if (StepCounter == DISTANCE) {
      StepCounter = 0;
      Stepping = false;
    }
  }
}

 

 

 

 

 

 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2674
 

Hi Nerder,

not sure if anyone else will chime in. My experience with stepper motors is pretty much zero, but maybe I can assist in getting you in the right direction.

I assume you would to use a potentiometer (variable resistor) to set the speed?

In that case we first need to find out how to connect (source: Arduino Analog Input):

Note: I have written an article once on how to use an LDR (light sensitive resistor), in which I do talk a little bit about a potentiometer as well. May be a good additional read.

In short: We will use A0 for reading the analog value of the potentiometer. We need to connect GND and +5V to the potentiometer as illustrated - you can swap +5 and GND on the potentiometer - it will just change its behavior from ascending to descending and vice versa (R1 and R2).

 

Next we need to read the value of pin A0 in our code:

int potentiometerValue;

...

potentiometerValue = analogRead(A0);

...

 

For this to be "responsive" (respond in a timely fashion) our code will need to read this value very frequently (in loop()).

Based on the value we get from A0, we need to slow down the original code. I see a few potential spots, and either way we will use the "delay()" function.

void loop() {
  if (digitalRead(3) == LOW && Stepping == false) {
    digitalWrite(8, LOW);
    Stepping = true;
  }
  if (digitalRead(2) == LOW && Stepping == false) {
    digitalWrite(8, HIGH);
    Stepping = true;
  }

  if (Stepping == true) {
    digitalWrite(9, HIGH);
    delay(1);  
    digitalWrite(9, LOW);
    delay(1);  // <---- HERE ??

    StepCounter = StepCounter + 1;

    if (StepCounter == DISTANCE) {
      StepCounter = 0;
      Stepping = false;
    }
  }
}

 

When the motor runs at its highest speed, you want a delay of "1". At its slowest speed this should be a higher value, but what that value should be, depends on your application of course.
The value from the analog pin is between 0 and 1024. So this could translate to 0-1024 milliseconds delay for slowing down the rotation - which could be fine (1000 milliseconds = 1 second).

If you would like to use a different range, then you could use the "map" function, which takes an input value, inputrange and outputrange.
So lets say you wanted a 0 - 199 range, derived from the values we just read (0-1023) then this could be done like so:

delayMs = map(potentiometerValue, 0, 1023, 0, 199);

and we'd change the loop code to something like this (untested!!!):

...
int potentiometerValue;
int delayMs;
...

void loop() {
  potentiometerValue = analogRead(A0);
  delayMs = map(potentiometerValue, 0, 1023, 0, 199);

  if (digitalRead(3) == LOW && Stepping == false) {
    digitalWrite(8, LOW);
    Stepping = true;
  }
  if (digitalRead(2) == LOW && Stepping == false) {
    digitalWrite(8, HIGH);
    Stepping = true;
  }

  if (Stepping == true) {
    digitalWrite(9, HIGH);
    delay(1);  
    digitalWrite(9, LOW);
    delay(delayMs);

    StepCounter = StepCounter + 1;

    if (StepCounter == DISTANCE) {
      StepCounter = 0;
      Stepping = false;
    }
  }
}

 

Hope this helps getting you started. 😊 

 

 


 

While trying to find you an answer, I did run into this stepper speed control example on the Arcuino website.
You may want to look at that one since it is using a potentiometer and a stepper motor - it even has a nice library (stepper.h) which may mike life easier.

Their code:

/*
 Stepper Motor Control - speed control
 by Tom Igoe
 */

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;  // number of steps the motor has taken

void setup() {
  // nothing to do inside the setup
}

void loop() {
  // read the sensor value:
  int sensorReading = analogRead(A0);

  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);

  // set the motor speed:
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
  }
}

 


   
ReplyQuote
Share: