Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Define two integer variables for accelerometer and sensor values
- //Note: We are measuring only the z-axis acceleration
- int z_axisPin = A2;
- int z_sensorValue;
- int z_highValue = 0;
- int z_lowValue = 1023;
- int blueLED = 13;
- int redLED = 2;
- //Introduce a new variable type called FLOAT -- a variable that store decimal numbers
- //Define the voltage source to the board. This can be the output of the battery or USB connection
- //Note: Incorrectly defining the voltage will generate incorrect voltage scale
- float supply_voltage = 3.38;
- //Variables to store conversions, type FLOAT
- float z_voltage;
- //Setup run once
- void setup() {
- Serial.begin(9600);
- pinMode(z_axisPin,INPUT);
- //wait 1 sec to stabilize the sensor
- delay(100);
- }
- void loop() {
- //Read the sensor and store the integer value 0-1023
- z_sensorValue = analogRead(z_axisPin);
- //keep track of which is the highest value
- if (z_sensorValue > z_highValue) {
- z_highValue = z_sensorValue;
- }
- //keep track of which is the lowest value
- if (z_sensorValue < z_lowValue) {
- z_lowValue = z_sensorValue;
- }
- float swing_g = map (z_sensorValue, 505 , 1023, 1 ,16);
- if (z_sensorValue > 750) {
- digitalWrite(blueLED, HIGH);
- delay(5000);
- digitalWrite(blueLED,LOW);
- }
- if (z_sensorValue < 750 && z_sensorValue > 600) {
- digitalWrite(redLED, HIGH);
- delay(5000);
- digitalWrite(redLED, LOW);
- }
- //Print out the sensor number and print a tab space - \t
- Serial.print("Z-axis reading: ");
- Serial.print(z_sensorValue);
- Serial.print("\t");
- Serial.print("Z-axis high: ");
- Serial.print(z_highValue);
- Serial.print("\t");
- Serial.print("Z-axis low: ");
- Serial.print(z_lowValue);
- Serial.print("\t");
- //Convert to voltage and print result
- z_voltage = z_sensorValue * (supply_voltage/1024);
- Serial.print("\tZ-axis voltage: ");
- Serial.println(z_voltage);
- //Numbers to G force
- Serial.print("gforce: ");
- Serial.println(swing_g);
- //short delay before next reading. Sensor needs ~20ms between responses
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement