Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Rough programming to piece the helmet together
- // By Chad Michaud
- // 26 Feb 2013
- #include <I2C.h> // Accelerometer Library
- #include <MMA8453_n0m1.h> // Accelerometer Library
- #include <Servo.h> // Servo Library
- 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)
- Servo helmservo; // Creates object for the servo
- int xVal1 = 0; // Variable that tracks X Value one
- int xVal2 = 0; // Variable that tracks X Value two
- int xDiff = 0; // Variable that tracks the difference of Xone and Xtwo
- boolean hopen = false; // Boolean to keep track of the helmets open/close state
- int helmpos = 0; // Variable that stores the helmets position
- int servopin = 9; // Stores what Pin the servo is connected to. Must be a PWM pin
- int eyeleds = 11; // Stores what Pin the LEDS are connected to. Must be a PWM pin
- int eyebrightness = 0; // Variable that stores the LED brightness
- int fadeamount = 5; // Controls how fast the LEDs will fade by
- void setup()
- {
- Serial.begin(9600); // For debuging
- accel.setI2CAddr(0x1D); // Sets the address for the Accelerometer
- accel.dataMode(false, 2); // Enable highRes 10bit (true) or lowRes 8bit (false), 2g range [2g,4g,8g]
- pinMode(eyeleds, OUTPUT); // Sets pin to output for the eyes
- helmservo.attach(servopin); // Sets pin for servo signal wire
- }
- void loop()
- {
- accel.update(); // Updates X,Y,Z Position of the Accelerometer
- xVal1 = accel.x(); // Sets Value 1
- delay(500); // Waits to check for a new position, this will have to be tweaked when mounting in the helm
- accel.update(); //updates X,Y,Z Position of the Accelerometer again
- xVal2 = accel.x(); // Sets Value 2
- xDiff = xVal1 - xVal2; // Finds the difference between X Value one and X Value two
- 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
- {
- 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.
- {
- helmpos += 10; // Increase the position variable, this may need to be changed aswell
- helmservo.write(helmpos); // Write the position to the servo
- delay(100); // Delay between running the command again.
- }
- // this is where the closing sound will have to be made, and turn on the LEDs
- // *******************First Blink on and off ************************
- eyebrightness = 255;
- analogWrite(eyeleds, eyebrightness);
- delay(100);
- eyebrightness = 0;
- analogWrite(eyeleds, eyebrightness);
- delay(100);
- // *******************************************************************
- // *******************Second Blink on and off*************************
- eyebrightness = 255;
- analogWrite(eyeleds, eyebrightness);
- delay(100);
- eyebrightness = 0;
- analogWrite(eyeleds, eyebrightness);
- delay(100);
- // *******************************************************************
- eyebrightness = 255; // Sets eyes to full brightness
- 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
- {
- analogWrite(eyeleds, eyebrightness); // Write the brightness to the LED
- delay(50); // Wait before you run again
- }
- hopen = false; // helmet is now closed
- }
- 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
- {
- eyebrightness = 0;
- analogWrite(eyeleds, eyebrightness);
- // I don't think there is a sound for this but if there is put that here
- 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.
- {
- helmpos -= 10; // Decrease the position variable, this may need to be changed aswell
- helmservo.write(helmpos); // Write the position to the servo
- delay(100); // Delay between running the command again.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment