Created
February 3, 2017 14:51
-
-
Save anonymous/bb3afe7d59c9efae1b0a60b2f7bf3b72 to your computer and use it in GitHub Desktop.
Roulette Game using TinyCLR-OS and Gadgeteer FEZ Reaper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading; | |
using GHIElectronics.TinyCLR.Pins; | |
using GHIElectronics.TinyCLR.Devices.Gpio; | |
using GHIElectronics.TinyCLR.Devices.Pwm; | |
namespace Roulette { | |
public class Program { | |
public static GpioPin[] LED = new GpioPin[48]; | |
public static bool buttonPressed = false; | |
public static int lastNum = 0;//Holds the number of the last LED to light | |
public static int position = 0;//Holds the current positon of the LED light. | |
public static GpioPin button01; | |
public static GpioPin button02; | |
public static GpioPin button03; | |
public static GpioPin button04; | |
public static GpioController gpio = GpioController.GetDefault(); // Get the default GPIO controller | |
public static PwmController pwm = PwmController.GetDefault(); // Get the default PWM controller | |
public static PwmPin tunesPin; | |
public static bool player1Played = false; //Determines if the player has used his turn on on a round | |
public static bool player2Played = false; | |
public static bool player3Played = false; | |
public static bool player4Played = false; | |
public static int player1Score = 0; | |
public static int player2Score = 0; | |
public static int player3Score = 0; | |
public static int player4Score = 0; | |
public static int speed = 100;// Starting speed/sleeptime is decreased by 10 each time a player wins | |
public static int winningScore = 5;// Starting speed/sleeptime is decreased by 5 each time a player wins | |
public static void Main() { | |
InitializeGPIO(); | |
tunesPin = pwm.OpenPin(G80.PwmOutput.PD15); //Socket 4 Pin 9 FEZ Reaper | |
while (true) { | |
setLED(position); | |
playBlip(); | |
position++; | |
//After reaching the 48th LED in the circle the value is set back to the beginning | |
if (position == 48) { | |
position = 0; | |
} | |
Thread.Sleep(speed); | |
//player1 | |
if (position <= 5 || position >= 44) { | |
if (button01.Read() == GpioPinValue.Low && player1Played == false) { | |
if (position == 1) { | |
playWinningTune(); | |
player1Score++; | |
if (player1Score == winningScore) | |
restart(0); | |
else | |
increaseSpeed(); | |
} | |
else { | |
playLosingTune(); | |
} | |
Thread.Sleep(100); | |
player1Played = true; | |
} | |
} | |
else { | |
player1Played = false; | |
} | |
//player2 | |
if (position >= 7 && position <= 17) { | |
if (button02.Read() == GpioPinValue.Low && player2Played == false) { | |
if (position == 13) { | |
playWinningTune(); | |
player2Score++; | |
if (player2Score == winningScore) | |
restart(12); | |
else | |
increaseSpeed(); | |
} | |
else { | |
playLosingTune(); | |
} | |
Thread.Sleep(100); | |
player2Played = true; | |
} | |
} | |
else { | |
player2Played = false; | |
} | |
//player3 | |
if (position >= 19 && position <= 30) { | |
if (button03.Read() == GpioPinValue.Low && player3Played == false) { | |
if (position == 25) { | |
playWinningTune(); | |
player3Score++; | |
if (player3Score == winningScore) | |
restart(24); | |
else | |
increaseSpeed(); | |
} | |
else { | |
playLosingTune(); | |
} | |
Thread.Sleep(100); | |
player3Played = true; | |
} | |
} | |
else { | |
player3Played = false; | |
} | |
//player4 | |
if (position >= 32 && position <= 42) { | |
if (button04.Read() == GpioPinValue.Low && player4Played == false) { | |
if (position == 37) { | |
playWinningTune(); | |
player4Score++; | |
if (player4Score == winningScore) | |
restart(36); | |
else | |
increaseSpeed(); | |
} | |
else { | |
playLosingTune(); | |
} | |
Thread.Sleep(100); | |
player4Played = true; | |
} | |
} | |
else { | |
player4Played = false; | |
} | |
} | |
} | |
public static void InitializeGPIO() { | |
//Socket 3 FEZ Reaper | |
button01 = gpio.OpenPin(G80.Gpio.PE7); | |
button02 = gpio.OpenPin(G80.Gpio.PD8); | |
button03 = gpio.OpenPin(G80.Gpio.PD9); | |
button04 = gpio.OpenPin(G80.Gpio.PD12); | |
//Socket 4 FEZ Reaper | |
LED[0] = gpio.OpenPin(G80.Gpio.PC13); | |
LED[1] = gpio.OpenPin(G80.Gpio.PB13); | |
LED[2] = gpio.OpenPin(G80.Gpio.PB12); | |
LED[3] = gpio.OpenPin(G80.Gpio.PA15); | |
LED[4] = gpio.OpenPin(G80.Gpio.PE11); | |
LED[5] = gpio.OpenPin(G80.Gpio.PE13); | |
LED[6] = gpio.OpenPin(G80.Gpio.PE14); | |
//Socket 7 FEZ Reaper | |
LED[7] = gpio.OpenPin(G80.Gpio.PE5); | |
LED[8] = gpio.OpenPin(G80.Gpio.PA9); | |
LED[9] = gpio.OpenPin(G80.Gpio.PA10); | |
LED[10] = gpio.OpenPin(G80.Gpio.PE0); | |
LED[11] = gpio.OpenPin(G80.Gpio.PB8); | |
LED[12] = gpio.OpenPin(G80.Gpio.PB9); | |
LED[13] = gpio.OpenPin(G80.Gpio.PB11); | |
//Socket 8 FEZ Reaper | |
LED[14] = gpio.OpenPin(G80.Gpio.PC0); | |
LED[15] = gpio.OpenPin(G80.Gpio.PC1); | |
LED[16] = gpio.OpenPin(G80.Gpio.PA4); | |
LED[17] = gpio.OpenPin(G80.Gpio.PC6); | |
LED[18] = gpio.OpenPin(G80.Gpio.PB5); | |
LED[19] = gpio.OpenPin(G80.Gpio.PB4); | |
LED[20] = gpio.OpenPin(G80.Gpio.PB3); | |
//Socket 9 FEZ Reaper | |
LED[21] = gpio.OpenPin(G80.Gpio.PC4); | |
LED[22] = gpio.OpenPin(G80.Gpio.PC5); | |
LED[23] = gpio.OpenPin(G80.Gpio.PA5); | |
LED[24] = gpio.OpenPin(G80.Gpio.PD7); | |
LED[25] = gpio.OpenPin(G80.Gpio.PC3); | |
LED[26] = gpio.OpenPin(G80.Gpio.PC2); | |
LED[27] = gpio.OpenPin(G80.Gpio.PB10); | |
//Socket 11 FEZ Reaper | |
LED[28] = gpio.OpenPin(G80.Gpio.PE9); | |
LED[29] = gpio.OpenPin(G80.Gpio.PC8); | |
LED[30] = gpio.OpenPin(G80.Gpio.PC9); | |
LED[31] = gpio.OpenPin(G80.Gpio.PD2); | |
LED[32] = gpio.OpenPin(G80.Gpio.PC10); | |
LED[33] = gpio.OpenPin(G80.Gpio.PC11); | |
LED[34] = gpio.OpenPin(G80.Gpio.PC12); | |
//Socket 5 FEZ Reaper | |
LED[35] = gpio.OpenPin(G80.Gpio.PA3); | |
LED[36] = gpio.OpenPin(G80.Gpio.PA6); | |
LED[37] = gpio.OpenPin(G80.Gpio.PA7); | |
LED[38] = gpio.OpenPin(G80.Gpio.PE8); | |
//Socket 6 FEZ Reaper | |
LED[39] = gpio.OpenPin(G80.Gpio.PA8); | |
LED[40] = gpio.OpenPin(G80.Gpio.PA0); | |
LED[41] = gpio.OpenPin(G80.Gpio.PA1); | |
//Socket 13 FEZ Reaper | |
LED[42] = gpio.OpenPin(G80.Gpio.PA2); | |
LED[43] = gpio.OpenPin(G80.Gpio.PB0); | |
LED[44] = gpio.OpenPin(G80.Gpio.PB1); | |
//Socket 10 FEZ Reaper | |
LED[45] = gpio.OpenPin(G80.Gpio.PE6); | |
LED[46] = gpio.OpenPin(G80.Gpio.PD5); | |
LED[47] = gpio.OpenPin(G80.Gpio.PD6); | |
//Set DriveMode for all LEDs | |
for (int x = 0; x < LED.Length; x++) { | |
LED[x].SetDriveMode(GpioPinDriveMode.Output); | |
} | |
//Set DriveMode with Pullup for Buttons | |
button01.SetDriveMode(GpioPinDriveMode.InputPullUp); | |
button02.SetDriveMode(GpioPinDriveMode.InputPullUp); | |
button03.SetDriveMode(GpioPinDriveMode.InputPullUp); | |
button04.SetDriveMode(GpioPinDriveMode.InputPullUp); | |
} | |
public static void setLED(int num) { | |
LED[lastNum].Write(GpioPinValue.Low); | |
LED[num].Write(GpioPinValue.High); | |
lastNum = num; | |
} | |
public static void playLosingTune() { | |
pwm.SetDesiredFrequency(125.5); | |
tunesPin.Start(); | |
tunesPin.SetActiveDutyCyclePercentage(0.5); | |
Thread.Sleep(400); | |
tunesPin.Stop(); | |
pwm.SetDesiredFrequency(58.5); | |
tunesPin.Start(); | |
tunesPin.SetActiveDutyCyclePercentage(0.5); | |
Thread.Sleep(100); | |
tunesPin.Stop(); | |
} | |
public static void playWinningTune() { | |
pwm.SetDesiredFrequency(329.628); | |
tunesPin.Start(); | |
tunesPin.SetActiveDutyCyclePercentage(0.5); | |
Thread.Sleep(400); | |
tunesPin.Stop(); | |
pwm.SetDesiredFrequency(349.228); | |
tunesPin.Start(); | |
tunesPin.SetActiveDutyCyclePercentage(0.5); | |
Thread.Sleep(100); | |
tunesPin.Stop(); | |
} | |
public static void playBlip() { | |
pwm.SetDesiredFrequency(58); | |
tunesPin.Start(); | |
tunesPin.SetActiveDutyCyclePercentage(0.5); | |
Thread.Sleep(20); | |
tunesPin.Stop(); | |
} | |
public static void increaseSpeed() { | |
speed = speed - 10; | |
if (speed <= 10) | |
speed = 10; | |
} | |
public static void restart(int num) { | |
playWinningTune(); | |
playLosingTune(); | |
turnOffAllLED(); | |
///Flashes the winning players LED | |
for (int i = 0; i < 5; i++) { | |
LED[num].Write(GpioPinValue.High); | |
Thread.Sleep(500); | |
LED[num].Write(GpioPinValue.Low); | |
} | |
speed = 100; | |
player1Score = 0; | |
player2Score = 0; | |
player3Score = 0; | |
player4Score = 0; | |
} | |
public static void turnOnAllLED() | |
{ | |
for (int x = 0; x < LED.Length; x++) | |
{ | |
LED[x].Write(GpioPinValue.High); | |
} | |
} | |
public static void turnOffAllLED() | |
{ | |
for (int x = 0; x < LED.Length; x++) | |
{ | |
LED[x].Write(GpioPinValue.Low); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment