#include #include #include #include #define one 0 //LED 1 #define two 1 //LED 2 #define three 2 //LED 3 #define four 3 //LED 4 #define five 4 //LED 5 #define six 5 //LED 6 #define seven 6 //LED 7 #define eight 21 //LED 8 #define readyLight 24 //Green ready LED #define rollPin 22 //Starting the lights button pin #define resetPin 23 //Reset Button #define lastSpeed 1500 //speed of the final round of spinning #define blinkSpeed 750 //speed of final blinking #define slowSpeed 1750 //speed of slow spin #define speedInc 250 //number by which to increment speed for slowdown #define firstSpeed 50 //speed for the first cycle void flash(int pin, int delayTime) { digitalWrite(pin, 1); //turn the light on for the pin sent to the function delay(delayTime); //keep the light on for the time defined in delayTime digitalWrite(pin, 0); //turn the light off } void init(void) { //set all pins as output or input pinMode(one, OUTPUT); pinMode(two, OUTPUT); pinMode(three, OUTPUT); pinMode(four, OUTPUT); pinMode(five, OUTPUT); pinMode(six, OUTPUT); pinMode(seven, OUTPUT); pinMode(eight, OUTPUT); pinMode(readyLight, OUTPUT); pinMode(rollPin, INPUT); pinMode(resetPin, INPUT); //set both button pins to 3.3v so that when the button is pressed it is brought down to 0v pullUpDnControl(rollPin, PUD_UP); pullUpDnControl(resetPin, PUD_UP); //turn all lights off for (int i = 0; i<7; i++) { digitalWrite(i, 0); } digitalWrite(eight, 0); //turn the ready light on digitalWrite(readyLight, 1); //seed the random rand function srand (time(NULL)); } int runTheLights(void) { int speed; //make the speed variable digitalWrite(readyLight, 0); //turn the ready light off // cycle through the lights until the lastSpeed delay is reached slowing down each time by speedInc for (speed = firstSpeed; speed < lastSpeed; speed = speed) { //spin the lights and land on the random number flash(one, speed); speed = (speed + (speedInc/8)); flash(two, speed); speed = (speed + (speedInc/8)); flash(three, speed); speed = (speed + (speedInc/8)); flash(four, speed); speed = (speed + (speedInc/8)); flash(five, speed); speed = (speed + (speedInc/8)); flash(six, speed); speed = (speed + (speedInc/8)); flash(seven, speed); speed = (speed + (speedInc/8)); flash(eight, speed); speed = (speed + (speedInc/8)); } speed = slowSpeed; //set the speed to the slowest speed that will end the cycles int decision = rand() % 8 + 1; //set the winning random number // cycle throught the lights then finish the at the winning light. Flash that light at the speed defined by blinkSpeed flash(one, speed); if (decision == 1) { while(1) { flash(one, blinkSpeed); delay(blinkSpeed); if (digitalRead(resetPin) == 0) { return(1); } } } else { flash(two, speed); if (decision == 2) { while(1) { flash(two, blinkSpeed); delay(blinkSpeed); if (digitalRead(resetPin) == 0) { return(1); } } } else { flash(three, speed); if (decision == 3) { while(1) { flash(three, blinkSpeed); delay(blinkSpeed); if (digitalRead(resetPin) == 0) { return(1); } } } else { flash(four, speed); if (decision == 4) { while (1) { flash(four, blinkSpeed); delay(blinkSpeed); if (digitalRead(resetPin) == 0) { return(1); } } } else { flash(five, speed); if (decision == 5) { while (1) { flash(five, blinkSpeed); delay(blinkSpeed); if (digitalRead(resetPin) == 0) { return(1); } } } else { flash(six, speed); if (decision == 6) { while (1) { flash(six, blinkSpeed); delay(blinkSpeed); if (digitalRead(resetPin) == 0) { return(1); } } } else { flash(seven, speed); if (decision == 7) { while (1) { flash(seven, blinkSpeed); delay(blinkSpeed); if (digitalRead(resetPin) == 0) { return(1); } } } else { flash(eight, speed); if (decision == 8) { while (1) { flash(eight, blinkSpeed); delay(blinkSpeed); if (digitalRead(resetPin) == 0) { return(1); } } } } } } } } } } } int main(void) { // if the wiringPiSetup doesn't work show an error messange and end this if(wiringPiSetup() == -1) { //wiringpi failed for some reason so exit printf("setup of wiringPi failed!"); return 1; } init(); //setup the pins for input and output while(1) { digitalWrite(readyLight, 1); //make sure the ready light is on //if button is pressed spin the lights otherwise do nothing if (digitalRead(rollPin) == 0) { runTheLights(); } } }