Declare a global (or static) boolean variable to keep track of whether the timer is running or not, as well as a variable to hold the start millis()
value. When the lux drops below your threshold, subtract the current millis()
from the start value, and you have the duration in milliseconds, which is trivial to convert to mm:ss.sss format.
Example code (not a complete sketch)
uint32_t timer_start = 0;
uint8_t timer_running = false;
void loop() {
if (lux>150) {
if (!timer_running) {
// Timer is not already running, so capture the start time
timer_start = millis();
timer_running = true;
}
} else {
if (timer_running) {
// The timer is currently running; stop it calculate/print the run time
timer_running = false;
Serial.print(millis()-timer_start); // format however you like here
}
}
}
This now tells me how many millis the gymnast is in the air, divided
by 1000.0 with 3 decimals. If I start the Serial Monitor when the
gymnast is in the trampoline bed, then I can get the seconds of
air_time the gymnast has. Is there a way to have this script running
ONLY for 10 loops?
Certainly. If it is assumed that you want a cumulative total air-time, then you need to declare another global or static variable to hold the cumulative air-time, and you need to have another variable to keep track of how many cycles have passed.
void setup() {
Serial.begin(9600);
// initialize digital pin 13 as an output.
}
uint32_t timer_start = 0;
int timer_running = false;
uint32_t total_time = 0;
uint8_t jump_count = 0;
void loop()
{
// Get LDR information
int sensorValue = analogRead(A0); // read the input on analog pin 0
float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V)
float lux = voltage * 100;
if (jump_count<10) {
if (lux>150) {
if (!timer_running) {
// Timer is not already running, so capture the start time
timer_start = millis();
timer_running = true;
}
} else {
if (timer_running) {
// The timer is currently running; stop it calculate/print the run time
timer_running = false;
// Add this-jump air time to the cumulative total:
total_time += millis()-timer_start;
// Increment jump_count
jump_count++;
Serial.println((millis()-timer_start) / 1000.0, 3); // format however you like here
}
}
} else {
// Print the total air-time for 10 jumps;
Serial.println((total_time)) / 1000.0, 3);
}
}
static unsigned long last_off = 0;
state variable inloop{}
, resetting at the end of the else clause:last_off = millis();
and also report at the beginning of the else clause if someon_time=millis()-last_off
is greater than some threshhold. Are you having problems with the formatting? Or with calculating the on time? – Dave X Oct 8 '18 at 15:20if (lux>150) {...}
block has the timer start and stop code, where instead, theif (lux>150) {...}
block should have the timer starting code, and theelse {...}
block (meaning, wherelux<=150
) should have the timer stopping code. – jose can u c Oct 10 '18 at 1:20