Hi guys,
I have a program which starts a loop repeatedly when a push button is being pressed.
I need to put another push button to terminate the loop. my mean is at any point of the loop, when the second button is pressed (for a moment) do the rest of the loop and stop.
my initial sample project is below.
if some body know, please help me.
int b1 = 4;
int b2 = 5;
int LED = 13;
int buttonstate1 = LOW;
int buttonstate2 = LOW;
void setup() {
pinMode(b1, INPUT);
pinMode(b2, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
buttonstate1 = digitalRead(b1);
if (buttonstate1 == HIGH) {
c1();
}
}
void c1() {
buttonstate2 = digitalRead(b2);
if (buttonstate2 == LOW) {
// below code is a sample code and is not my program
for (int i = 1 ; i <= 300 ; i = i + 5) {
digitalWrite(LED, HIGH);
delay(i);
digitalWrite(LED, LOW);
delay(300 - i);
}
}
c1();
}
Don't call c1() from within c1() This can only end in tears.
Fix that first.
UKHeliBob:
Don't call c1() from within c1() This can only end in tears.
Fix that first.
I want c1() continues repeatedly until operator presses the pushbutton2 at any point of loop (program should detects state of the pushbutton2 while loop is running), then rest of the loop runs and stop.
if I don't call c1(), the loop just runs for a single time.
Keep running the code inside c1() using a while loop rather than calling c1() again from inside itself, or call it again from loop() while the condition(s) are still correct.
UKHeliBob:
or call it again from loop() while the condition(s) are still correct.
Which is the version you want to go with
That's the way you can make the Arduino multitask.
UKHeliBob:
Keep running the code inside c1() using a while loop rather than calling c1() again from inside itself
I used while loop instead of "if", but it's still not working.
I used while loop instead of "if", but it's still not working.
Are you going to share the code with us ?
And are you going to ignore the advice you don't like for some reason?
UKHeliBob:
Are you going to share the code with us ?
int b1 = 4;
int b2 = 5;
int LED = 13;
int buttonstate1 = LOW;
int buttonstate2 = LOW;
void setup() {
pinMode(b1, INPUT);
pinMode(b2, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
buttonstate1 = digitalRead(b1);
if (buttonstate1 == HIGH) {
c1();
}
}
void c1() {
buttonstate2 = digitalRead(b2);
// below code is a sample code and is not my program
while (buttonstate2 == LOW) {
for (int i = 1 ; i <= 300 ; i = i + 5) {
digitalWrite(LED, HIGH);
delay(i);
digitalWrite(LED, LOW);
delay(300 - i);
}
}
}
while (buttonstate2 == LOW)
When and how will buttonstate2 change inside the while loop ? Could you perhaps read the input inside the while loop ?
WARNING - there are other potential problems ...
UKHeliBob:
while (buttonstate2 == LOW)
When and how will buttonstate2 change inside the while loop ? Could you perhaps read the input inside the while loop ?
At any moment while c1() is running, if user changes the state of button2, the program should runs to the end and do not start again.
I changed my code and the program stops the loop, but only when I press the button2 at the end of loop (not at any point).
int b1 = 4;
int b2 = 5;
int LED = 13;
int buttonstate1 = LOW;
int buttonstate2 = LOW;
int lastbuttonstate1 = LOW;
int lastbuttonstate2 = LOW;
void setup() {
pinMode(b1, INPUT);
pinMode(b2, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
lastbuttonstate1 = buttonstate1;
buttonstate1 = digitalRead(b1);
if (buttonstate1 != lastbuttonstate1 && buttonstate1 == HIGH) {
c1();
}
}
void c1() {
lastbuttonstate2 = buttonstate2;
buttonstate2 = digitalRead(b2);
// below code is a sample code and is not my program
if (buttonstate2 == lastbuttonstate2 &&buttonstate2 == LOW) {
for (int i = 1 ; i <= 100 ; i = i + 5) {
digitalWrite(LED, HIGH);
delay(i);
digitalWrite(LED, LOW);
delay(100 - i);
}
c1();
}
}
WARNING - there are other potential problems ...
What are other problems?
You are doing it again. c1() is calling c1()
You really do not want to do that.
UKHeliBob:
You are doing it again. c1() is calling c1()
You really do not want to do that.
if I don't call c1(), the program doesn't repeat, while I want it runs repeatedly (but until button2 is pressed at any point)
I really don't know what should I do! I am working on this piece of my program for 2 days. If you know please help me and write the code.
Then what is this?
void c1() {
//stuff
if (buttonstate2 == lastbuttonstate2 &&buttonstate2 == LOW) {
//stuff
c1();
}
}
Yes, it's a piece of your latest code. I just removed some lines...
Don't try to call c1() (which is a terrible terrible name for a function) inside c1()....
What you should do is reread the answers given to you and I think it's best if you start over (NOT looking at your old code) to make the program flow like we suggested.
Because once you started off the wrong way it can be hard (both a lot of work and wrapping you head around it with all the "confusing" bits of code) to correct it. That's why I think it's a bad thing to use an example and extend that. Instead, view the example, get the hang of what it does, and try to incorporate it into a program you want.
Thank you for your guidance but I have tried many ways and I have written my codes from beginning over and over but I couldn't find any solution.
Again, you ignore an important part of the advice:
septillion:
What you should do is reread the answers given to you
Because clearly the idea still isn't clear if you try to call c1() from within c1()....
septillion:
Again, you ignore an important part of the advice:
Because clearly the idea still isn't clear if you try to call c1() from within c1()....
As you said, I changed my code entirely.
I wrote my code from the first. with this code I am able to stop the "while loop" by pushing button2, but still I have two problems:
1- After I stopped the loop by pushing button 2, when I push the button1 again, the loop does not start from the first again.
2- The button2 stops the loop, only when I hold it at the end of loop.
int led = 13;
int button1 = 4;
int button2 = 5;
int buttonstate1 = LOW;
int buttonstate2 = LOW;
void setup() {
pinMode(led, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}
void loop() {
buttonstate1 = digitalRead(button1);
if (buttonstate1 == HIGH) {
while (buttonstate2 == LOW) {
for (int i = 1; i <= 100 ; i = i + 5) {
digitalWrite(led, HIGH);
delay(i);
digitalWrite(led, LOW);
delay(100 - i);
}
buttonstate2 = digitalRead(button2);
}
}
}
would you please tell me what should I do now?
Untested, but it will compile.
Resetting buttonstate1 to low after you start your loop, an adding a variable, endIt that keep track of if button2 has been pressed.
..and I do try to read button 2 as frequently as possible between the delays to not miss a short press..
..looking a bit more, resetting buttonstate1 is not needed..
int led = 13;
int button1 = 4;
int button2 = 5;
int buttonstate1 = LOW;
int buttonstate2 = LOW;
boolean endIt = false;
void setup() {
pinMode(led, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}
void loop() {
buttonstate1 = digitalRead(button1);
if (buttonstate1 == HIGH) {
buttonstate1 = LOW; //not needed, my mistake
while (endIt == false) {
for (int i = 1; i <= 100 ; i = i + 5) {
digitalWrite(led, HIGH);
readButton2();
delay(i);
digitalWrite(led, LOW);
readButton2();
delay(100 - i);
}
}
endIt = false;
}
}
void readButton2() {
buttonstate2 = digitalRead(button2);
if (buttonstate2 == HIGH) endIt = true;
}
Gabriel_swe:
Untested, but it will compile.
Resetting buttonstate1 to low after you start your loop, an adding a variable, endIt that keep track of if button2 has been pressed.
..and I do try to read button 2 as frequently as possible between the delays to not miss a short press..
..looking a bit more, resetting buttonstate1 is not needed..
int led = 13;
int button1 = 4;
int button2 = 5;
int buttonstate1 = LOW;
int buttonstate2 = LOW;
boolean endIt = false;
void setup() {
pinMode(led, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}
void loop() {
buttonstate1 = digitalRead(button1);
if (buttonstate1 == HIGH) {
buttonstate1 = LOW; //not needed, my mistake
while (endIt == false) {
for (int i = 1; i <= 100 ; i = i + 5) {
digitalWrite(led, HIGH);
readButton2();
delay(i);
digitalWrite(led, LOW);
readButton2();
delay(100 - i);
}
}
endIt = false;
}
}
void readButton2() {
buttonstate2 = digitalRead(button2);
if (buttonstate2 == HIGH) endIt = true;
}
Thank you.
Thank you so much.
Your answer is the best answer I have ever gotten.
mechanics:
Your answer is the best answer I have ever gotten.
No, it's not. It's the easiest for you. But that doesn't make it the best 