Guest User

Untitled

a guest
Apr 10th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Skateboard Lights v0.1
  3. Matthew Wong
  4. 4/9/22
  5. SLO Hacks 2022
  6.  */
  7.  
  8. // -- Accelerometer --
  9.    
  10.   #include <MPU6050_tockn.h>
  11.   #include <Wire.h>
  12.  
  13.   MPU6050 mpu6050(Wire);
  14.  
  15.  
  16. # include <FastLED.h>
  17. # define NUM_LEDS 26
  18. # define LED_DATA_PIN 7
  19. # define BRIGHTNESS 100
  20.  
  21. CRGB leds[NUM_LEDS];
  22. /*
  23. leds[0:9] = left
  24. leds[10:19] = right
  25. leds[20:25] = back
  26.  */
  27.  
  28. const int TURN_DELAY = 125; //period of turn (each inc) in ms)
  29. const int BRAKE_DELAY = 50; //period of brake light in ms
  30.  
  31. const int DEBUG_LEFT = 5;
  32. const int DEBUG_RIGHT = 6; // short to GND to activate
  33.  
  34. int   turn_state_left = 0; //int 0-5 defining state of left turn
  35. int   turn_state_right = 0;
  36. int   brake_state = 0;
  37. bool  turn_active_left = false;
  38. bool  turn_active_right = false;
  39. bool  brake_active = false;
  40. int   time_turn = 0;
  41. int   time_brake = 0;
  42.  
  43. void setup() {
  44.   delay(2000); //Delay 2s
  45.   Serial.begin(9600);
  46.   FastLED.addLeds<NEOPIXEL, LED_DATA_PIN>(leds, NUM_LEDS);
  47.   FastLED.setBrightness(BRIGHTNESS);
  48.   pinMode(DEBUG_LEFT, INPUT_PULLUP);
  49.   pinMode(DEBUG_RIGHT, INPUT_PULLUP);
  50.  
  51.   //--I2C Init--
  52.   Wire.begin();
  53.   Serial.println("I2C Initialized...");
  54.   mpu6050.begin();
  55.   Serial.println("Calculating Gyro Offsets...");
  56.   mpu6050.calcGyroOffsets(true); //Zeros All Sensors, Will zero to orientation
  57.   Serial.println("Complete!");
  58.  
  59. }
  60.  
  61. void loop() {
  62.  
  63. /////////////////////////////////////////////////////////////////////////////////////////////
  64. // Poll Accell
  65.   mpu6050.update();
  66.   turn_active_right = readRight();
  67.   brake_active = readBrake();
  68.   turn_active_left = readLeft();
  69.  
  70.   //Serial.print(mpu6050.getAngleY());
  71.   //Serial.print(mpu6050.getAccY());
  72.  
  73.   //turn_active_left = !digitalRead(DEBUG_LEFT);
  74.   //turn_active_right = !digitalRead(DEBUG_RIGHT);
  75.   //Serial.print(turn_active_left);
  76.   //Serial.print("  ");
  77.   //Serial.print(turn_active_right);
  78.   //Serial.print("  ");
  79.   //Serial.println(brake_active);
  80.  
  81.   // Static Color when not turning
  82.   if (turn_active_left == false) {
  83.     for (int i = 0; i < 10; i++){
  84.       leds[i].setRGB(255, 0, 0);
  85.     }
  86.     turn_state_left = 0;
  87.     FastLED.show();
  88.   }
  89.   if (turn_active_right == false) {
  90.     for (int i = 0; i < 10; i++){
  91.       leds[i + 10].setRGB(255, 0, 0);
  92.     }
  93.     turn_state_right = 0;
  94.     FastLED.show();
  95.   }
  96.   if ((brake_active == false) && (brake_state == 0)) {
  97.     for (int i = 0; i < 6; i++){
  98.       leds[i + 20].setRGB(255, 0, 0);
  99.     }
  100.     FastLED.show();
  101.   }
  102. /////////////////////////////////////////////////////////////////////////////////////////////
  103.  
  104. /////////////////////////////////////////////////////////////////////////////////////////////
  105. // LED Updates
  106.  
  107. //Turn Activation
  108.   if (millis() - time_turn >= TURN_DELAY) {
  109.     time_turn = millis();
  110.    /* Serial.println("LED Values: ");
  111.       for (int i = 0; i < 26; i++){
  112.         Serial.print(leds[i].r);
  113.         Serial.print(" || ");
  114.         Serial.print(leds[i].g);
  115.         Serial.print(" || ");
  116.         Serial.print(leds[i].b);
  117.         Serial.println("");
  118.       } */
  119.     if (turn_active_left) {
  120.       //Serial.print("Turn State Left ");
  121.       //Serial.println(turn_state_left);
  122.       updateTurnState(0, turn_state_left);
  123.      
  124.       if (turn_state_left < 5) {
  125.         turn_state_left = turn_state_left + 1;
  126.       }
  127.       else if (turn_state_left >= 5) {
  128.         turn_state_left = 0;
  129.       }
  130.     }
  131.  
  132.     if (turn_active_right) {
  133.       //Serial.print("Turn State Right ");
  134.       //Serial.println(turn_state_right);
  135.       updateTurnState(1, turn_state_right);
  136.      
  137.       if (turn_state_right < 5) {
  138.         turn_state_right = turn_state_right + 1;
  139.       }
  140.       else if (turn_state_right >= 5) {
  141.         turn_state_right = 0;
  142.       }
  143.     }
  144.   }
  145. // Brake Activation
  146.   if ((millis() - time_brake >= BRAKE_DELAY) && brake_active) {
  147.     time_brake = millis();
  148.     if (brake_state > 0) {
  149.       brakeLightActive(brake_state);
  150.       brake_state = brake_state - 1;
  151.     }
  152.   }
  153. }
  154. /////////////////////////////////////////////////////////////////////////////////////////////
  155.  
  156. void updateTurnState(int side, int turn_state_local) {
  157.  
  158.   // sides
  159.   // left = 0
  160.   // right = 1
  161.  
  162.   int side_comp;
  163.   if (side == 0) {
  164.     side_comp = 0;
  165.   }
  166.   else {
  167.     side_comp = 10;
  168.   }
  169.   for (int i = 0; i < 2*turn_state_local; i = i + 2) {
  170.     leds[side_comp + i].setRGB( 255, 0, 0);
  171.     leds[side_comp + i + 1].setRGB( 255, 0, 0);
  172.   }
  173.   for (int i = 10; i > 2*turn_state_local; i = i - 2) {
  174.     leds[side_comp + i].setRGB( 0, 0, 0);
  175.     leds[side_comp + i - 1].setRGB( 0, 0, 0);
  176.   }
  177.  
  178.   FastLED.show();
  179.  
  180. }
  181.  
  182. void brakeLightActive(int brake_state){
  183.   if (brake_state % 2 == 0) {
  184.     for (int i = 20; i < 26; i++){
  185.       leds[i].setRGB(255, 0, 0);
  186.     }
  187.   }
  188.   else {
  189.     for (int i = 20; i < 26; i++){
  190.       leds[i].setRGB(0, 0, 0);
  191.     }
  192.   }
  193.  
  194.   FastLED.show();
  195.  
  196. }
  197.  
  198. bool readBrake() {
  199.   // brake -> +y
  200.   bool brake = false;
  201.   if (mpu6050.getAccY() > 0.25) {
  202.     brake = true;
  203.   }
  204.   Serial.print("Brake Flag: ");
  205.   Serial.print(brake);
  206.   return brake;
  207.  
  208. }
  209.  
  210. bool readRight() {
  211.   // Turning right -> 179 and 160
  212.   bool right = false;
  213.   float angle = mpu6050.getAccAngleY();
  214.   if ((90 < angle) && (angle < 165)) {
  215.     right = true;
  216.   }
  217.   Serial.print(" Right Flag: ");
  218.   Serial.print(right);
  219.   return right;
  220. }
  221.  
  222. bool readLeft() {
  223.   // angleY between -179 and -160
  224.   bool left = false;
  225.   float angle = mpu6050.getAccAngleY();
  226.   if ((-165 < angle) && (angle < -90)) {
  227.     left = true;
  228.    
  229.   }
  230.   Serial.print(" Left Flag: ");
  231.   Serial.print(left);
  232.   Serial.print(" Y Accel Angle ");
  233.   Serial.println(mpu6050.getAccAngleY());
  234.   return left;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment