/*
--------------------------------------------
-------- Begin Initialise Variables --------
--------------------------------------------
*/
//STAGE #1
//define pins
int solenoid_pin = 2;
int hall_effect_sensor = A0;
//add code here
//Sensor readings
//add code here
int hall_effect_reading = 0;
float pendulum_length = 0.23;
int peak_threshold = 520;
/*--------------------------------------------
-------- End Initialise Variables ----------
--------------------------------------------
*/
/*
----------------------------------
-------- Begin Setup Loop --------
----------------------------------
*/
void setup()
{
// initialize serial communication at 115200 bits per second.
Serial.begin(9600);
//STAGE 2 - pin modes
//add code here
pinMode(solenoid_pin, OUTPUT);
pinMode(hall_effect_sensor, INPUT);
//Control solenoid
//add code here
digitalWrite(solenoid_pin, LOW);
digitalWrite(solenoid_pin, HIGH);
delay(1000);
digitalWrite(solenoid_pin, LOW);
}
/*
-------------------------------
------- End Setup Loop --------
-------------------------------
*/
/*
----------------------------------
-------- Begin Main Loop ---------
----------------------------------
*/
//STAGE 3 - the main loop
void loop()
{
// variables for timing
unsigned long start_time = 0;
unsigned long current_time = 0;
unsigned long elapsed_time = 0;
//add more code here...
int hall_effect_reading_old = 0;
int peak_count = 0;
float gravity = 0;
float period = 0;
float average_period = 0;
float average_period_ms = 0;
float average_period_s = 0;
//get start time
//add code here
start_time = millis();
do {
//read Hall effects sensor
//add code here
hall_effect_reading = analogRead(hall_effect_sensor);
//get current time
//add code here
current_time = millis();
//calc elapsed time in milliseconds
//add code here
elapsed_time = current_time - start_time;
if ((hall_effect_reading >= peak_threshold) && (hall_effect_reading_old <= peak_threshold));
{
peak_count = peak_count + 1;
}
hall_effect_reading_old = hall_effect_reading;
//Print data back to the computer
//add code here
Serial.print(elapsed_time);
Serial.print(',');
Serial.print(hall_effect_reading);
Serial.print(',');
Serial.print(peak_count);
Serial.print('\n');
} while (elapsed_time <= 10000);
//STAGE 5 - calculate Gravity...
average_period_ms = 2*elapsed_time/peak_count;
average_period_s = average_period_ms * 0.001;
//evaluate gravity
//g = your calculation here: use the sq() and PI
gravity = 4*sq(PI)*pendulum_length/average_period_s;
//STAGE 5b - print your answer...
Serial.print(gravity);
delay(10000); //wait for 10 seconds - LEAVE THIS FOR RELOAD TO WORK PROPERLY!
}
/*
----------------------------------
--------- End Main Loop ----------
----------------------------------
*/