Go Down

Topic: Buttons (Read 1 time) previous topic - next topic

Ghostbusters

In the Sparkfun Inventors Kit the sample code for a push button runs under the format of "if____,then____,else____"
Code: [Select]
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

Grumpy_Mike

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"

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: [Select]

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!

Ghostbusters

Thanks i will try that and see if that works

Ghostbusters

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.

Grumpy_Mike


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.

Ghostbusters


Ghostbusters

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

Grumpy_Mike


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.

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: [Select]

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: [Select]

if (var==1 && button1State == HIGH) {
}

Ghostbusters

Thanks :D Thanks :D Thanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :DThanks :D Thanks :D

MAS3

Quote from: Ghostbusters
Thanks :D Thanks :D Thanks :D Thanks :DThanks .....

There seems to be a syntax error in that code.
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

Go Up