 # Help Understanding

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…

1. To start the game press the Startt button
2. The ball release lever will lower and allow the ball to roll to the player.
3. Player will then roll the ball over the first switch(SW1) and the release lever
will rise blocking the ball.
4. 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.
5. 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
6. 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
7. The customer will get four rolls to attempt to win the game.
8. 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 = { //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)*

I'm having some difficulty visualizing what is supposed to be happening, but at a quick glance, I don't know that digitalRead(lever) will ever return anything besides 0, since you've defined that pin as an output. It would probably make sense to just have a variable that keeps track of the lever state. bool leverUp = false, and switch it when you put the lever up or down. Replace the digitalRead(lever) with the Boolean.

Also, get some serial logging in there so you know what's happening real time. `so your code looks like this` and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor. The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.

…R

``````int rolls = { //rolls count
{1, 1, 1, 1},
{0, 1, 1, 1},
{0, 0, 1, 1},
{0, 0, 0, 1},
{0, 0, 0, 0}
};
``````

320 bits of precious RAM to store 20 bits of information

That's not a good ratio.

Consider reading the tutorial on how to blink without using delay, then rewrite your code without using delays at all.

``````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 = { //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);

}
}
}
``````

dcr_inc: 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..

First answer...as you haven't done enough study of the system or the instructions on how to post here either by the look of how you presented the code.

Second answer.....no such thing as "luck" in programming

From a quick look at the code in Reply #5 I suggest that you do all the digitalRead()s as the first thing in loop() and save their values for use throughout loop(). Think of it as taking a snapshot of the state of the system and then making decisions based on that state.

Also read up about the concept of State Machine - it will allow you to create a much simpler program structure.

If you want a responsive program I also suggest you get rid of ALL the delay()s. The functions delay() and delayMicroseconds() block the Arduino until they complete.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R