I am trying to make a device that will register latency and number of shocks for passive avoidance behavior in drosophila. My device has two buttons green and red. I press the green button when I put a fruit fly into the test tube. When drosophila enters the shock compartment I press the red button to give electric shock through the Grass stimulator. My Arduino registers latencies or elapsed time between pressing the green button and subsequent presses of the red button as well as the number of shocks (presses of the red button). This part works just fine, however, I want to have a 60-sec beeper as the test runs only for 1 min. This part does not work. The beeper counts 60 seconds from the starting of the device, not from the press of the green button. I am not sure what I am doing wrong. Any advice is greatly appreciated!!
Here is a the code:
#include <LiquidCrystal.h>
/*
Super-basic stopwatch using millis();
*/
unsigned long start, finished, elapsed;
unsigned long startTime;
unsigned long endTime;
unsigned long duration;
byte timerRunning;
int switchPin = 3; // switch is connected to pin 3
int val; // variable for reading the pin status
int buttonState; // variable to hold the button state
int buttonPresses = 0; // how many times the button has been pressed
const int buz = 13;
unsigned long delayStart = 0; // the time the delay started
//bool timerRunning = false; // true if still waiting for delay to finish
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd (9, 8, 7, 6, 5, 4);
void setup()
{
lcd.begin(16, 2);
//lcd.setCursor (0,0);
//lcd.print("resetting");
lcd.setCursor (3,1);
lcd.print("resseting");
delay(3000);
lcd.clear();
lcd.setCursor (0,0);
lcd.print ("Latency= ");
lcd.setCursor (0,1);
lcd.print ("Shocks= ");
Serial.begin(9600);
pinMode(2, INPUT); // start button
pinMode (2, INPUT_PULLUP);
pinMode (buz,OUTPUT);
pinMode(switchPin, INPUT); // Set the switch pin as input
digitalWrite(buz, LOW); // turn buz off
delayStart = millis(); // start delay
//delayRunning = true; // not finished yet
buttonState = digitalRead(switchPin); // read the initial state
Serial.println("Press 'Green' for Start/Reset, 'Red' for Shocks");
}
void displayResult()
{
float h,m,s,ms;
unsigned long over;
elapsed=finished-start;
h=int(elapsed/3600000);
over=elapsed%3600000;
m=int(over/60000);
over=over%60000;
s=int(over/1000);
ms=over%1000;
Serial.print("Elapsed time: ");
Serial.println(elapsed);
Serial.print("Latency: ");
Serial.print(m,0);
Serial.print("m ");
Serial.print(s,0);
Serial.print("s ");
Serial.print(ms,0);
Serial.println("ms");
Serial.print("Shocks: ");
Serial.print(buttonPresses);
Serial.println(" time(s)");
lcd.setCursor (8,0);
lcd.print(m,0);
lcd.print("m");
lcd.print(s,0);
lcd.print("s");
lcd.print(ms,0);
lcd.setCursor (7,1);
lcd.print(buttonPresses);
}
void loop()
{float h,m,s,ms;
if (digitalRead(2) == HIGH);{ // button pressed & timer not running already
start=millis();
lcd.setCursor (7,1);
lcd.print(" ");
buttonPresses=0;
lcd.print(buttonPresses);
Serial.println("Started");
lcd.setCursor (8,0);
lcd.print(" ");
lcd.print("Start...");
lcd.print(buttonPresses);
lcd.setCursor (8,0);
lcd.noBlink();
delay(300);
// Turn on the blinking cursor:
lcd.blink();
delay(200); // for debounce
}
if (digitalRead(3)==HIGH)
{ lcd.noBlink();
lcd.setCursor (9,0);
lcd.print(" ");
finished=millis();
delay(200); // for debounce
buttonPresses++;
displayResult();
delay(200);}
if (timerRunning && ((millis() - delayStart) >= 60000)) {
//timerRunning = false; // // prevent this code being run more then once
digitalWrite(buz, HIGH); // turn buz on
// tone(buz, 1000); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buz); // Stop sound...
delay(1000); // ...for 1sec
Serial.println("Turned buz on");
lcd.setCursor (10,1);
lcd.print("1min");
}}