button - Arduino - Action is being performed three times (DIY Game) -
i creating arduino project plays rock paper scissors you, have part of code reading value of button , whenever click 1 button, outputs 3 times (view code , images of serial monitor understand mean, can't explain it)
here code:
// constants won't change. they're used here // set pin numbers: const int buttonpin1 = a0; // number of pushbutton pin const int buttonpin2 = a1; const int buttonpin3 = a2; const int ledpin = 12; // number of led pin // variables change: int buttonstate1 = 0; // variable reading pushbutton status int buttonstate2 = 0; int buttonstate3 = 0; char * choices[3] = {"rock", "paper", "scissors"}; char * finalresult[3] = {"you lost", "you won!", "it's tie"}; byte human = 0, computer = 0, fr = 0; char result[25]; void setup() { // initialize led pin output: pinmode(ledpin, output); // initialize pushbutton pin input: pinmode(buttonpin1, input); pinmode(buttonpin2, input); pinmode(buttonpin3, input); digitalwrite(ledpin,low); serial.begin(9600); unsigned long r; randomseed(r); } void loop(){ int choice = random(1,4); int pick; // read state of pushbutton value: // check if pushbutton pressed. // if is, buttonstate high: serial.println("entering reading loop..."); while(true) { buttonstate1 = digitalread(buttonpin1); buttonstate2 = digitalread(buttonpin2); buttonstate3 = digitalread(buttonpin3); if (buttonstate1 == high && buttonstate2 == low && buttonstate3 == low) { // turn led on: throw('1'); } else if (buttonstate1 == low && buttonstate2 == high && buttonstate3 == low) { // turn led on: pick = 2; //paper throw('2'); }else if (buttonstate1 == low && buttonstate2 == low && buttonstate3 == high) { // turn led on: pick = 3; //scissor throw('3'); } } } void throw(char h) { bool thrown = false; h -= '0'; //convert ascii decimal h -= 1; // instead of 1,2,3, h 0,1,2 byte c = random(0, 3); sprintf(result, "the computer chose: %s, chose: %s", choices[c], choices[h]); serial.println(result); if ( c == h) serial.println(f("its tie")); else { switch (c) { case 0: switch (h) { case 1: serial.println(f("paper wraps rock, win!")); human++; break; case 2: serial.println(f("rock crushes scissors, lose!")); computer++; break; } break; case 1: switch (h) { case 0: serial.println(f("paper wraps rock, lose!")); computer++; break; case 2: serial.println(f("scissors cuts paper, win!")); human++; break; } break; case 2: switch (h) { case 0: serial.println(f("rock crushes scissors, win!")); human++; break; case 1: serial.println(f("scissors cuts paper, lose!")); computer++; break; } break; } } } this im getting on serial monitor when click 1 button(the rock button)
http://gyazo.com/ceb5c8329993339368cf5d52181ed4d7
as can see, chooses right option button calls throw function 3 times.
i beginner(the throw function implemented source http://playground.arduino.cc/main/rockpaperscissors) please bare me if missed crucial information.
from arduino docs recommend using debounce strategy handling inputs.
without debouncing, pressing button once can appear code multiple presses.
http://www.arduino.cc/en/tutorial/debounce
here example illustrating debounce technique:
http://danthompsonsblog.blogspot.com/2011/12/arduino-push-button-onoff-example.html
Comments
Post a Comment