Pages: [1] 2   Go Down
Author Topic: Looping for a set amount of times.  (Read 1167 times)
0 Members and 1 Guest are viewing this topic.
Derbyshire
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all, this is a basic programming question.

I have a set up where a button changes a button state. (ranging from 0-5)

What i want to do is:

Make some leds flash a few times when a button state is reached.

Then Stop these flashing leds and continue with the rest of the program.

I am looking at 'for' statements.


for example:

Code:
if (buttonMode == 3) {
  
  for(int i = 0; i<5; i++){
    
  Serial.println(i);
    
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  delay(400);

  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  delay(400);
  
  }}}}

I am having trouble stopping the code listed above, and  I require to play another bit of code after it have been repeated a few times.

I have tried inserting [if button state > 3, 'break:'] etc.

Basically I am missing some fundamental programming knowledge!



Many thanks,
« Last Edit: October 05, 2012, 02:42:27 pm by Wi11turner » Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Basically I am missing some fundamental programming knowledge!
Actually, you are missing the rest of your code.

From what it looks like, you have attempted to implement a state machine, but without seeing all of the code, I can't tell how.
Logged

UK, Southwest
Offline Offline
Full Member
***
Karma: 5
Posts: 138
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One way of doing what you want is a state machine

Imagine your states are:
Idle
Any_button_seen
Button1_flashing
Button_2_flashing

What would make the transition idle->Any_button_seen?  How would you decide to go to the state Button1_flashing?
If you were in the state Button1_flashing, what should the Arduino do?  How would it decide to finish flashing (hint: look at blinkwithoutdelay example)
When finished flashing, what state do you think it should go back to?

Look up state machine.
Try drawing out the state machine on a sheet of paper.  What might make it move from one state to another?

You will need a variable representing a state, and a switch statement.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23652
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd say you were close:
Code:
while (buttonMode == 3) {
  int i = 0;
  while( i<5){
   
  Serial.println(i);
   
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  delay(400);

  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  delay(400);
  i=i+1;
  }
button = 4;
}
stay in the inner loop, when done change the outer loop condition so it jumps out
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any reason not to use a for loop there?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23652
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I wasn't sure about where to change button to 4 with a for:next, this way seemed clearer to me.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, the "while" loop is a classic "for"
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23652
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, the long hand way of writing it out still works tho, yes?
4:15 on a Friday, feeling a little frazzled ...
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46013
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while (buttonMode == 3) {
// Do some stuff
button = 4;
}

How are you intending to exit this loop? Changing the value of button has no effect on buttonMode.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am guessing you are learning and just building your own sketches to verify you understand (basically, that is where I am).

the for loop works if you set it up as a function instead of inside the void loop.

so for example
void loop ()
{check for a button press,
if received
check the state
kick out to the function that handles that
}


function state1
{do stuff}

function state2
{do different stuff}

Your for loop would be setup in the function state1 or 2.
When the if loop is done, it would automatically return to void loop and look for another button press.

you might also want to look at debounce (search debounce arduino)
I had the issue of my button press being seen more than once changing my state rapidly. Originally I used a delay, but that basically just paused the program instead of keeping multiple button presses from occurring.

Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11148
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


the for loop works if you set it up as a function instead of inside the void loop.

When the if loop is done, it would automatically return to void loop and look for another button press.


I can't interpret either of those sentences in any way that makes sense.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23652
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while (buttonMode == 3) {
// Do some stuff
buttonMode = 4;  <<< yeah, little typo there.
}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


the for loop works if you set it up as a function instead of inside the void loop.

When the if loop is done, it would automatically return to void loop and look for another button press.


I can't interpret either of those sentences in any way that makes sense.

how about

the for command works if used in a separate function instead of being inside the void loop().
when the if loop has i in the function reach 5 (no longer less than 5), it will return back to the void loop() and wait for another button press.


for example
Code:
void loop ()
{// general code to look for button press, current state, etc etc

if (button == 3) buttonstate3;
}

int butttonstate3()
{
 {for(int i = 0; i < 5; i++)       
 {
  Serial.println(i); 
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  delay(400);

  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  delay(400);
  }
button = 4;
}}

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the for command works if used in a separate function instead of being inside the void loop().

No. Don't just make stuff up.

Quote
Code:
if (button == 3) buttonstate3;

That does not call the function buttonstate3. This does:

Code:
if (button == 3)
  buttonstate3 ();
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You would have got better answers if you posted all your code in the first place. Don't just excerpt it.
Logged

Pages: [1] 2   Go Up
Jump to: