• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
//************************global variables*******************************//const int xPin = 1; // X output of the accelerometerconst int yPin = 2; // Y output of the accelerometerint buzzPin = 5;int analogInput = 3; // pin to read voltagefloat vout = 0.0;int vin = 0;float R1 = 4670.0;float R2 = 4660.0;int buttonPin = 4; // pin for buttonint ledPinR = 8; // pin for red LEDint ledPinY = 6; // pin for yellow LEDint ledPinG = 7; // pin for green LEDint buttonState = 0;int value = 0;//***************************void setup***********************************void setup(){Serial.begin(9600);//pinMode(xPin, INPUT);// declaration of pin modespinMode(yPin, INPUT);pinMode(buzzPin, OUTPUT);pinMode(ledPinR, OUTPUT);pinMode(ledPinY, OUTPUT);pinMode(ledPinG, OUTPUT);pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:pinMode(analogInput, INPUT);}// *************************voltage read function*************************void voltageDisplay(){boolean buttonBool;buttonBool = digitalRead(buttonPin); while(buttonBool){value = analogRead(analogInput); // print result over the serial portvout = (value * 2.5) / 1024.0; // should be 5 on battvin = vout / (R2/(R1+R2));Serial.print(vin);Serial.println(" volt");Serial.print("\n");switch (vin) {case 2: // will be 9 while on battdigitalWrite(ledPinG, HIGH);case 1: // will be 8 while on battdigitalWrite(ledPinY, HIGH);case 0: // will be 7 while on battdigitalWrite(ledPinR, HIGH);break;default:digitalWrite(ledPinR, HIGH);// if nothing else matches, do the default
 
// default is optional}delay(100);buttonBool = digitalRead(buttonPin);}digitalWrite(ledPinG, LOW);digitalWrite(ledPinY, LOW);digitalWrite(ledPinR, LOW);while(digitalRead(buttonPin)); // wait until button is released//*******************************main loop********************************void loop(){buttonState = digitalRead(buttonPin);if(buttonState == 1){voltageDisplay();delay(100);}int pulseX, pulseY; // variables to read the pulse widthsint accelerationX, accelerationY; // variables to contain the// resulting accelerationsint sum = 0;int avg = 0; //pulseX = pulseIn(xPin,HIGH); // x axis not used in presentationpulseY = pulseIn(yPin,HIGH);// convert the pulse width into acceleration// acceleration is in milli-g's// earth's gravity is 1000 milli-g's, or 1g// accelerationX = ((pulseX / 10) - 500) * 8 // x axis not used(temp)for(int i = 0; i < 10; i++) // 10 samples to reduce signal noise{accelerationY = ((pulseY / 10) - 500) * 8;sum += accelerationY;delay(100);}avg = sum / 10;if(avg <= -90 || avg >= -10) //test avg to determine notification{Serial.print("TILT\n"); digitalWrite(ledPinG, HIGH);digitalWrite(ledPinY, HIGH);digitalWrite(ledPinR, HIGH);for (long j = 0; j < 2048 * 3; j++ ) //sound buzzer{// 1 / 2048Hz = 488uS, 244uS Hi and 244uS Lo to create 50% dutycycledigitalWrite(buzzPin, HIGH);delayMicroseconds(244);digitalWrite(buzzPin, LOW);delayMicroseconds(244);}delay(500); //after buzzer wait .5 seconds
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...