hey folks. working on a project, could use some help. I have the arduino motor shield and a 4 wire, 12v bipolar stepper. I modified an example from instructables, it's currently working. When you press a button, the motor moves continuously in one direction, until you release the button, then it sits still. an LED blinks, indicating the speed of the motor.
What i need it to do is:
- if the button isn't pressed, do nothing
- when the button is pressed, move forward a specified amount and stop
- when the button is released, return to start
here's the code I have so far (working on getting a pot to set the speed of rotation, it's not working yet):
/*
For more information see:
https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/
*/
int delaylength = 0;
const int ledPin = 7;
const int buttonPin = 2;
int buttonState = 0;
int potPin = A5;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
//establish motor direction toggle pins
pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
//establish motor brake pins
pinMode(9, OUTPUT); //brake (disable) CH A
pinMode(8, OUTPUT); //brake (disable) CH B
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
delaylength = analogRead(potPin/10);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, HIGH); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A
delay(delaylength);
//digitalWrite(ledPin, HIGH);
digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(13, LOW); //Sets direction of CH B
analogWrite(11, 255); //Moves CH B
delay(delaylength);
digitalWrite(ledPin, LOW);
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, LOW); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A
delay(delaylength);
//digitalWrite(ledPin, LOW);
digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(13, HIGH); //Sets direction of CH B
analogWrite(11, 255); //Moves CH B
delay(delaylength);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
I don't understand why it works well enough to change to code to do what I need. anyone that can explain why it works so I can make it do what I need? I don't understand the channels or the brake etc.