Sfoglia il codice sorgente

Updates to use LED rings and switches instead of load cells

Fred Damstra (Macbook 2015) 5 mesi fa
parent
commit
6dd7aa9230
1 ha cambiato i file con 158 aggiunte e 125 eliminazioni
  1. 158 125
      CiderRacer2600.ino

+ 158 - 125
CiderRaceTrack.ino → CiderRacer2600.ino

@@ -9,29 +9,30 @@
 
    Pin Assignments per Default Sketch:
      Digital:
-       2 - Left Player Display
-       3 - Right Player Display
-       4 - unused (RX for left display)
-       5 - unused (RX for right display)
-       13 - Left Ready Button
-       7 - Left Pressure Simulator
+       2 - Right Player Display
+       3 - Left Player Display
+       4 - unused (RX for right display)
+       5 - unused (RX for left display)
+       A0 - Left Pressure Simulator
        8 - Right Ready Button
        9 - Buzzer (+)
-       10 - Right Pressure Simularor
-       11 - Left Player Light Tree (6 lights) (If WS2812B)
-       12 - Right Player Light Tree (6 lights)(If WS2812B)
+       A7 - Right Pressure Simularor
+       11 - Left Player Light Ring
+       12 - Right Player Light Ring
+       13 - Left Ready Button
        30-39 - Colored LEDs
-
+    
      Analog:
-       0 - Right Player Pressure Sensor
-       1 - Left Player Pressure Sensor
-       7 - Unused; Seeds RNG.
+       A0 - Left Player Pressure Sensor [Digital]
+       A7 - Right Player Pressure Sensor [Digital]
+       A5 - Unused; Seeds RNG.
 
 
    Fred Damstra
    fred.damstra@gmail.com
 
    2016-09-29 - Initial Draft
+   2025-02-09 - Revised for mouse switches and LED rings
 
    Licensed under GPLv3: https://www.gnu.org/licenses/gpl.html
 */
@@ -51,14 +52,14 @@
     Lots of help for this example at
     https://learn.sparkfun.com/tutorials/sik-experiment-guide-for-arduino---v32/experiment-11-using-a-piezo-buzzer
 */
-const int debug = 0;
+const int debug = 1;
 
 const int buzzerPin = 9;
 const unsigned long songFrequency = 90000; // How frequently the song may play in seconds
 const float songProbability = 0.0; // Probability that the song will play during that frequency
 
 /* randomPin should be an unused analog pin for seeding the RNG */
-const int randomPin = 5;
+const int randomPin = A5;
 
 /* WS2812 Christmas Tree Setup:
     Two strips of 6 LEDs connected to 5V and Ground, plus the signal pin
@@ -87,18 +88,18 @@ const unsigned long topColor   = PURPLE; /* Top LED is constant to show power */
 /* 7-Segment Display Setup
     Two 7 OpenSegment displays from SparkFun in serial mode.
 */
-const int leftDisplayTX  = 2;
-const int rightDisplayTX = 3;
-const int leftDisplayRX  = 4; /* Unused */
-const int rightDisplayRX = 5; /* Unused */
+const int leftDisplayTX  = 3;
+const int rightDisplayTX = 2;
+const int leftDisplayRX  = 5; /* Unused */
+const int rightDisplayRX = 4; /* Unused */
 
 /* Force Sensitive Resistor Switches (Analog inputs) */
-const int leftFSR  = A7;
-const int rightFSR = A0;
-const int leftFSRThreshold  = 10; /* > this number is closed */
-const int rightFSRThreshold = 10;
-const int leftFSRSimPin = 7; /* For when the FSRs aren't there */
-const int rightFSRSimPin = 10; /* For when the FSRs aren't there */
+//const int leftFSR  = A7;
+//const int rightFSR = A0;
+//const int leftFSRThreshold  = 10; /* > this number is closed */
+//const int rightFSRThreshold = 10;
+const int leftFSRSimPin = A7;
+const int rightFSRSimPin = A0;
 
 /* Start Button */
 const int leftReadyButtonPin = 13;
@@ -125,7 +126,7 @@ const int clearPin = 53; /* White */
 /***********
    Application Constants
 */
-const int LED_COUNT = 6;
+const int LED_COUNT = 24;
 
 const unsigned int buzzerFrequency = 70;
 const int buzzerDuration = 1800;
@@ -140,8 +141,8 @@ const unsigned long debounceDelay = 250;
    Application Variables
 */
 unsigned long lastSongCheck = 0;
-Adafruit_NeoPixel rightTree = Adafruit_NeoPixel(LED_COUNT, ledPinRight, NEO_GRB + NEO_KHZ800);
-Adafruit_NeoPixel leftTree  = Adafruit_NeoPixel(LED_COUNT, ledPinLeft,  NEO_GRB + NEO_KHZ800);
+Adafruit_NeoPixel rightRing = Adafruit_NeoPixel(LED_COUNT + 1, ledPinRight, NEO_GRB + NEO_KHZ800);
+Adafruit_NeoPixel leftRing  = Adafruit_NeoPixel(LED_COUNT + 1, ledPinLeft,  NEO_GRB + NEO_KHZ800);
 
 SoftwareSerial leftDisplay(leftDisplayRX, leftDisplayTX);
 SoftwareSerial rightDisplay(rightDisplayRX, rightDisplayTX);
@@ -192,8 +193,8 @@ void setup() {
 
 
   /* WS2812 LED Setup */
-  rightTree.begin();
-  leftTree.begin();
+  rightRing.begin();
+  leftRing.begin();
   /* Digital LED Setup */
   for(int i = 0; i < 5; i++) {
     pinMode(ledPinsLeft[i], OUTPUT);
@@ -223,8 +224,8 @@ void setup() {
   pinMode(rightReadyButtonPin, INPUT_PULLUP);
   pinMode(leftFSRSimPin, INPUT_PULLUP);
   pinMode(rightFSRSimPin, INPUT_PULLUP);
-  pinMode(leftFSR, INPUT);
-  pinMode(rightFSR, INPUT);
+  //pinMode(leftFSR, INPUT_PULLUP);
+  //pinMode(rightFSR, INPUT_PULLUP);
 
   /* And our switches */
   pinMode(modePin, INPUT_PULLUP); /* Red */
@@ -246,9 +247,9 @@ void setup() {
   longbeep(); delay(400);
   setLEDs(RED, BLACK, BLACK, BLACK, BLACK);
   longbeep();
-  
-  /* Arbitrary delay before updating the 7-segments again */
-  delay(1000);
+
+  /* Demo the rings */
+  red_green_pulse();
   
   /* 7 Segment Display Continued */
   if(random(0, 1000) < 100) {
@@ -262,10 +263,6 @@ void setup() {
   }
   longbeep();
 
-  /* Switches */
-  pinMode(leftFSR, INPUT);
-  pinMode(rightFSR, INPUT);
-
   /* start button */
   //pinMode(startButtonPin, INPUT_PULLUP);
   Serial.println("Setup complete.");
@@ -303,8 +300,10 @@ void loop() {
     clearLEDs();
     if(!leftSwitchOpen()) {
       digitalWrite(ledPinsLeft[0], HIGH);
+      ring_setColor(leftRing, RED);
     } else if(!rightSwitchOpen()) {
       digitalWrite(ledPinsRight[0], HIGH);
+      ring_setColor(rightRing, RED);
     }
     delay(mainDelay);
     return; /* Can't continue in main loop */
@@ -677,6 +676,9 @@ int leftSwitchOpen_debounce(int silent) {
   /* Not a standard debounce method. We're going to keep the last 16 values
    * and majority rules.
    */
+  return leftSwitchOpen(silent);
+  
+  // We are ignorign all of this now 
   static int results[16] = {0,0,0,0,0,
                             0,0,0,0,0,
                             0,0,0,0,0,0};
@@ -705,28 +707,10 @@ int leftSwitchOpen(int silent) {
   boolean leftButtonState;
   static int lastvalue = 0;
 
-/* Sim Mode: 
   leftFSRButton.readButton(&leftButtonAction, &leftButtonState);
-  if(leftButtonState == false) { */
+  if(leftButtonState == false) {
     /* Button is low, meaning it's pressed */
-/*    return 0;
-  }
-  return 1;
-*/
-  int value = analogRead(leftFSR);
-  if( value > leftFSRThreshold) {
-    /* Pressed! */
-    if(lastvalue < leftFSRThreshold) {
-      Serial.print("Left pressed, pressure: ");
-      Serial.println(value);
-    }
-    lastvalue = value;
     return 0;
-  } 
-  if(lastvalue > leftFSRThreshold) {
-    Serial.print("Left released, pressure: ");
-    Serial.println(value);
-    lastvalue = value;
   }
   return 1;
 }
@@ -739,6 +723,9 @@ int rightSwitchOpen_debounce(int silent) {
   /* Not a standard debounce method. We're going to keep the last 16 values
    * and majority rules.
    */
+  return rightSwitchOpen(silent);
+
+  // we are ignoring all this
   static int results[16] = {0,0,0,0,0,
                             0,0,0,0,0,
                             0,0,0,0,0,0};
@@ -767,28 +754,10 @@ int rightSwitchOpen(int silent) {
   boolean rightButtonState;
   static int lastvalue = 0;
 
-/*
   rightFSRButton.readButton(&rightButtonAction, &rightButtonState);
-  if(rightButtonState == false) { */
+  if(rightButtonState == false) {
     /*Button is low, meaning it's pressed */
-/*    return 0;
-  }
-  return 1;
-*/
-  int value = analogRead(rightFSR);
-  if( value > rightFSRThreshold) {
-    /* Pressed! */
-    if(lastvalue < rightFSRThreshold) {
-      Serial.print("Right pressed, pressure: ");
-      Serial.println(value);
-    }
-    lastvalue = value;
     return 0;
-  } 
-  if(lastvalue > rightFSRThreshold) {
-    Serial.print("Right released, pressure: ");
-    Serial.println(value);
-    lastvalue = value;
   }
   return 1;
 }
@@ -856,10 +825,10 @@ void ledsToRed() {
 // Sets red light on.
 void ws2812_ledsToRed() {
   clearLEDs();
-  rightTree.setPixelColor(0, RED);
-  leftTree.setPixelColor(0, RED);
-  rightTree.show();
-  leftTree.show();
+  rightRing.setPixelColor(0, RED);
+  leftRing.setPixelColor(0, RED);
+  rightRing.show();
+  leftRing.show();
 }
 
 /* Clears all LEDs (for WS2812, except the top one) */
@@ -873,15 +842,8 @@ void clearLEDs() {
 
 void ws2812_clearLEDs()
 {
-  for (int i = 0; i < (LED_COUNT - 1); i++)
-  {
-    rightTree.setPixelColor(i, 0);
-    leftTree.setPixelColor(i, 0);
-  }
-  rightTree.setPixelColor(LED_COUNT - 1, topColor);
-  leftTree.setPixelColor(LED_COUNT - 1, topColor);
-  rightTree.show();
-  leftTree.show();
+  //ring_setColor(rightRing, WHITE);
+  //ring_setColor(leftRing, WHITE);
 }
 
 void setLEDs(unsigned long redlight,
@@ -889,7 +851,7 @@ void setLEDs(unsigned long redlight,
              unsigned long yellow3,
              unsigned long yellow2,
              unsigned long yellow1) {
-  ws2812_setLEDs(redlight, greenlight, yellow3, yellow2, yellow1);
+  //ws2812_setLEDs(redlight, greenlight, yellow3, yellow2, yellow1);
 
   /* If black, turn it off, otherwise turn it on */
   if(redlight == BLACK) {
@@ -937,25 +899,89 @@ void setLEDs(unsigned long redlight,
   }
 }
 
+void red_green_pulse() {
+  // pulses one coaster red and the other green.
+  leftRing.setBrightness(1);
+  rightRing.setBrightness(1);
+
+  if(random(0, 2)) {
+    leftring_setColor(RED);
+  } else {
+    leftring_setColor(GREEN);
+  }
+
+  if(random(0, 2)) {
+    rightring_setColor(RED);
+  } else {
+    rightring_setColor(GREEN);
+  }
+
+  for(int pulses=0; pulses < 3; pulses++) {
+    Serial.println("Pulsing up.");
+
+    for(int brightness=1; brightness <= 255; brightness++) {
+      rightRing.setBrightness(brightness);
+      leftRing.setBrightness(brightness);
+      rightRing.show();
+      leftRing.show();
+      delay(1);
+    }
+
+    Serial.println("Pulsing down.");
+
+    for(int brightness=255; brightness >= 1; brightness--) {
+      rightRing.setBrightness(brightness);
+      leftRing.setBrightness(brightness);
+      rightRing.show();
+      leftRing.show();
+      delay(1);
+    }
+  }
+  leftRing.setBrightness(255);
+  rightRing.setBrightness(255);
+  clearLEDs();
+}
+
+void rings_setColor(unsigned long color) {
+  for(uint8_t i=0; i< LED_COUNT; i++) {
+    rightRing.setPixelColor(i, color);
+    leftRing.setPixelColor(i, color);
+  }
+  rightRing.show();
+  leftRing.show();
+}
+void leftring_setColor(unsigned long color) {
+  for(uint8_t i=0; i< LED_COUNT; i++) {
+    leftRing.setPixelColor(i, color);
+  }
+  leftRing.show();
+}
+void rightring_setColor(unsigned long color) {
+  for(uint8_t i=0; i< LED_COUNT; i++) {
+    rightRing.setPixelColor(i, color);
+  }
+  rightRing.show();
+}
+
 void ws2812_setLEDs(unsigned long redlight,
              unsigned long greenlight,
              unsigned long yellow3,
              unsigned long yellow2,
              unsigned long yellow1) {
-  rightTree.setPixelColor(0, redlight);
-  rightTree.setPixelColor(1, greenlight);
-  rightTree.setPixelColor(2, yellow3);
-  rightTree.setPixelColor(3, yellow2);
-  rightTree.setPixelColor(4, yellow1);
-  rightTree.setPixelColor(LED_COUNT - 1, topColor);
-  leftTree.setPixelColor(0, redlight);
-  leftTree.setPixelColor(1, greenlight);
-  leftTree.setPixelColor(2, yellow3);
-  leftTree.setPixelColor(3, yellow2);
-  leftTree.setPixelColor(4, yellow1);
-  leftTree.setPixelColor(LED_COUNT - 1, topColor);
-  rightTree.show();
-  leftTree.show();
+  rightRing.setPixelColor(0, redlight);
+  rightRing.setPixelColor(1, greenlight);
+  rightRing.setPixelColor(2, yellow3);
+  rightRing.setPixelColor(3, yellow2);
+  rightRing.setPixelColor(4, yellow1);
+  rightRing.setPixelColor(LED_COUNT, topColor);
+  leftRing.setPixelColor(0, redlight);
+  leftRing.setPixelColor(1, greenlight);
+  leftRing.setPixelColor(2, yellow3);
+  leftRing.setPixelColor(3, yellow2);
+  leftRing.setPixelColor(4, yellow1);
+  leftRing.setPixelColor(LED_COUNT, topColor);
+  rightRing.show();
+  leftRing.show();
 }
 
 void prettyLEDCheck() {
@@ -1046,23 +1072,23 @@ void ws2812_cylon(unsigned long color, unsigned long wait)
   byte blue = (color & 0x0000FF);
 
   // Start at closest LED, and move to the outside
-  for (int i = 0; i <= LED_COUNT - 1; i++)
+  for (int i = 0; i < LED_COUNT; i++)
   {
     clearLEDs();
-    rightTree.setPixelColor(i, red, green, blue);  // Set the bright middle eye
-    leftTree.setPixelColor(i, red, green, blue);  // Set the bright middle eye
+    rightRing.setPixelColor(i, red, green, blue);  // Set the bright middle eye
+    leftRing.setPixelColor(i, red, green, blue);  // Set the bright middle eye
     // Now set two eyes to each side to get progressively dimmer
     for (int j = 1; j < 3; j++)
     {
       if (i - j >= 0)
-        leftTree.setPixelColor(i - j, red / (weight * j), green / (weight * j), blue / (weight * j));
-      rightTree.setPixelColor(i - j, red / (weight * j), green / (weight * j), blue / (weight * j));
-      if (i - j <= LED_COUNT)
-        leftTree.setPixelColor(i + j, red / (weight * j), green / (weight * j), blue / (weight * j));
-      rightTree.setPixelColor(i + j, red / (weight * j), green / (weight * j), blue / (weight * j));
+        leftRing.setPixelColor(i - j, red / (weight * j), green / (weight * j), blue / (weight * j));
+      rightRing.setPixelColor(i - j, red / (weight * j), green / (weight * j), blue / (weight * j));
+      if (i - j < LED_COUNT)
+        leftRing.setPixelColor(i + j, red / (weight * j), green / (weight * j), blue / (weight * j));
+      rightRing.setPixelColor(i + j, red / (weight * j), green / (weight * j), blue / (weight * j));
     }
-    leftTree.show();  // Turn the LEDs on
-    rightTree.show();  // Turn the LEDs on
+    leftRing.show();  // Turn the LEDs on
+    rightRing.show();  // Turn the LEDs on
     delay(wait);  // Delay for visibility
   }
 
@@ -1070,20 +1096,20 @@ void ws2812_cylon(unsigned long color, unsigned long wait)
   for (int i = LED_COUNT - 2; i >= 1; i--)
   {
     clearLEDs();
-    leftTree.setPixelColor(i, red, green, blue);
-    rightTree.setPixelColor(i, red, green, blue);
+    leftRing.setPixelColor(i, red, green, blue);
+    rightRing.setPixelColor(i, red, green, blue);
     for (int j = 1; j < 3; j++)
     {
       if (i - j >= 0)
-        leftTree.setPixelColor(i - j, red / (weight * j), green / (weight * j), blue / (weight * j));
-      rightTree.setPixelColor(i - j, red / (weight * j), green / (weight * j), blue / (weight * j));
-      if (i - j <= LED_COUNT)
-        leftTree.setPixelColor(i + j, red / (weight * j), green / (weight * j), blue / (weight * j));
-      rightTree.setPixelColor(i + j, red / (weight * j), green / (weight * j), blue / (weight * j));
+        leftRing.setPixelColor(i - j, red / (weight * j), green / (weight * j), blue / (weight * j));
+      rightRing.setPixelColor(i - j, red / (weight * j), green / (weight * j), blue / (weight * j));
+      if (i - j < LED_COUNT)
+        leftRing.setPixelColor(i + j, red / (weight * j), green / (weight * j), blue / (weight * j));
+      rightRing.setPixelColor(i + j, red / (weight * j), green / (weight * j), blue / (weight * j));
     }
 
-    leftTree.show();
-    rightTree.show();
+    leftRing.show();
+    rightRing.show();
     delay(wait);
   }
 }
@@ -1238,4 +1264,11 @@ void playsong2() {
     delay(tempo2 / 10);            // brief pause between notes
   }
 }
-
+
+/* Ring Functions */
+void  ring_setColor(Adafruit_NeoPixel& ring, unsigned long color) {
+  for(int i=0; i < LED_COUNT; i++) {
+    ring.setPixelColor(i, color);
+  }
+  ring.show();
+}