-2

I am trying to write a program to control a stepper, and incorporate a selector switch and a rheostat.

The object is to make the stepper oscillate, and to be able to control the speed with the rheostat, and the amount of oscillation with the selector switch.

What would the best way to go about this?

4
  • 7
    By writing a program to do it. Commented Aug 30, 2016 at 19:53
  • So you want to use the rheostat as an analog controller then?-If so your program would use the analog pins. Uisng the info gathred from the analog pins you can control the stepper motor, though you may need to build a h bridge or use something similar Commented Aug 30, 2016 at 20:14
  • 1
    A stepper motor may well not be the right choice Commented Aug 30, 2016 at 22:39
  • "The object is to make the stepper oscillate" if you mean you want to make the stepper go back a fixed number of degrees in one direction then go foward a fixed number of degrees in the opposite direction-then yes a stepper with control circuit can do this. My interpretation of your question is your are using select switch to vary degrees in an oscillation and rheostat as analog controller for speed of oscillation. Right? Commented Aug 31, 2016 at 0:26

2 Answers 2

3

Majenko provided the correct answer in his comment: you just have to write a program to do it! This site is not meant to provide you with a ready made solution, but to give help if you run into an issue while trying to solve the problem yourself.

That being said, before you attempt to write the program, you should learn how to interface with the hardware you are using. Here are the relevant Arduino tutorials:

And then there is plenty of material available on the Web for each of these topics.

Go through the tutorials, understand the programs given as examples, make them work with your hardware, then put the pieces together to solve your problem. If you run into an issue you cannot solve buy yourself, then come back here with a specific question and show us what you have attempted so far, including code and schematic. See also How do I ask a good question?.

1

Here are the code fragments that would help you.

Connect the three wires from the potentiometer to your board. The first goes from one of the outer pins of the potentiometerto ground. The second goes from the other outer pin of the potentiometer to 5 volts. The third goes from the middle pin of the potentiometer to the analog pin A0.

Here is the connection schematic:

enter image description here

Here is the code to read the analog value from port A0:

int sensorValue = analogRead(A0);

this would assign a value ranging fro 0-1023 to sensorValue according to the position of the Pot.

int stepsPerRevolution = 200; 
//this variable holds the no of steps the motor rotates to complete a full circle rotation.

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

stepsPerRevolution = map(sensorReading, 0, 1023, 0, 200); 

// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);

Here is an example program for controlling the direction of stepper using microswitches:

#define stepPin 2
#define dirPin 3
#define pinLimitswitchLeft 4  // these can be either left or right depending on motor wiring and dir pin.
#define pinLimitswitchRight 5 // these switches must be N.O. types
int stepDelay = 100; // this is the step delay in microseconds. Reduce this to speed up motor rotation and vice versa.

void setup()
{
    pinMode (stepPin, OUTPUT);
    pinMode (dirPin, OUTPUT);
    pinMode (pinLimitswitchLeft, INPUT_PULLUP);
    pinMode (pinLimitswitchRight, INPUT_PULLUP); 
}
    
void loop()
{
    digitalWrite(dirPin, HIGH);  // set direction pin, the direction that the motor will rotate is dictated by it's wiring
    while (digitalRead(pinLimitswitchLeft) == HIGH) // move motor until left limit switch is pressed.
    {
        stepMotor(); 
    }
    
    digitalWrite(dirPin, LOW);  // set direction pin, the direction that the motor will rotate is dictated by it's wiring
    while (digitalRead(pinLimitswitchRight) == HIGH) // move motor until right limit switch is pressed.
    {
        stepMotor(); 
    }
    
}
    
void stepMotor()
{
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(stepDelay);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(stepDelay); 
}

Change the control from the control switch to the number of steps taken by your motor when the switch is pressed and it will work as you require.

I have not included the header files.

Please edit the program as you require

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.