Components and supplies
7 Segment 4 digit display
LED (generic)
Old Hard Drive Motor
Buzzer
WS2812 Addressable LED Strip
Arduino Nano R3
Tools and machines
Drill / Driver, Cordless
Project description
Code
Spin The Wheel Arduino Code
arduino
Program the Arduino Nano
1/** 2Spin the Wheel! 3**/ 4 5#include <TM1637Display.h> 6#include <Adafruit_NeoPixel.h> 7 8// Display Module connection pins (Digital Pins) 9#define CLK 2 10#define DIO 3 11 12// Data PIN, led count and declaration of the led strip 13#define LED_PIN 5 14#define LED_COUNT 11 15Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); 16 17TM1637Display display(CLK, DIO); 18 19const uint8_t SEG_SPIN[] = { 20 SEG_A | SEG_C | SEG_D | SEG_F | SEG_G, // S 21 SEG_A | SEG_B | SEG_E | SEG_F | SEG_G, // P 22 SEG_E | SEG_F, // I 23 SEG_C | SEG_E | SEG_G // n 24 }; 25 26const uint8_t SEG_WAIT[] = { 27 SEG_G, // - 28 SEG_G, // - 29 SEG_G, // - 30 SEG_G // - 31 }; 32 33const uint8_t SEG_P1[] = { 34 SEG_G, // - 35 SEG_A | SEG_B | SEG_E | SEG_F | SEG_G, // P 36 SEG_B | SEG_C, // 1 37 SEG_G // - 38 }; 39 40const uint8_t SEG_P2[] = { 41 SEG_G, // - 42 SEG_A | SEG_B | SEG_E | SEG_F | SEG_G, // P 43 SEG_A | SEG_B | SEG_D | SEG_E | SEG_G, // 2 44 SEG_G // - 45 }; 46 47// notes in the melody: 48int melody[] = { 49 262, 196, 196, 220, 196, 0, 247, 262 50}; 51 52// note durations: 4 = quarter note, 8 = eighth note, etc.: 53int noteDurations[] = { 54 4, 8, 8, 4, 4, 4, 4, 4 55}; 56 57int statusWheel = -1; 58long randomVal = 0; 59int p1 = 0; 60int p2 = 0; 61int i = 0; 62int j = 0; 63int Player = 1; 64int Player1 = 0; 65int Player2 = 0; 66 67void setup() 68{ 69 Serial.begin(9600); 70 Serial.println("Starting......"); 71 pinMode(8, OUTPUT); 72 pinMode(10, OUTPUT); 73 randomSeed(analogRead(0)); 74 display.setBrightness(0x01); 75 strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) 76 strip.show(); // Turn OFF all pixels ASAP 77 strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255) 78 gameStart(); 79} 80 81boolean readWheel() { 82 p1 = analogRead(1); 83 p2 = analogRead(2); 84 85 if (p1+p2 < 35) 86 return false; 87 else 88 return true; 89} 90 91 92void loop() 93{ 94 if (readWheel() and Player == 1) { 95 Serial.print("P1 = "); 96 Serial.println(p1); 97 Serial.print("P2 = "); 98 Serial.println(p2); 99 j=0; 100 tone(6, 192, 250); 101 randomVal = random(10000); 102 Serial.println(randomVal); 103 display.showNumberDec(randomVal, false); 104 if (i>=LED_COUNT) 105 i=0; 106 strip.setPixelColor(i, strip.Color(127, 0, 0)); 107 strip.show(); 108 i++; 109 delay(80); 110 noTone(6); 111 } 112 delay(20); 113 j++; 114 if (randomVal != 0 and Player == 1 and j>= 100){ 115 Serial.println("Finish P1"); 116 j=0; 117 Player1 = randomVal; 118 randomVal = 0; 119 Player = 2; 120 display.setSegments(SEG_P2); 121 digitalWrite(10, LOW); 122 delay (2000); 123 display.setSegments(SEG_SPIN); 124 digitalWrite(8, HIGH); 125 } 126 127 if (readWheel() and Player == 2) { 128 Serial.print("P1 = "); 129 Serial.println(p1); 130 Serial.print("P2 = "); 131 Serial.println(p2); 132 j=0; 133 tone(6, 192, 250); 134 randomVal = random(10000); 135 Serial.println(randomVal); 136 display.showNumberDec(randomVal, false); 137 if (i>=LED_COUNT) 138 i=0; 139 strip.setPixelColor(i, strip.Color(127, 127, 0)); 140 strip.show(); 141 i++; 142 delay(80); 143 noTone(6); 144 } 145 delay(20); 146 147 if (randomVal != 0 and Player == 2 and j>= 100){ 148 Serial.println("Finish P2"); 149 j=0; 150 Player2 = randomVal; 151 randomVal = 0; 152 Player = 0; 153 if (Player1 > Player2) { 154 display.setSegments(SEG_WAIT); 155 digitalWrite(8, LOW); 156 digitalWrite(10, LOW); 157 delay (2000); 158 display.setSegments(SEG_P1); 159 digitalWrite(8, LOW); 160 digitalWrite(10, HIGH); 161 victory(); 162 colorWipe(strip.Color(64, 0, 0), 50); 163 colorWipe(strip.Color(128, 0, 0), 50); 164 colorWipe(strip.Color(192, 0, 0), 50); 165 colorWipe(strip.Color(255, 0, 0), 50); 166 } 167 else { 168 display.setSegments(SEG_WAIT); 169 digitalWrite(8, LOW); 170 digitalWrite(10, LOW); 171 delay (2000); 172 display.setSegments(SEG_P2); 173 digitalWrite(10, LOW); 174 digitalWrite(8, HIGH); 175 victory(); 176 colorWipe(strip.Color(64, 64, 0), 50); 177 colorWipe(strip.Color(128, 128, 0), 50); 178 colorWipe(strip.Color(192, 192, 0), 50); 179 colorWipe(strip.Color(255, 255, 0), 50); 180 } 181 } 182 if (readWheel() and Player == 0) 183 gameStart(); 184} 185 186void theaterChase(uint32_t color, int wait) { 187 for(int a=0; a<10; a++) { // Repeat 10 times... 188 for(int b=0; b<3; b++) { // 'b' counts from 0 to 2... 189 strip.clear(); // Set all pixels in RAM to 0 (off) 190 // 'c' counts up from 'b' to end of strip in steps of 3... 191 for(int c=b; c<strip.numPixels(); c += 3) { 192 strip.setPixelColor(c, color); // Set pixel 'c' to value 'color' 193 } 194 strip.show(); // Update strip with new contents 195 delay(wait); // Pause for a moment 196 } 197 } 198} 199 200void colorWipe(uint32_t color, int wait) { 201 for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip... 202 strip.setPixelColor(i, color); // Set pixel's color (in RAM) 203 strip.show(); // Update strip to match 204 delay(wait); // Pause for a moment 205 } 206} 207 208void gameStart() { 209 statusWheel = -1; 210 randomVal = 0; 211 p1 = 0; 212 p2 = 0; 213 i = 0; 214 j = 0; 215 Player = 1; 216 Player1 = 0; 217 Player2 = 0; 218 219 digitalWrite(10, LOW); 220 digitalWrite(8, LOW); 221 display.setSegments(SEG_WAIT); 222 223 theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness 224 theaterChase(strip.Color(127, 0, 0), 50); // Red, half brightness 225 theaterChase(strip.Color( 0, 0, 127), 50); // Blue, half brightness 226 colorWipe(strip.Color( 0, 0, 0), 50); // All switched off 227 228 display.setSegments(SEG_P1); 229 delay (2000); 230 display.setSegments(SEG_SPIN); 231 digitalWrite(10, HIGH); 232} 233 234void victory() { 235 for (int thisNote = 0; thisNote < 8; thisNote++) { 236 int noteDuration = 1000 / noteDurations[thisNote]; 237 tone(6, melody[thisNote], noteDuration); 238 int pauseBetweenNotes = noteDuration * 1.30; 239 delay(pauseBetweenNotes); 240 noTone(6); 241 } 242} 243
Downloadable files
Spin The Wheel Diagram
Use it to build the game
Spin The Wheel Diagram
Spin The Wheel Diagram
Use it to build the game
Spin The Wheel Diagram
Comments
Only logged in users can leave comments