michaudtime

Rough Helmet Program

Feb 26th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. // Rough programming to piece the helmet together
  2. // By Chad Michaud
  3. // 26 Feb 2013
  4.  
  5. #include <I2C.h> // Accelerometer Library
  6. #include <MMA8453_n0m1.h> // Accelerometer Library
  7. #include <Servo.h> // Servo Library
  8.  
  9. MMA8453_n0m1 accel; // Creates object for the accelerometer Accelerometer has to be wired SDA to Analog 4 and SLC to Analog 5 (not sure how to change this yet)
  10. Servo helmservo; // Creates object for the servo
  11.  
  12. int xVal1 = 0; // Variable that tracks X Value one
  13. int xVal2 = 0; // Variable that tracks X Value two
  14. int xDiff = 0; // Variable that tracks the difference of Xone and Xtwo
  15. boolean hopen = false; // Boolean to keep track of the helmets open/close state
  16. int helmpos = 0; // Variable that stores the helmets position
  17. int servopin = 9; // Stores what Pin the servo is connected to. Must be a PWM pin
  18. int eyeleds = 11; // Stores what Pin the LEDS are connected to. Must be a PWM pin
  19. int eyebrightness = 0; // Variable that stores the LED brightness
  20. int fadeamount = 5; // Controls how fast the LEDs will fade by
  21.  
  22.  
  23. void setup()
  24. {
  25. Serial.begin(9600); // For debuging
  26. accel.setI2CAddr(0x1D); // Sets the address for the Accelerometer
  27. accel.dataMode(false, 2); // Enable highRes 10bit (true) or lowRes 8bit (false), 2g range [2g,4g,8g]
  28.  
  29. pinMode(eyeleds, OUTPUT); // Sets pin to output for the eyes
  30. helmservo.attach(servopin); // Sets pin for servo signal wire
  31. }
  32.  
  33. void loop()
  34. {
  35. accel.update(); // Updates X,Y,Z Position of the Accelerometer
  36. xVal1 = accel.x(); // Sets Value 1
  37.  
  38. delay(500); // Waits to check for a new position, this will have to be tweaked when mounting in the helm
  39.  
  40. accel.update(); //updates X,Y,Z Position of the Accelerometer again
  41. xVal2 = accel.x(); // Sets Value 2
  42.  
  43. xDiff = xVal1 - xVal2; // Finds the difference between X Value one and X Value two
  44.  
  45. if(xDiff >= 200 && hopen == true) // If there is a difference greater or equal to specified (will have to be tweaked when in helm) and the helmet is open
  46. {
  47. while(helmpos <= 180) // while the helmet is not in it's down position. This will probably have to be tweaked when servo is in the helm.
  48. {
  49. helmpos += 10; // Increase the position variable, this may need to be changed aswell
  50. helmservo.write(helmpos); // Write the position to the servo
  51. delay(100); // Delay between running the command again.
  52. }
  53.  
  54. // this is where the closing sound will have to be made, and turn on the LEDs
  55.  
  56. // *******************First Blink on and off ************************
  57. eyebrightness = 255;
  58. analogWrite(eyeleds, eyebrightness);
  59. delay(100);
  60. eyebrightness = 0;
  61. analogWrite(eyeleds, eyebrightness);
  62. delay(100);
  63. // *******************************************************************
  64.  
  65. // *******************Second Blink on and off*************************
  66. eyebrightness = 255;
  67. analogWrite(eyeleds, eyebrightness);
  68. delay(100);
  69. eyebrightness = 0;
  70. analogWrite(eyeleds, eyebrightness);
  71. delay(100);
  72. // *******************************************************************
  73.  
  74. eyebrightness = 255; // Sets eyes to full brightness
  75.  
  76. for(eyebrightness = 255; eyebrightness > 50; eyebrightness = eyebrightness - fadeamount) // Now that the brightness is full, fade the brightness to 50(that may need to be changed) based on the fade amount
  77. {
  78. analogWrite(eyeleds, eyebrightness); // Write the brightness to the LED
  79. delay(50); // Wait before you run again
  80. }
  81.  
  82. hopen = false; // helmet is now closed
  83. }
  84.  
  85. else if(xDiff >= 200 && hopen == false) // else if the differenece is greater or equal to specified (will have to be tweaked when in helm) and the helmet is closed
  86. {
  87. eyebrightness = 0;
  88. analogWrite(eyeleds, eyebrightness);
  89.  
  90. // I don't think there is a sound for this but if there is put that here
  91.  
  92. while(helmpos >= 1) // while the helmet is not in it's up position. This will probably have to be tweaked when servo is in the helm.
  93. {
  94. helmpos -= 10; // Decrease the position variable, this may need to be changed aswell
  95. helmservo.write(helmpos); // Write the position to the servo
  96. delay(100); // Delay between running the command again.
  97. }
  98.  
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment