Copy paste is hard.
const int pin1 = 9; //set motor input 1
const int pin2 = 10; //set motor input 2
const int pausePin = 2; //Set to pin 2
int buttonState = 0; // variable for reading the pushbutton status
int i; //Not used right now
int n; //Used for rotation variable
void setup()
{
// attachInterrupt(0, interruptFunction, HIGH); //Not used
pinMode(pin1, OUTPUT); // initialize the motor pin as an output
pinMode(pin2, OUTPUT); // initialize the motor pin as an output
pinMode(pausePin, INPUT); // initialize the pushbutton pin as an input
Serial.begin(9600);
}
void loop()
{
Serial.println("Loop Top"); //Used for debugging
buttonState = digitalRead(pausePin); //Read if pausePin is HIGH or LOW
Serial.println(n); //Used for debugging
if (buttonState == HIGH){ //If pausePin is HIGH goto pausing
pausing();
}
rotateMotorCW(); //Clockwise function
digitalWrite(pin1, LOW); // sets the motor off
delay(1000); //Stops for short time before changing direction
rotateMotorCCW(); //Counter Clockwise funtion
digitalWrite(pin2, LOW); // sets the motor off
delay(1000); //Stops for short time before changing direction
}
void rotateMotorCW(){
buttonState = digitalRead(pausePin); //Read if pausePin is HIGH or LOW
if (buttonState == HIGH){ //If pausePin is HIGH goto pausing
pausing();
}
for(int n = 0; n < 10; n++){ //Uses "n" for rotation amounts
digitalWrite(pin1, HIGH); // sets the motor on
digitalWrite(pin2, LOW); // sets the motor on rotating CW
delay(1000); //delay before looping back through
Serial.println(n);
}
}
void rotateMotorCCW(){
buttonState = digitalRead(pausePin); //Read if pausePin is HIGH or LOW
if (buttonState == HIGH){ //If pausePin is HIGH goto pausing
pausing();
}
Serial.println("CCW"); //Used for debugging
for(int n=10; n >= 0; n--){ //Uses "n" for rotation amounts in opposite direction
Serial.println(n); //Used for debugging
digitalWrite(pin1, LOW); // sets the motor on
digitalWrite(pin2, HIGH); // sets the motor on rotating CCW
delay (1000);
}
}
void pausing (){
digitalWrite(pin1, LOW); // sets the motor off
digitalWrite(pin2, LOW); // sets the motor off
Serial.println("Pausing"); //Used for debugging
delay(10000); //Stops
}
gpop1:
I read the program as
if button pushed and not paused rotate cw for 10 seconds (11 seconds as relays are active during pause)
stop for 1 second (does that work?) Yes it works, why wouldnt it? its done to ease stress on the motor, thats it
rotate ccw for 10 seconds then repeat
if paused stop motors after they Finnish turning and delay 10 seconds.
then carry on pausing 10 seconds until button is released and the pause is finished (0 to 10 seconds)
you need to understand code flow and to read the code the same way the arduino does.Thats why I have my debugging print lines in and why im asking for help. I tried to use VisualMicro with no success.
code is read top to bottom left to right.
when it sees a delay it says to the arduino stop here and wait
when it sees a "for" it says stop here and stay here until the "for" is completeThat is why I put my check for pause button everywhere, by this logic i should be able to put my if statement inside my for statement and then it would function like that, which does not work either
it doesn't care what you did with a button during the time it in a delay or a for.
The arduino is busy doing nothing waiting for the delay, for to Finnish so it will see the button has changed when it gets to a code line that tells it to check it. I understand this and that is why there are miniuim delays and all of them can be changed for final release.
The arduino outputs are set. They need one instruction to turn them on then they will stay on. If you turn something on then you should also plan to turn it off.
GoForSmoke, The button has a pulldown resistor on the button to avoid bouncing. There is only one motor and all I do is switch the high and low pins to spin in both directions
There are three parts, rotating CW, rotating CCW, and pausing. By themselves they all work (tested), both rotatings work together (tested), but getting the pause to work whenever its pushed is where I am having issues.
I was previously using a delay for both rotates of 5 mins and then using a hardware interrupt. I know thats not the right way so I have been working on doing it a better way. When the coding is done I can remove all delays so lets not get hung up on seeing those. I used variables in place of my old delays so I can increase those and prolong the rotations. I just assumed I should be able to jump out of a for statement without much hassle.
EDIT: I forgot my digitalRead in my for loops when I tried and put my if statements inside it. So now it works and comes back to the for loop prefectly. Below is what I was missing, this is about 90% of how I want the program to work. I will look at the other methods I want to try and will of course ask any questions when I get stuck. Thank you all.
void rotateMotorCCW(){
Serial.println("CCW"); //Used for debugging
for(int n=10; n >= 0; n--){ //Uses "n" for rotation amounts in opposite direction
buttonState = digitalRead(pausePin);
if (buttonState == HIGH){ //If pausePin is HIGH goto pausing
pausing();
}
Serial.println(n); //Used for debugging
digitalWrite(pin1, LOW); // sets the motor on
digitalWrite(pin2, HIGH); // sets the motor on rotating CCW
delay (1000);
}
}