Pages: [1]   Go Down
Author Topic: Buttons  (Read 1064 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Who you gonna call?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In the Sparkfun Inventors Kit the sample code for a push button runs under the format of "if____,then____,else____"
Code:
if (((button1State == LOW)))
     
  {
    digitalWrite(ledPin, HIGH);  // turn the LED on
  }
  else
  {
    digitalWrite(ledPin, LOW);  // turn the LED off
  }
I was wondering if you can have more than one "then" and the arduino randomly picks a "then".
The format would be like" if__,then__,or__,or__,else__". I tried to do that but the "or" doesn't turn orange like "if and else"

If there is a way to do this or close to doing this i would appreciate if you can reply

Thanks in advance
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
was wondering if you can have more than one "then" and the arduino randomly picks a "then".
No this is the C++ language you are using.
There is no "then" in it at all.

The closest you could get it to generate a random number and use that in a switch statement to trigger a piece of code at "random"
Logged

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

Your best bet is to check for what you are looking for and then, if it isn't there, try a Random function to determine where to go from there.

Handy resources:
Random:  http://arduino.cc/en/reference/random#.UwQCr_ldV9U
Switch:  http://arduino.cc/en/Reference/SwitchCase#.UwQDePldV9U
if...else:  http://arduino.cc/en/Reference/Else#.UwQDevldV9U

Code:
int min = 1;
int max =6;
if (button1State == LOW)  {
    digitalWrite(ledPin, HIGH);  // turn the LED on
  } else {
    int var = random(min,max);
  switch (var) {
    case 1:
      //do something when var equals 1
      break;
    case 2:
      //do something when var equals 2
      break;
    default:
      // if nothing else matches, do the default
      // default is optional
  }
  }
Hope this helps!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
Who you gonna call?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks i will try that and see if that works
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
Who you gonna call?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just wondering, for the code you put up am i able to put up more cases. e.i a case 3, 4, 5, or 6.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just wondering, for the code you put up am i able to put up more cases. e.i a case 3, 4, 5, or 6.
Yes you can have as many cases as you want.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
Who you gonna call?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
Who you gonna call?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

tdewitt274, the code you put up what function does the min and max serve in the beggining
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

tdewitt274, the code you put up what function does the min and max serve in the beggining
It defines the range of the random numbers.
Logged

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

I prefer using IF statements as I feel it's less prone to mistakes (at least for me).  I think the preferred method would be SWITCH in most cases.

You could do the same code as the below. 

Code:
int min = 1;
int max = 6;
if (button1State == LOW)  {
    digitalWrite(ledPin, HIGH);  // turn the LED on
  } else {
  int var = random(min,max);
  if (var==1 && button1State == LOW) {
      //do something when var equals 1
  } else if (var==2) {
      //do something when var equals 2
  } else {
      // if nothing else matches, do the default
      // default is optional
  }
}
With IF statements, you have the ability to do AND (&&) and OR (||) statements as part of your criteria.  For example,  the below. 
Code:
if (var==1 && button1State == HIGH) {
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
Who you gonna call?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks smiley-grin Thanks smiley-grin Thanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks :DThanks smiley-grin Thanks smiley-grin
Logged

The Netherlands
Offline Offline
Edison Member
*
Karma: 47
Posts: 1537
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote from: Ghostbusters
Thanks smiley-grin Thanks smiley-grin Thanks smiley-grin Thanks :DThanks .....
There seems to be a syntax error in that code.
Logged

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

Pages: [1]   Go Up
Jump to: