Hi, I am in urgent need as i doing a final year project and I am stuck on how to implement the next functionality i want on my code
I am trying to achieve 2 things :
1)Press a push button A which starts the my motor and makes it move in one direction whilst taking analog read values at each step and then serial printing that and the angle which is an arithmetic operation of currentstepnumber.
2)Then i press another push button B which will reverse the motor and take it back to the initial position it started from; so kind of like a hardware reset so that i always know my starting point.
So far I have managed to make the motor turn in one direction when i hold pushbuttonA, and I have also made the motor turn in the other direction when i hold PushButton B. I want to change this so I only press PushbuttonA once and it moves in one direction and when I press PushbuttonA again it stops. Then Finally when I press Push buttonB, the stepper turns back to the position it was before push button A was pressed the first time.
I will highly appreciate any help .with this code because i am a beginner and though pseudo code is useful, i find it hard to visualise. Lastly I am using the arduino made motor shield which is based on a L298 full bridge to control the stepper
Thank you
#include <Stepper.h>
int motordelaylength = 5;
unsigned int average;
const int NumAverages = 8;
volatile boolean flag = false;
double currentstepnumberA = 0;
double currentstepnumberB = 0;
float angle;
double stepsPerRevolution = 200;
const int buttonPin = 30;
boolean buttonState = HIGH ;
boolean buttonState2 = HIGH;
int measuredelaylength = 1;
const int buttonPin2 = 31;
int storeAnalogValue() { // fuction to store and average analog read for a 6 times
unsigned long accumulator = 0;
for (int i = 0; i < NumAverages; i++) {
accumulator += analogRead(A8);
}
average = accumulator / NumAverages;
delay(measuredelaylength);
}
void enableChannel(int pinA, int pinB)
{
digitalWrite(pinA, LOW); //ENABLE CH A because brake is off so current passes through coil (set low)
digitalWrite(pinB, HIGH); //DISABLE CH B because brake is on so current passes does not through coil (set high)
}
void motorpinchange()// flag function to show when pin has changed so it can be intterrupted and read
{
flag = true;
}
void setPinDirection(int pin, int direction_pin, int pin_level) // i.e. setPinDirection(12, 8, HIGH)
{
digitalWrite(pin, pin_level); //Sets direction of CH
analogWrite(direction_pin, 255); //Moves CH at speed max speed - 255(-255) spein backwards(100% duty cycle)
}
void setup() {
Serial.begin(115200);
//establish motor direction toggle pins
pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW
pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW
//establish motor brake pins
pinMode(9, OUTPUT); //brake (disable) CH A
pinMode(8, OUTPUT); //brake (disable) CH B
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}
void loop() {
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
if (buttonState == LOW) {
if (currentstepnumberA == 200) {
currentstepnumberA = 0;
}
enableChannel(9, 8); // Enable CH A
setPinDirection(12, 3, HIGH); //Sets direction of CH A and Moves CH A
if (flag) {
flag = false;
detachInterrupt(digitalPinToInterrupt(12));
attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
}
storeAnalogValue();
Serial.println(average);
currentstepnumberA ++;
currentstepnumberB ++;
angle = ((currentstepnumberA) * 360) / (stepsPerRevolution);
Serial.println(angle);
delay(motordelaylength);
enableChannel(8, 9); // Enable CH B
setPinDirection(13, 11, LOW); //Sets direction of CH B and Moves CH B
if (flag) {
flag = false;
detachInterrupt(digitalPinToInterrupt(12));
attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
}
storeAnalogValue();
Serial.println(average);
currentstepnumberA ++;
currentstepnumberB ++;
angle = ((currentstepnumberA) * 360) / (stepsPerRevolution);
Serial.println(angle);
delay(motordelaylength);
enableChannel(9, 8); // Enable CH A
setPinDirection(12, 3, LOW); //Sets direction of CH A and Moves CH A
if (flag) {
flag = false;
detachInterrupt(digitalPinToInterrupt(12));
attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
}
storeAnalogValue();
Serial.println(average);
currentstepnumberA ++;
currentstepnumberB ++;
angle = ((currentstepnumberA) * 360) / (stepsPerRevolution);
Serial.println(angle);
delay(motordelaylength);
enableChannel(8, 9); // Enable CH B
setPinDirection(13, 11, HIGH); //Sets direction of CH B and Moves CH B
if (flag) {
flag = false;
detachInterrupt(digitalPinToInterrupt(12));
attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
}
storeAnalogValue();
Serial.println(average);
currentstepnumberA ++;
currentstepnumberB ++;
angle = ((currentstepnumberA) * 360) / (stepsPerRevolution);
Serial.println(angle);
delay(motordelaylength);
buttonState = digitalRead(buttonPin);
}
if (buttonState2 == LOW) {
// while (currentstepnumberB != 0);
// {
enableChannel(9, 8); // Enable CH A
setPinDirection(12, 3, HIGH); //Sets direction of CH A and Moves CH A
if (flag) {
flag = false;
detachInterrupt(digitalPinToInterrupt(12));
attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
}
storeAnalogValue();
Serial.println(average);
currentstepnumberB --;
angle = ((currentstepnumberB) * 360) / (stepsPerRevolution);
Serial.println(angle);
delay(motordelaylength);
enableChannel(8, 9); // Enable CH B
setPinDirection(13, 11, HIGH); //Sets direction of CH B and Moves CH B
if (flag) {
flag = false;
detachInterrupt(digitalPinToInterrupt(12));
attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
}
storeAnalogValue();
Serial.println(average);
currentstepnumberB --;
angle = ((currentstepnumberB) * 360) / (stepsPerRevolution);
Serial.println(angle);
delay(motordelaylength);
enableChannel(9, 8); // Enable CH A
setPinDirection(12, 3, LOW); //Sets direction of CH A and Moves CH A
if (flag) {
flag = false;
detachInterrupt(digitalPinToInterrupt(12));
attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
}
storeAnalogValue();
Serial.println(average);
currentstepnumberB --;
angle = ((currentstepnumberB) * 360) / (stepsPerRevolution);
Serial.println(angle);
delay(motordelaylength);
enableChannel(8, 9); // Enable CH B
setPinDirection(13, 11, LOW); //Sets direction of CH B and Moves CH B
if (flag) {
flag = false;
detachInterrupt(digitalPinToInterrupt(12));
attachInterrupt(digitalPinToInterrupt(13), motorpinchange , CHANGE);
}
storeAnalogValue();
Serial.println(average);
currentstepnumberB --;
angle = ((currentstepnumberB) * 360) / (stepsPerRevolution);
Serial.println(angle);
delay(motordelaylength);
}
}