Advertisement
markruff

Arduino twister spinner

Jan 20th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. /*
  2.  * TWISTER SPINNER
  3.  *
  4.  * Randomly lights a colour LED (red, yellow, green, blue) to correspond
  5.  * to twister colours and turns the servo to one of four angles (to
  6.  * correspond to a chart with right/left hand/foot).
  7.  *
  8.  * At selection randomly changes lights and spins servo a random number
  9.  * of times to simulate spinning of the wheel
  10.  *
  11.  * Turns last a set amount of time (turnTime). A warning starts when
  12.  * 80% of the turn time has expired. Warning involves tone played on piezo
  13.  *
  14.  * When turn ends piezo plays final (ominous) note and indicator LED flashes.
  15.  * The next spin then occurs
  16.  *
  17.  * As if Twister doesn't get hard enough as body parts and bodies contort
  18.  * around each other. To make things more difficult, we reduce the time between spins
  19.  * each turn :D
  20.  *
  21.  * Mark Ruff, Jan 2016
  22.  */
  23.  
  24. #include <Servo.h>
  25.  
  26. // GLOBALS
  27.  
  28. // servo tells us which limb
  29. Servo limbServo;
  30. int limbAngles[] = { 20, 60, 100, 140 };
  31.  
  32. // LED pins
  33. // The pin numbers matter, because I have hard coded them into
  34. // loops later on... my bad.
  35. int redLed = 7;
  36. int yellowLed = 6;
  37. int greenLed = 5;
  38. int blueLed = 4;
  39.  
  40. int speaker = 13;
  41. int indicatorLed = 3;
  42.  
  43. // timing
  44. int timer;
  45. int prevMillis;
  46. int turnTime = 15000;
  47. int warningTime;
  48. int turnTimeDecrement = 200;
  49. int turnTimeMin = 5000;
  50. bool newTurn = true;
  51.  
  52. // notes for the warning bell
  53. int notes[] = { 262, 195, 235, 176, 99 };
  54.  
  55. void setup() {
  56.   limbServo.attach(10);
  57.  
  58.   // set all LED pins to output
  59.   for ( int i = 3; i < 8 ; i++ ) {
  60.     pinMode(i, OUTPUT);
  61.   }
  62. }
  63.  
  64. void loop() {
  65.  
  66.   // increment the timer
  67.   timer += millis() - prevMillis;
  68.   prevMillis = millis();
  69.  
  70.   // handle new turn
  71.   if ( newTurn ) {
  72.     // random dance for a bit then end on the real thing
  73.     for (int i = 0 ; i < random(8,12) ; i++ ) {
  74.       // turn off all the color LEDs
  75.       for (int n = 4 ; n < 8 ; n++ ) {
  76.         digitalWrite(n, LOW);
  77.       }
  78.       // write high to a random color LED
  79.       digitalWrite(random(4,8),HIGH);
  80.  
  81.       // select a random limb      
  82.       limbServo.write(limbAngles[random(0,4)]);
  83.       delay(250);
  84.     }
  85.     newTurn = false;
  86.     timer = 0;
  87.     turnTime = max ( turnTime - turnTimeDecrement, turnTimeMin );
  88.     warningTime = (turnTime * 0.8);
  89.   }
  90.   // handle time up
  91.   else if ( timer > turnTime ) {
  92.     // turn over tone and indicator lights flash
  93.     tone(speaker, notes[4], 2000);
  94.     for (int i = 0 ; i < 5 ; i++ ) {
  95.       digitalWrite(indicatorLed, HIGH);
  96.       delay(200);
  97.       digitalWrite(indicatorLed, LOW);
  98.       delay(200);
  99.     }
  100.     // turn over, just start a new turn
  101.     newTurn = true;
  102.   }
  103.   // handle warning time
  104.   else if ( timer > warningTime ) {
  105.     // warning tone
  106.     int noteDuration = turnTime/60;
  107.     for ( int i = 0 ; i < 4 ; i++ ) {
  108.       tone(speaker,notes[i], noteDuration - 20 );
  109.       delay(noteDuration);
  110.     }
  111.  
  112.   }
  113.   // otherwise just keep the LEDs lit and pass the time... loop!
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement