Hi guys!
I'm just beginning to understand programing arduino and I'm making a simple roulette with four leds and I want to make them stop randomly. At the moment it just stops at the same place every time. Can you please guide me?
Here is my code:
const int buttonPin = 2;
int lightpins[4] = {
10,11,12,13};
int state=0;
void setup()
{
pinMode (buttonPin,INPUT);
pinMode (lightpins[0],OUTPUT);
pinMode (lightpins[1],OUTPUT);
pinMode (lightpins[2],OUTPUT);
pinMode (lightpins[3],OUTPUT);
digitalWrite (lightpins[0],LOW);
digitalWrite (lightpins[1],LOW);
digitalWrite (lightpins[2],LOW);
digitalWrite (lightpins[3],LOW);
}
void loop ()
{
int reading = digitalRead (buttonPin);
int blinktime=20;
boolean done;
if (reading == HIGH)
{
if (state==0)
{
state=1;
done=false;
blinktime=20;
blinktime += random(3);
while (!done)
{
for (int j=0;j<4;j++)
{
blinktime += 4;
digitalWrite(lightpins[j],HIGH);
if (blinktime>200)
{
done=true;
break;
}
delay(blinktime);
digitalWrite(lightpins[j],LOW);
delay(blinktime);
}
}
}
}
else
{
state=0;
}
}