Go Down

Topic: Arduino uno R3 stepper motor control (Read 12466 times) previous topic - next topic

JimboZA

ps.... you would also want to declare a variable to hold the result of your digitalRead, and use that value (say mySwitchVal) in the test of the if/else.

I am writing a short sketch to do this as we speak, but I'm reluctant to post it until you go first  8)
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

Robin2

I found this tutorial re Arduino and EasyDriver employing 3 push buttons for forward, stop and reverse plus potentiometer for speed on the fly. Included also is a schematic diagram.
I'm delighted you have found something that works for you.

But, to be honest, I don't understand how the link you gave is any different from the combination of my code and the diagram on the Pololu A4988 page.

And I guess my frustration has been my inability to find the key that would help me to bridge whatever gap exists between what I write and your understanding of what I write. I suppose I expect people who don't understand to respond with lots of questions that illustrate that gap - and you will have noticed from earlier posts that for a long time I was not even sure if you had read my stuff.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

neksmerj

Hi Robin2,

I need to be hand fed at this stage until I'm up to speed. The tutorial I linked to shows the code and a pictorial diagram that I can relate to. 

http://www.schmalzhaus.com/EasyDriver/Examples/EasyDriverExamples.html

Your link to the Polou A4988 shows a schematic only, no code.

I wouldn't know how to hook your board up to an Arduino, and how to control direction and speed.

I haven't reached that level of knowledge yet. I'm ploughing through as many tutorials as I can find, it seems to be the only way to learn.

JimboZA

I vividly remember the two days years ago when I let go of the saddle on my kids' bikes and all of a sudden they could ride a two-wheeler with no fairy wheels. But I was still jogging behind just for in case.....

I'm pretty sure you remember those days too neksmerj, yet you seem reluctant to let go of your own saddle so to speak.

My advice: try to write the code as I suggested a few posts ago, to get (and this is merely a suggestion) one or other led on when a switch is pressed or not. Post it here by all means for comment, but the time has come where you have to do that.

Quote
I'm ploughing through as many tutorials as I can find, it seems to be the only way to learn.
There's only one way to find out what you have learned, and that's to try a sketch. Right now, you're like Sheldon in TBBT when he said he learned to swim on the floor of the apartment.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

neksmerj

#49
Nov 16, 2014, 06:13 am Last Edit: Nov 16, 2014, 08:13 am by neksmerj
JimboZA,

I have set up a bread board with 4 leds to represent the 4 windings of a stepper motor.
Each led is connected via resistors to the Arduino board to pins 8, 9, 10 and 11.

Credit goes to David Cuartielles for a very basic program that cycles through each led with a pause in between.

To take it from here, I would like to be able to vary the speed with a pot, and be able to reverse the direction.

His cut down code is here

int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
int delayTime = 500;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
}

void loop() {
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delay(delayTime);
}
 This I can follow without much trouble.

I have fallen off my bike many times, but that was a long, long time ago........

Ken

JimboZA

This I can follow this without much trouble.

To take it from here, I would like to be able to vary the speed with a pot, and be able to reverse the direction.
Well taking the speed thing first, have you looked at this?

And for the direction, you could do what I suggested earlier: run either leg of an if/else based on the switch's value. One leg has the code you have currently in loop(), the other has very similar code but with those highs and lows in the digitalWrites re-jigged to go in the opposite direction.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

neksmerj

Hi JimboZA,

Let's see your version of using the if/else statement, I've had my little go.

I've managed to control the speed of the sequential leds using a pot but haven't yet figured how to reverse the sequence.

Ken














Robin2

#52
Nov 16, 2014, 11:06 am Last Edit: Nov 16, 2014, 11:10 am by Robin2
I have set up a bread board with 4 leds to represent the 4 windings of a stepper motor.
Each led is connected via resistors to the Arduino board to pins 8, 9, 10 and 11.

Sorry -  but this is sheer folly.

Your Arduino will NOT be connecting to motor coils. That is the job of the stepper driver. You have no need to know what goes on inside the stepper driver.

The Arduino just needs to control the stepper driver by setting the direction pin for Clockwise or CounterClockwise and producing a pulse on the step pin whenever you want the motor to move one step in the selected direction.

This is what my code has been illustrating all along. My first simple code is deliberately very simple. I think part of the "cycling without stabilizers experience" is just to sit down and study my code (100 times if necessary) until you do understand it. And ask as many "stupid" questions about it as you like. Learning the code will also help you to learn about stepper motors.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

neksmerj

Oh dear oh dear Robin2,

Now I'm wondering whether you have read the above threads/posts etc.

I fully realise that in the real world a stepper motor will require a stepper motor board, and the code will be different.

The above excercise was a challenge started by JimboZA to simply simulate a stepper using leds run straight off the Arduino board.

Ken

JimboZA

#54
Nov 16, 2014, 12:34 pm Last Edit: Nov 16, 2014, 12:35 pm by JimboZA
Ken-

Let's see your version of using the if/else statement, I've had my little go.
My code from yesterday looks like the below: two leds and a switch, as shown in the attached schematic.

Code: [Select]
//read a switch and if it's high,
//    put a led on. If it's low,
//    put a different led on instead.
//LEDs are on pins 5 and 6, with the anodes
//    to 5V and cathodes to the pins. So, the
//    leds come on when their i/o pins are low
//The switch is on pin 10 with pullup enabled,
//    so pin 10 is usually high but goes low
//    when the switch is pushed.
//Serial is enabled and the value of the pin
//    is shown in the monitor each time thru loop()

byte ledOne=5; // a byte is the smallest type that can store a number
byte ledTwo=6;
byte mySwitch = 10;
bool mySwitchVal; // it can only be on or off so bool is cool


void setup() {
 Serial.begin(9600);
 pinMode(ledOne, OUTPUT);
 pinMode(ledTwo, OUTPUT);
 pinMode(mySwitch, INPUT_PULLUP);

}

void loop() {
mySwitchVal= digitalRead(mySwitch);
Serial.println(mySwitchVal);

if (mySwitchVal == 1)  // note the == not just a =
{
 digitalWrite(ledOne, HIGH);
 digitalWrite(ledTwo, LOW);
}
else // if not 1, it must be 0
{
 digitalWrite(ledOne, LOW);
 digitalWrite(ledTwo, HIGH);
}

}
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

neksmerj

Thanks JimboZA,

At my next opportunity, ie, daylight, I'll whack your code in and see what it does, looks interesting.

Ken

Robin2

Oh dear oh dear Robin2,

Now I'm wondering whether you have read the above threads/posts etc.
I've put a lot into this. Now, and I'm out of here.

@KenF I hope your patience is more elastic than mine.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

neksmerj

#57
Nov 18, 2014, 09:41 am Last Edit: Nov 18, 2014, 12:09 pm by neksmerj
For no apparent reason, the port comm3 has changed to comm1, and sample sketches that were running ok, now don't.

Under the "Tools" menu, comm1 appears with no provision to change it to comm3.

When I first started to play with the Arduino, I had the choice, not now.

Any clues?

Ken

edit, looking at device manager, and clicking on Ports (com & lpt)
Arduino uno reports com3

When trying to run a sketch, the bottom dialogue box reports com1.

The sketch compiles but will not run

neksmerj

I accidentally found a fix. In Device Manager, I uninstalled Arduino UNO com1 port, then plugged my Arduio UNO board in.

Device manager then found the board and assigned com3 to it.

I uploaded the sketch, and it failed saying "could not find port com1, try another port", so I selected com3 and wallah.

Ken

Go Up