markruff icon

Arduino twister spinner

markruff | PRO | 01/21/16 03:32:29 AM UTC | 0 ⭐ | 402 👁️ | Never ⏰ | []
C++ |

2.91 KB

|

None

|

0 👍

/

0 👎

/*
 * TWISTER SPINNER
 * 
 * Randomly lights a colour LED (red, yellow, green, blue) to correspond
 * to twister colours and turns the servo to one of four angles (to
 * correspond to a chart with right/left hand/foot).
 * 
 * At selection randomly changes lights and spins servo a random number
 * of times to simulate spinning of the wheel
 * 
 * Turns last a set amount of time (turnTime). A warning starts when
 * 80% of the turn time has expired. Warning involves tone played on piezo
 * 
 * When turn ends piezo plays final (ominous) note and indicator LED flashes.
 * The next spin then occurs
 * 
 * As if Twister doesn't get hard enough as body parts and bodies contort
 * around each other. To make things more difficult, we reduce the time between spins
 * each turn :D
 * 
 * Mark Ruff, Jan 2016
 */
 
#include <Servo.h>
 
// GLOBALS
 
// servo tells us which limb
Servo limbServo;
int limbAngles[] = { 20, 60, 100, 140 };
 
// LED pins
// The pin numbers matter, because I have hard coded them into
// loops later on... my bad.
int redLed = 7;
int yellowLed = 6;
int greenLed = 5;
int blueLed = 4;
 
int speaker = 13;
int indicatorLed = 3;
 
// timing
int timer;
int prevMillis;
int turnTime = 15000;
int warningTime;
int turnTimeDecrement = 200;
int turnTimeMin = 5000;
bool newTurn = true;
 
// notes for the warning bell
int notes[] = { 262, 195, 235, 176, 99 };
 
void setup() {
  limbServo.attach(10);
 
  // set all LED pins to output
  for ( int i = 3; i < 8 ; i++ ) {
    pinMode(i, OUTPUT);
  }
}
 
void loop() {
 
  // increment the timer
  timer += millis() - prevMillis;
  prevMillis = millis();
 
  // handle new turn
  if ( newTurn ) {
    // random dance for a bit then end on the real thing
    for (int i = 0 ; i < random(8,12) ; i++ ) {
      // turn off all the color LEDs
      for (int n = 4 ; n < 8 ; n++ ) {
        digitalWrite(n, LOW);
      }
      // write high to a random color LED
      digitalWrite(random(4,8),HIGH);
 
      // select a random limb      
      limbServo.write(limbAngles[random(0,4)]);
      delay(250);
    }
    newTurn = false;
    timer = 0;
    turnTime = max ( turnTime - turnTimeDecrement, turnTimeMin );
    warningTime = (turnTime * 0.8);
  }
  // handle time up
  else if ( timer > turnTime ) {
    // turn over tone and indicator lights flash
    tone(speaker, notes[4], 2000);
    for (int i = 0 ; i < 5 ; i++ ) {
      digitalWrite(indicatorLed, HIGH);
      delay(200);
      digitalWrite(indicatorLed, LOW);
      delay(200);
    }
    // turn over, just start a new turn
    newTurn = true;
  }
  // handle warning time
  else if ( timer > warningTime ) {
    // warning tone
    int noteDuration = turnTime/60;
    for ( int i = 0 ; i < 4 ; i++ ) {
      tone(speaker,notes[i], noteDuration - 20 );
      delay(noteDuration);
    }
 
  }
  // otherwise just keep the LEDs lit and pass the time... loop!
}

Comments