Hey, in School we got a project to create a gambling game and i had the idea of a program, that starts when the button is first pressed, then you have to hold down the button for 1/2/3/4 seconds to bet on the 1st/2nd/3rd/4th LED then if you swipe over the LDR the game starts and shuffles through the LEDs (they blink like a wheel of fortune) then a random LED blinks and if the BetLED corresponds to the RandomLED you win, else you lose.
My problem is, that the pulseIn doesn't work it always gives me values beneath 1 second and then my program does automatically bet on the first LED. My second problem is that in the Win-for-loop the LEDs don't blink but the Serial Monitor shows me the Text that is in there ''You won!''. And My third Problem is how can i make the 2nd last if loop to wait for the swipe over the LDR?
Thanks for your Help already ( And sorry, if my english is bad :D)
int ledred = 6;
int ledred1= 7;
int ledred2= 8;
int ledred3= 9;
int Button= 2;
int ButtonCondition=0;
int BetNumber;
unsigned long Duration;
int Light;
int RandomNumber;
void setup()
{
pinMode(ledred,OUTPUT);
pinMode(ledred1,OUTPUT);
pinMode(ledred2,OUTPUT);
pinMode(ledred3,OUTPUT);
pinMode(Button,INPUT);
Serial.begin (9600);
randomSeed(analogRead(0));
}
void loop()
{
Serial.println(analogRead(0));
Serial.print("Please press the Button. Depending on which LED you want to bet, hold down the Button for 1/2/3/4 Seconds. ");
Serial.print("You have to count in your Mind, on which LED you are.");
ButtonCondition=digitalRead(Button);
if(ButtonCondition==1)
{
Duration = pulseIn(2,HIGH); // doesn't work, return value always is < 1000 even if I press the button for 5 seconds.
delay(100);
if(Duration<=1000)
{
digitalWrite(ledred,HIGH);
BetNumber=6;
Serial.println(" You bet on the first LED.");
delay(100);
}
if(Duration<=2000&&Duration>=1000)
{
digitalWrite(ledred1,HIGH);
BetNumber=7;
Serial.println(" You bet on the second LED.");
delay(100);
}
if(Duration<=3000&&Duration>=2000)
{
digitalWrite(ledred2,HIGH);
BetNumber=8;
Serial.println(" You bet on the third LED.");
delay(100);
}
if(Duration<=4000&&Duration>=3000)
{
digitalWrite(ledred3,HIGH);
BetNumber=9;
Serial.println(" You bet on the fourth LED.");
delay(100);
}
}
delay(1000);
digitalWrite(ledred,LOW);
digitalWrite(ledred1,LOW);
digitalWrite(ledred2,LOW);
digitalWrite(ledred3,LOW);
Serial.print("To start the gambling game, swipe over the LDR."); // it should wait here, for the user to swipe over it and don't continue after the if loop
if(Light<=700);
{
for(int slower = 0;slower<=200;slower=slower+10)
{
digitalWrite(ledred,HIGH);
delay(slower);
digitalWrite(ledred,LOW);
digitalWrite(ledred1,HIGH);
delay(slower);
digitalWrite(ledred1,LOW);
digitalWrite(ledred2,HIGH);
delay(slower);
digitalWrite(ledred2,LOW);
digitalWrite(ledred3,HIGH);
delay(slower);
digitalWrite(ledred3,LOW);
}
RandomNumber= random(6,10);
digitalWrite(RandomNumber,HIGH);
delay(100);
digitalWrite(RandomNumber,LOW);
if(RandomNumber==BetNumber)
{
for(int Win=0;Win<=10;Win=Win+1) // the LEDs in this for loop don't even blink, but the serial monitor shows the ''you won!''
{
digitalWrite(ledred,HIGH);
digitalWrite(ledred1,HIGH);
digitalWrite(ledred2,HIGH);
digitalWrite(ledred3,HIGH);
delay(15);
digitalWrite(ledred,LOW);
digitalWrite(ledred1,LOW);
digitalWrite(ledred2,LOW);
digitalWrite(ledred3,LOW);
Serial.print("You won!");
}
}
else
{
digitalWrite(ledred,HIGH);
digitalWrite(ledred1,HIGH);
digitalWrite(ledred2,HIGH);
digitalWrite(ledred3,HIGH);
Serial.print("You lost.");
delay(1000);
}
}
digitalWrite(ledred,LOW);
digitalWrite(ledred1,LOW);
digitalWrite(ledred2,LOW);
digitalWrite(ledred3,LOW);
delay(1000);
}