… So, an armless bandit?

Homework #2

I wanted to experiment with random.

Could the code be simpler? … YES

#define ledPin_1 6
#define ledPin_2 5
#define ledPin_3 4
#define ledPin_4 3
#define buttonPin 2

// a variable to hold a random number
long randomNumber_1;
long randomNumber_2;
long randomNumber_3;
long randomNumber_4;

boolean ledState_1 = 0;
boolean ledState_2 = 0;
boolean ledState_3 = 0;
boolean ledState_4 = 0;
boolean prevBtnState = LOW;



void setup() {

//start serial communication
Serial.begin(9600);

//send at start messages to the serial monitor
Serial.println("New sequence");

// Set LED Mode
pinMode( ledPin_1, OUTPUT );
pinMode( ledPin_2, OUTPUT );
pinMode( ledPin_3, OUTPUT );
pinMode( ledPin_4, OUTPUT );

//Set Button Mode
pinMode( buttonPin, INPUT );

//generate a random seed
randomSeed( analogRead(A0) );

}



void loop() {
    
boolean btnState = digitalRead( buttonPin );

if ( btnState == HIGH && prevBtnState == LOW ) {


//Suspense sequence ------------------------------------------------------

digitalWrite( ledPin_1, HIGH );
digitalWrite( ledPin_2, LOW );
digitalWrite( ledPin_3, LOW );
digitalWrite( ledPin_4, LOW );
delay( 100 );

digitalWrite( ledPin_1, LOW );
digitalWrite( ledPin_2, HIGH );
digitalWrite( ledPin_3, LOW );
digitalWrite( ledPin_4, LOW );
delay( 100 );

digitalWrite( ledPin_1, LOW );
digitalWrite( ledPin_2, LOW );
digitalWrite( ledPin_3, HIGH );
digitalWrite( ledPin_4, LOW );
delay( 100 );

digitalWrite( ledPin_1, LOW );
digitalWrite( ledPin_2, LOW );
digitalWrite( ledPin_3, LOW );
digitalWrite( ledPin_4, HIGH );
delay( 100 );

digitalWrite( ledPin_1, HIGH );
digitalWrite( ledPin_2, LOW );
digitalWrite( ledPin_3, LOW );
digitalWrite( ledPin_4, LOW );
delay( 100 );

digitalWrite( ledPin_1, LOW );
digitalWrite( ledPin_2, HIGH );
digitalWrite( ledPin_3, LOW );
digitalWrite( ledPin_4, LOW );
delay( 100 );

digitalWrite( ledPin_1, LOW );
digitalWrite( ledPin_2, LOW );
digitalWrite( ledPin_3, HIGH );
digitalWrite( ledPin_4, LOW );
delay( 100 );

digitalWrite( ledPin_1, LOW );
digitalWrite( ledPin_2, LOW );
digitalWrite( ledPin_3, LOW );
digitalWrite( ledPin_4, HIGH );
delay( 100 );

digitalWrite( ledPin_1, HIGH );
digitalWrite( ledPin_2, LOW );
digitalWrite( ledPin_3, LOW );
digitalWrite( ledPin_4, LOW );
delay( 100 );

digitalWrite( ledPin_1, LOW );
digitalWrite( ledPin_2, HIGH );
digitalWrite( ledPin_3, LOW );
digitalWrite( ledPin_4, LOW );
delay( 100 );

digitalWrite( ledPin_1, LOW );
digitalWrite( ledPin_2, LOW );
digitalWrite( ledPin_3, HIGH );
digitalWrite( ledPin_4, LOW );
delay( 100 );

digitalWrite( ledPin_1, LOW );
digitalWrite( ledPin_2, LOW );
digitalWrite( ledPin_3, LOW );
digitalWrite( ledPin_4, HIGH );
delay( 100 );

digitalWrite( ledPin_1, LOW );
digitalWrite( ledPin_2, LOW );
digitalWrite( ledPin_3, LOW );
digitalWrite( ledPin_4, LOW );
delay( 100 );


//Random draw sequence ---------------------------------------------------

// assign a random number to variable
randomNumber_1 = random(2);
ledState_1 = randomNumber_1;
randomNumber_2 = random(2);
ledState_2 = randomNumber_2;
randomNumber_3 = random(2);
ledState_3 = randomNumber_3;
randomNumber_4 = random(2);
ledState_4 = randomNumber_4;

//print the output to the serial monitor window
Serial.print(" ");
Serial.println(randomNumber_1);
Serial.println(randomNumber_2);
Serial.println(randomNumber_3);
Serial.println(randomNumber_4);

//Turn on ledPin_1 or not
digitalWrite( ledPin_1, ledState_1 );
delay ( 1000 );

//Turn on ledPin_2 or not
digitalWrite( ledPin_2, ledState_2 );
delay ( 1000 );

//Turn on ledPin_3 or not
digitalWrite( ledPin_3, ledState_3 );
delay ( 1000 );

//Turn on ledPin_4 or not
digitalWrite( ledPin_4, ledState_4 );
delay ( 2000 );


//result sequence ---------------------------------------------------

digitalWrite( ledPin_1, LOW );
digitalWrite( ledPin_2, LOW );
digitalWrite( ledPin_3, LOW );
digitalWrite( ledPin_4, LOW );
delay ( 200 );

digitalWrite( ledPin_1, ledState_1 );
digitalWrite( ledPin_2, ledState_2 );
digitalWrite( ledPin_3, ledState_3 );
digitalWrite( ledPin_4, ledState_4 );
delay ( 200 );

digitalWrite( ledPin_1, LOW );
digitalWrite( ledPin_2, LOW );
digitalWrite( ledPin_3, LOW );
digitalWrite( ledPin_4, LOW );
delay ( 200 );

digitalWrite( ledPin_1, ledState_1 );
digitalWrite( ledPin_2, ledState_2 );
digitalWrite( ledPin_3, ledState_3 );
digitalWrite( ledPin_4, ledState_4 );
delay ( 200 );

digitalWrite( ledPin_1, LOW );
digitalWrite( ledPin_2, LOW );
digitalWrite( ledPin_3, LOW );
digitalWrite( ledPin_4, LOW );
delay ( 200 );

digitalWrite( ledPin_1, ledState_1 );
digitalWrite( ledPin_2, ledState_2 );
digitalWrite( ledPin_3, ledState_3 );
digitalWrite( ledPin_4, ledState_4 );
delay ( 200 );

}
}