I need help whit my code . I dont know how can I stop my void loop whit push button .
My code is
include <Stepper.h>
const int switchPin1 = 2;
const int switchPin2 = 3;
const int switchPin3 = 4;
const int switchPin4 = 5;
const int stepsPerRevolution1 = 160000;
const int stepsPerRevolution2 = 4000;
//const int stepsPerRevolution2 =4000;// change this to fit the number of steps per revolution // for your motor
int state1;
int state2;
int state3;
int state4;
long int br1=0;
long int br2=0;
// initialize the stepper library on pins 8 through 11:
Stepper myStepper1(stepsPerRevolution1, 8,9);
//Stepper myStepper2(stepsPerRevolution2, 8,9);
void setup() {
// set the speed at 60 rpm:
myStepper1.setSpeed(300);
//myStepper2.setSpeed(100);
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
pinMode(switchPin3,INPUT);
pinMode(switchPin4,INPUT);
// initialize the serial port:
Serial.begin(9600); }
void loop() {
state1 = digitalRead(switchPin1);
state2 = digitalRead(switchPin2);
state3 = digitalRead(switchPin3);
state4 = digitalRead(switchPin4);
if (state1 == HIGH) {
//Pomicanje motora nazad
Serial.println("Nazad");
myStepper1.step(stepsPerRevolution2);
br1=br1+stepsPerRevolution2;
Serial.println(br1);
}
if (state2 == HIGH) {
// pomicanje motora naprijed
Serial.println("Naprijed");
myStepper1.step(-stepsPerRevolution2);
br2=br2+stepsPerRevolution2;
Serial.println(br2);
}
if (state4 == HIGH) {
}
}
//zaustavljanje
if (state3 == HIGH) {
int i;
while(1)
{int k,j;
for(k=0;k<=br1;k+=4000)
{
myStepper1.step(stepsPerRevolution2);
Serial.println(k);
Serial.println("nazad");
delay(1000);
}
for(j=0;j<br2;j+=4000)
{
myStepper1.step(-stepsPerRevolution2);
Serial.println(j);
Serial.println("naprijed");
delay(1000);
}
}
}
}
You can read the button on a digital input pin.
Then put the code you want to stop in an if statement, that ask for the buttons state.
Sample code to add :
On top of your code with declarations add variables :
const int myButton = 9; // this is the Arduino pin that connects to your button
boolean readMyButton = 0; // this is the variable that holds input reading from that pin, only true or false
boolean myButtonPressed =0; // this is the variable that remembers the button was pressed
in your loop() add :
readMyButton = DigitalRead(myButton); // get the button state on the input
If (readMyButton == 0)
{ // if button was pressed briefly, remember that it was pressed
myButtonPressed = 1;
if (myButtonPressed = 0)
{ // only do this embraced code if myButtonPressed is false, otherwise ignore the code -
// do your stuff here
// it will be done everytime the loop() runs your code and the button wasnot pressed
// if the button was pressed, myButton gets HIGH and this part of code is ignored
}
The code is my rookie code as I am still a newbie like you are, but it can be read and understand right away.
later you can use the while() statement, as this is a smarter construct, but somewhat more complex to understand.
if state3 in code high and if I push state4 that stop my program executing
Stop forever or stop while the two states are HIGH ?
There are problems in your program as it stands. For instance, if state1 is HIGH for any length of time then the value of br1 is going to increase very rapidly as the code is repeated by the loop() function. You probably want to detect when state1 becomes HIGH rather than when it is HIGH. However, as you have not said what is driving the inputs it is impossible to say, and, of course, if state3 is ever HIGH the program will continue to turn the motor one way then the other forever because of the while(1). Is that what it is meant to do ?
t0r30rs:
if state3 in code high and if I push state4 that stop my program executing
Is this what you mean?
void loop()
{
if (state3)
{
if (button4 == HIGH)
{
// stop program execution by looping forever here.
// Arduino must be RESET after this.
while (1)
;
}
}
}
UKHeliBob:
Stop forever or stop while the two states are HIGH ?
There are problems in your program as it stands. For instance, if state1 is HIGH for any length of time then the value of br1 is going to increase very rapidly as the code is repeated by the loop() function. You probably want to detect when state1 becomes HIGH rather than when it is HIGH. However, as you have not said what is driving the inputs it is impossible to say, and, of course, if state3 is ever HIGH the program will continue to turn the motor one way then the other forever because of the while(1). Is that what it is meant to do ?
My program does the following: when you activate state1 engine is turning to one side for as many steps as the times the button state1, in the same way works and state2. state 3 for the next went to one side for as many steps as was done with a button state1 when it is over going to the other side for as many steps as was done with state2. state 4 I need to end this while (1) loop that is that we cease what works state3.
all buttons are activated with just one click.
We are having a little bit of trouble understanding your use of English. But, we are are here to help, so:
The first thing we need an answer to is this:
Do you understand that if you use "while (1) ;" in your program, the only way to make it start again is to reset the Arduino, or turn it off? You cannot just press your button again. You must press the reset button. Is that what you want?
arduinodlb:
We are having a little bit of trouble understanding your use of English. But, we are are here to help, so:
The first thing we need an answer to is this:
Do you understand that if you use "while (1) ;" in your program, the only way to make it start again is to reset the Arduino, or turn it off? You cannot just press your button again. You must press the reset button. Is that what you want?
I see you think you got what I want, how I do over of the external restart button if there is a function
As far as general programming of "state machine" which is what you are doing, you can take control of it using while construct.
You can do that in setup() or in loop() but loop() really does not add much in such usage.
Using while inside loop is just little redundant / unnecessary. ( personal opinion ) .
Use Auto formater and check your code blocks ({ }) - mouse click on {.
Good luck
include <Stepper.h>
const int switchPin1 = 2; since you are using pin 2 - why not name the variable switchPin2
const int switchPin2 = 3;
const int switchPin3 = 4;
const int switchPin4 = 5;
const int stepsPerRevolution1 = 160000; what is int max value ??
const int stepsPerRevolution2 = 4000;
//const int stepsPerRevolution2 =4000;// change this to fit the number of steps per revolution // for your motor
int state1; byte would be OK
int state2;
int state3;
int state4;
long int br1 = 0;
long int br2 = 0;
// initialize the stepper library on pins 8 through 11:
Stepper myStepper1(stepsPerRevolution1, 8, 9);
//Stepper myStepper2(stepsPerRevolution2, 8,9);
void setup() {
// set the speed at 60 rpm:
myStepper1.setSpeed(300);
//myStepper2.setSpeed(100);
pinMode(switchPin1, INPUT); would be better active LOW - depending on how it is wired of course could use INPUT_PULLUP
pinMode(switchPin2, INPUT);
pinMode(switchPin3, INPUT);
pinMode(switchPin4, INPUT);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
state1 = digitalRead(switchPin1);
state2 = digitalRead(switchPin2);
state3 = digitalRead(switchPin3);
state4 = digitalRead(switchPin4);
if (state1 == HIGH) { if(state1) works / evaluates just fine and eliminates common error state1 = x which someone already made in this discussion
//Pomicanje motora nazad
Serial.println("Nazad");
myStepper1.step(stepsPerRevolution2);
br1 = br1 + stepsPerRevolution2;
Serial.println(br1);
no delay ??
}
if (state2 == HIGH) {
// pomicanje motora naprijed
Serial.println("Naprijed");
myStepper1.step(-stepsPerRevolution2);
br2 = br2 + stepsPerRevolution2;
Serial.println(br2);
no delay ??
}
if (state4 == HIGH) {
}
} [b] end of loop()[/b]
//zaustavljanje
not in loop()
if (state3 == HIGH) {
int i;
while (1) how do you exit from state3?
{ int k, j;
for (k = 0; k <= br1; k += 4000)
{
myStepper1.step(stepsPerRevolution2);
Serial.println(k);
Serial.println("nazad");
delay(1000);
}
for (j = 0; j < br2; j += 4000)
{
myStepper1.step(-stepsPerRevolution2);
Serial.println(j);
Serial.println("naprijed");
delay(1000);
}
}
}
} ??