Hi, after much help from some members on here and a lot of trial and error I have my code working as I want it but as always enough is never enough so there is one last function I want to add but can't get it to work.
So an overview of what it does.
I have several functions that are called using either a android app or a variety of push sequences on 2 push buttons. These functions control 2 stepper motors.I have 1 other button that when pressed stops the motor and rotates it in the other direction slightly(basicallya limit switch) .
All works fine, I sure its not the best written or most compact code ever but it works and I understand it.
My problem lies when using the app. I can call all the functions as required but I can't stop them mid way through like i can with the limit switch.
I want to be able to press a button on the app and instantly stop the motors.
the main parts of the code are as follows.
OneButton button1(4, true);
OneButton button2(5, true);
int turnMotor = 9;
int slideMotor = 10;
int motorPin1 = 14;
int motorPin2 = 12;
int motorPin3 = 13;
int motorPin4 = 15;
int limitSwitch = 16;
float c ;
int delayTime = 2;
bool stopNow;
void turnOn(String deviceId) {
if (deviceId == DEVICE1)
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
turnPart(); //call function turn part
}
else if (deviceId == DEVICE2)
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
turnFull(); //call function turn full
}
else if (deviceId == DEVICE7)
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
stopNow = true;
digitalWrite(limitSwitch, LOW);
delay(1000);
digitalWrite(limitSwitch, HIGH);
}
}
void turnOff(String deviceId) {
if (deviceId == DEVICE1)
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
//no fuction needed
}
else if (deviceId == DEVICE2)
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
//no fuction needed
}
else if (deviceId == DEVICE7)
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(limitSwitch, HIGH);
}
}
void setup() {
Serial.begin(115200);
stopNow = false;
button1.attachClick(turnPart);
button1.attachDoubleClick(turnFull);
button1.attachLongPressStop(turnClose);
button2.attachClick(slidePart);
button2.attachDoubleClick(slideFull);
button2.attachLongPressStop(slideClose);
pinMode(limitSwitch, INPUT_PULLUP);
digitalWrite(limitSwitch, HIGH);
}
void loop() {
button1.tick();
button2.tick();
}
void turnPart() // turnMotor part
{
for (int i = 0; i < 750; i++)
{
CWTP();
// Exit from the loop if the limit switch has been pressed
if (digitalRead(limitSwitch) == LOW || stopNow = true;)
{
// Hit clockwise limit so move anticlockwise ten steps and end the clockwise motion
for (int i = 0; i < 70; i++)
{
ACWTP();
}
break;
}
}
}
void CWTP()
{
digitalWrite(turnMotor, LOW);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(turnMotor, HIGH);
}
void ACWTP()
{
digitalWrite(turnMotor, LOW);
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);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(turnMotor, HIGH);
}
I have cut the code down to fit it the post I can show sections in more detail if needed.
There are more motor control function but the are all very much the same.
I tried to use a boolean called stopNow to break the loop but could make it work.
Any help appreciated