/*
--------------------------------------------
-------- Begin Initialise Variables --------
--------------------------------------------
*/
//STAGE #1
//define pins
//add code here
const int transistor_pin = 2;
const int hall_pin = A0;
//define pendulum length
float length = 0.23;
const int ping_time = 200;
const int threshold_value = 530;
//Sensor readings
int hall_voltage = 0;
/*--------------------------------------------
-------- End Initialise Variables ----------
--------------------------------------------
*/
/*
----------------------------------
-------- Begin Setup Loop --------
----------------------------------
*/
void setup()
{
// initialize serial communication at 115200 bits per second.
Serial.begin(9600);
//STAGE 2 - pin modes
pinMode(transistor_pin,OUTPUT);
pinMode(hall_pin, INPUT);
//Control solenoid
digitalWrite(transistor_pin,LOW);
digitalWrite(transistor_pin, HIGH);
delay(ping_time); //wait for 200ms
digitalWrite(transistor_pin,LOW);
}
/*
-------------------------------
------- End Setup Loop --------
-------------------------------
*/
/*
----------------------------------
-------- Begin Main Loop ---------
----------------------------------
*/
//STAGE 3 - the main loop
void loop()
{
// variables for timing
unsigned long starttime = 0;
unsigned long currenttime = 0;
unsigned long elapsedtime = 0;
int hall_voltage_old = 0;
int peak_count = 0;
float gravity;
float T_period;
//get start time
starttime = millis();
do {
//read Hall effects sensor
hall_voltage = analogRead(hall_pin);
//get current time
currenttime = millis();
//calc elapsed time in milliseconds
elapsedtime = currenttime - starttime;
if(hall_voltage >= threshold_value && hall_voltage_old < threshold_value) {
peak_count = peak_count + 1; // register that peak has occured
}
hall_voltage_old = hall_voltage; // setting current reading to the old one
//Print data back to the computer
Serial.print (elapsedtime);
Serial.print (",");
Serial.println (hall_voltage);
Serial.print (",");
Serial.println(peak_count);
delay(1);
} while (peak_count < 10);
//STAGE 5 - calculate Gravity...
T_period = (elapsedtime*0.001) / 5;
//evaluate gravity
gravity = length / (sq(T_period / (2*(PI))));
//STAGE 5b - print your answer...
Serial.print("gravity = ");
Serial.print(gravity);
Serial.print('\n');
//Serial.println("T_period = ");
//Serial.print(T_period);
delay(10000); //wait for 10 seconds - LEAVE THIS FOR RELOAD TO WORK PROPERLY!
}
/*
----------------------------------
--------- End Main Loop ----------
----------------------------------
*/