I need to build a timer that uses the breaking of copper strips to trigger its starting and stopping. The intention is to measure the speed of a rocket with this - it will break the first wire, starting the timer, and then break the second, stopping it.
I am using an Arduino Uno, and a couple of lengths of wire for now.
I can plug one wire into the 1st 5V power supply on the board, and the other end into one of the analog pins (AO), (and then the second wire into the 2nd 5v supply and A1) so that when it is pulled out of the pin socket, it is 'broken'. Is this the correct method to go about this, and if so, how do I tell the Arduino to look for those signals as its 'start' and 'stop' and then record the time difference accordingly?
So far, I have managed to read a signal from the wire using the Analog Read Signal tutorial on Arduino.cc (https://www.arduino.cc/en/Tutorial/AnalogReadSerial). I noted whilst watching the printed data that once the wire is removed from the pin, the voltage value slowly decreased, and held at around 290 (starting at 1023, as per the tutorial). Is this simply a side effect of not using a grounded potentiometer, but a wire?
void setup() {
// set up comms:
Serial.begin(9600);
}
void loop() {
// check and read value of input:
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1);
}
I am new to Arduino, so please explain in detail, or point me to a thread that will! Thanks in advance for help!
UPDATE Having played around with analogWrite some more, I have managed to get a situation where I can output the relative value from both A0 and A1. However, I am still struggling to make it operate as a timer.
// declare variables
unsigned long startTime, stopTime;
unsigned long totalTime = (stopTime - startTime);
void setup() {
Serial.begin(9600); // set up comms
}
void loop() {
// check and read value of input:
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
if (sensorValue1 < 1023) {
startTime = micros();
Serial.println(sensorValue1);
}
if (sensorValue2 < 1023) {
stopTime = micros();
Serial.println(sensorValue2);
}
Serial.println("Time: "); // prints "Time: "
Serial.println(totalTime); // prints time value
delay(1000);
}
I have also tried to convert this to use digitalRead rather than analogRead but have not had much success - I am still failing to get a time. I know my method is wrong, but have struggled to find guidance on how to correct it.
// declare variables
boolean timingFinished = false;
unsigned long startTime, stopTime;
String strTime = "Time:";
void setup() {
Serial.begin(9600); // set up comms
pinMode(7, INPUT); // set up pin 7
pinMode(6, INPUT); // set up pin 6
}
void loop() {
// check and read value of input:
int sensorValue1 = digitalRead(7);
int sensorValue2 = digitalRead(6);
Serial.println(sensorValue1);
Serial.println(sensorValue2);
if (sensorValue1 = HIGH) {
startTime = micros();
Serial.println(sensorValue1);
}
if (sensorValue2 = HIGH) {
stopTime = micros();
Serial.println(sensorValue2);
}
unsigned long totalTime = (sensorValue1 - sensorValue2);
String strPrint = strTime + totalTime;
Serial.println(strPrint); // prints "Time"
}