Can anyone help me understand why I can't ever get a "Win" on this sketch?.. This is what I am trying to accomplish but having NO luck..
- To start the game press the Startt button
- The ball release lever will lower and allow the ball to roll to the player.
- Player will then roll the ball over the first switch(SW1) and the release lever
will rise blocking the ball. - If the player wins the game by stopping the ball in the valley and
triggering Switch 2 for three seconds, then the bell will ring for one
second and the rotating light will flash for ten seconds indicating a
winner. - If the ball rolls and triggers Switch 2 then back to Switch 1 the
computer will sense this as one roll. The Lever will drop after a 3 second delay - If the ball does not go over the peak and rolls back to Switch 1 the
computer will sense this as one roll. The Lever will drop after a 3 second delay - The customer will get four rolls to attempt to win the game.
- Each time Switch 1 is triggered the lever should go up until the roll is
completed to ensure customer safety.
*/
int nsw1; //number of times the switch #1 is closed
int nsw2; //number of times the switch #2 is closed
int tstart; //initial time
int tlast; // last time
int startt = 2; //pin button start
int sw1 = 3; //pin switch #1
int sw2 = 4; //pin switch #2
int lever = 5; //pin output #1 "release lever"
int bell = 6; //pin output #2 "bell"
int light = 7; //pin output #3 "light"
int rolls[5][4] = { //rolls count
{1, 1, 1, 1},
{0, 1, 1, 1},
{0, 0, 1, 1},
{0, 0, 0, 1},
{0, 0, 0, 0}
};
int i = 0; //the rolls row
int j = 0; //the rolls column
void setup() {
pinMode(startt, INPUT_PULLUP); //pin button start as input
pinMode(sw1, INPUT_PULLUP); //pin switch #1 as input
pinMode(sw2, INPUT_PULLUP); //pin switch #2 as input
pinMode(lever, OUTPUT); //pin lever as output
pinMode(bell, OUTPUT); //pin bell as output
pinMode(light, OUTPUT); //pin light as output
Serial.begin(9600);
for (i = 8; i <= 11; i++) { //four leds for rolls count as output
pinMode(i, OUTPUT);
}
i = 0;
for (j = 0; j <= 3; j++) { //on the four rolls
digitalWrite(j + 8, rolls*[j]);*
- }*
}
void loop() { - if (digitalRead(startt) == LOW && i == 0) { //for start the game push button start*
- delay(2000); //wait 2 seconds*
- digitalWrite(lever, HIGH); //The ball release lever will lower and allow the ball to roll to the player*
- }*
- if (digitalRead(sw1) == LOW && digitalRead(lever) == HIGH) { //Player will then roll the ball over the switch#1*
- digitalWrite(lever, LOW); //the release lever will rise blocking the ball.*
- delay(500);*
- tstart = millis();*
- tlast = tstart;*
- while ((tlast - tstart) <= 3000 && digitalRead(sw2) == LOW) { //wait 3 seconds*
- tlast = millis();*
- }*
- if (digitalRead(sw2) == LOW && (tlast - tstart) >= 3000 ) { // count triggering Switch# 2*
- nsw2++;*
- }*
- if (nsw2 == 1) { //the player wins the game by stopping the ball in the valley and triggering Switch# 2 in a 3 seconds*
- digitalWrite(bell, HIGH); //the bell will ring for one second*
- digitalWrite(light, HIGH); //the rotating light will flash for ten seconds indicating a winner.*
- delay(1000);*
- digitalWrite(bell, LOW);*
- delay(9000);*
- digitalWrite(light, LOW);*
- i = 0;*
- nsw2 = 0;*
- for (j = 0; j <= 3; j++) {*
_ digitalWrite(j + 8, rolls*[j]);_
_ }_
_ }_
_ else { //If the ball rolls and triggers Switch# 2 twice then back to Switch 1 the computer will sense this as one roll._
_ i++; //If the ball does not go over the peak and rolls back to Switch# 1 the computer will sense this as one roll._
_ nsw2 = 0;_
_ for (j = 0; j <= 3; j++) {_
_ digitalWrite(j + 8, rolls[j]);
}
delay(200);
while (i <= 3 && digitalRead(sw1) == HIGH) {} //The game operator will pick up the ball behind the hump and place it on the lever (switch#1 is activated)
delay(7000); //wait 7 seconds*
* digitalWrite(lever, HIGH); // the lever descends*
* delay(1000);
}
if (i == 4) {
i = 0;
for (j = 0; j <= 3; j++) { //loser*
digitalWrite(j + 8, rolls*[j]);
}
delay(500);
}
}
}_
hwR_5.ino (3.27 KB)*