cstram
Published © GPL3+

Spin The Wheel!

Today I built with Arduino a simple game for 2 players. Each player have to spin the wheel and the winner is the player that scores higher!

BeginnerFull instructions provided1,421
Spin The Wheel!

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Buzzer
Buzzer
×1
WS2812 Addressable LED Strip
Digilent WS2812 Addressable LED Strip
×1
LED (generic)
LED (generic)
×2
7 Segment 4 digit display
×1
Old Hard Drive Motor
×1

Hand tools and fabrication machines

Drill / Driver, Cordless
Drill / Driver, Cordless

Story

Read more

Schematics

Spin The Wheel Diagram

Use it to build the game

Code

Spin The Wheel Arduino Code

Arduino
Program the Arduino Nano
/**
Spin the Wheel! 
**/

#include <TM1637Display.h>
#include <Adafruit_NeoPixel.h>

// Display Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3

// Data PIN, led count and declaration of the led strip
#define LED_PIN    5
#define LED_COUNT 11
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

TM1637Display display(CLK, DIO);

const uint8_t SEG_SPIN[] = {
  SEG_A | SEG_C | SEG_D | SEG_F | SEG_G,           // S
  SEG_A | SEG_B | SEG_E | SEG_F | SEG_G,           // P
  SEG_E | SEG_F,                                   // I
  SEG_C | SEG_E | SEG_G                            // n
  };

const uint8_t SEG_WAIT[] = {
  SEG_G,                                           // -
  SEG_G,                                           // -
  SEG_G,                                           // -
  SEG_G                                            // -
  };

const uint8_t SEG_P1[] = {
  SEG_G,                                           // -
  SEG_A | SEG_B | SEG_E | SEG_F | SEG_G,           // P
  SEG_B | SEG_C,                                   // 1
  SEG_G                                            // -
  };

const uint8_t SEG_P2[] = {
  SEG_G,                                           // -
  SEG_A | SEG_B | SEG_E | SEG_F | SEG_G,           // P
  SEG_A | SEG_B | SEG_D | SEG_E | SEG_G,           // 2
  SEG_G                                            // -
  };

// notes in the melody:
int melody[] = {
  262, 196, 196, 220, 196, 0, 247, 262
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

int statusWheel = -1;
long randomVal = 0;
int p1 = 0;
int p2 = 0;
int i = 0;
int j = 0;
int Player = 1;
int Player1 = 0;
int Player2 = 0;

void setup() 
{
  Serial.begin(9600);
  Serial.println("Starting......");
  pinMode(8, OUTPUT);
  pinMode(10, OUTPUT);
  randomSeed(analogRead(0));
  display.setBrightness(0x01);
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
  gameStart();
}

boolean readWheel() {
  p1 = analogRead(1);
  p2 = analogRead(2);

  if (p1+p2 < 35)
    return false;
  else
    return true;
}


void loop()
{
  if (readWheel() and Player == 1) {
    Serial.print("P1 = ");
    Serial.println(p1);
    Serial.print("P2 = ");
    Serial.println(p2);
    j=0;
    tone(6, 192, 250);
    randomVal = random(10000);
    Serial.println(randomVal);
    display.showNumberDec(randomVal, false);
    if (i>=LED_COUNT)
      i=0;      
    strip.setPixelColor(i, strip.Color(127,   0,   0)); 
    strip.show();
    i++;     
    delay(80);
    noTone(6);
  }
  delay(20);
  j++;
  if (randomVal != 0 and Player == 1 and j>= 100){
    Serial.println("Finish P1");
    j=0;
    Player1 = randomVal;
    randomVal = 0;
    Player = 2;
    display.setSegments(SEG_P2);
    digitalWrite(10, LOW);
    delay (2000);
    display.setSegments(SEG_SPIN);    
    digitalWrite(8, HIGH);
    }

  if (readWheel() and Player == 2) {
    Serial.print("P1 = ");
    Serial.println(p1);
    Serial.print("P2 = ");
    Serial.println(p2);
    j=0;
    tone(6, 192, 250);
    randomVal = random(10000);
    Serial.println(randomVal);
    display.showNumberDec(randomVal, false);
    if (i>=LED_COUNT)
      i=0;      
    strip.setPixelColor(i, strip.Color(127,   127,   0)); 
    strip.show();
    i++;     
    delay(80);
    noTone(6);
  }
  delay(20);

  if (randomVal != 0 and Player == 2 and j>= 100){
    Serial.println("Finish P2");
    j=0;
    Player2 = randomVal;
    randomVal = 0;
    Player = 0;
    if (Player1 > Player2) {
      display.setSegments(SEG_WAIT);
      digitalWrite(8, LOW);
      digitalWrite(10, LOW);
      delay (2000);
      display.setSegments(SEG_P1);
      digitalWrite(8, LOW);
      digitalWrite(10, HIGH);
      victory();
      colorWipe(strip.Color(64,   0,   0), 50);
      colorWipe(strip.Color(128,   0,   0), 50);
      colorWipe(strip.Color(192,   0,   0), 50);
      colorWipe(strip.Color(255,   0,   0), 50);
      }
    else {
      display.setSegments(SEG_WAIT);
      digitalWrite(8, LOW);
      digitalWrite(10, LOW);
      delay (2000);
      display.setSegments(SEG_P2);
      digitalWrite(10, LOW);
      digitalWrite(8, HIGH);
      victory();
      colorWipe(strip.Color(64,   64,   0), 50);
      colorWipe(strip.Color(128,   128,   0), 50);
      colorWipe(strip.Color(192,   192,   0), 50);
      colorWipe(strip.Color(255,   255,   0), 50);
      }
    }
  if (readWheel() and Player == 0)
    gameStart();
}

void theaterChase(uint32_t color, int wait) {
  for(int a=0; a<10; a++) {  // Repeat 10 times...
    for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...
      strip.clear();         //   Set all pixels in RAM to 0 (off)
      // 'c' counts up from 'b' to end of strip in steps of 3...
      for(int c=b; c<strip.numPixels(); c += 3) {
        strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
      }
      strip.show(); // Update strip with new contents
      delay(wait);  // Pause for a moment
    }
  }
}

void colorWipe(uint32_t color, int wait) {
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}

void gameStart() {
  statusWheel = -1;
  randomVal = 0;
  p1 = 0;
  p2 = 0;
  i = 0;
  j = 0;
  Player = 1;
  Player1 = 0;
  Player2 = 0;

  digitalWrite(10, LOW);
  digitalWrite(8, LOW);
  display.setSegments(SEG_WAIT);

  theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness
  theaterChase(strip.Color(127,   0,   0), 50); // Red, half brightness
  theaterChase(strip.Color(  0,   0, 127), 50); // Blue, half brightness
  colorWipe(strip.Color(  0,   0, 0), 50); // All switched off

  display.setSegments(SEG_P1);
  delay (2000);
  display.setSegments(SEG_SPIN);
  digitalWrite(10, HIGH);
}

void victory() {
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(6, melody[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(6);
  }
}

Credits

cstram
16 projects • 23 followers
Passionate about IT, Electronics and DIY. Strong believer in Raspberry and Arduino devices. Experience in digital television and security.
Contact

Comments

Please log in or sign up to comment.