Recording latencies and shocks

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”);
}}

if (digitalRead(2) == HIGH);

Likely “oops”

How to modify it?

Lose the semicolon

Still does not work.

“Does not work” is about the most pointless, useless thing you could post here.

It does something different now, compared to before.
Describe what it does that you don’t want it to, and what it doesn’t do that you do want it to.

Please remember to use code tags when posting code.

The way the code is written to nearly ‘continuously’ buzzing after 1 minute because the “loop” is repeatedly called by the bootloader.

If you just want the sketch to run only 1 minute then stop; then add: “while(1);” right after the above code. - To prevent the reentry of the loop.
If you just want the sketch to buz every minute then stop then reset the delayStart value.
Hope this helps.

Happy Mosquito Free!
Phi

This is utter nonsense.
“loop” is repeatedly called by “main”.

I may use ‘wrong’ term: Bootloader & main are 2 different animals. main is a procedure & the other is the module.
My purpose is simply offering a help the poster. Perhaps, he may not aware of the repetitions of the ‘loop’

Happy Mosquito Free!
Phi

Sorry. I am very new to this. This is my first code. When I removed semicolon, nothing changed. It measures latencies (periods between pressing the green button and subsequent presses of the red button. It also counts the number of presses. The beeper counts 60 min since restarting of the device. I want it to beep 60 min after the first press of the green button.

Oh yes, it did.

I don’t really know how you got so much code before deciding it “didn’t work”, but I suggest you add some debug code, and get your sketch to tell you what it is doing.

I actually tweaked the code so now it looks like this:

#include <LiquidCrystal.h>
#include <Wire.h>

/*
Super-basic stopwatch using millis();
*/

unsigned long start, finished, elapsed;
int switchPin = 3;
int btn = 2;
// 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
int buz = 13;
byte delayRunning;
unsigned long delayStart = 0; // the time the delay started
//unsigned long timeout = 5 * 60 *1000 * 60; // 5 minutes
//bool delayRunning = 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(2000);
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 (btn, INPUT_PULLUP);
pinMode (buz,OUTPUT);
pinMode (switchPin, INPUT); // Set the switch pin as input
digitalWrite(buz, LOW); // turn buz off
//delayStart = false; // start delay
delayRunning = false; // 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 (delayRunning == 0 && digitalRead(btn)==HIGH)
{
start=millis();
delayRunning = 1;
digitalWrite(buz, LOW);
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 (delayRunning && ((millis() - delayStart) >= 10000)) {
//delayRunning = false; // // prevent this code being run more then once
Serial.println(“1 minute”);
lcd.setCursor (10,1);
lcd.print(“1min”);
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

}
if (digitalRead(3)==HIGH)
{ lcd.noBlink();
lcd.setCursor (9,0);
lcd.print(" ");
finished=millis();
delay(200); // for debounce
buttonPresses++;
displayResult();
delay(200);}}

The serial monitor is here:
18:22:56.947 → Press ‘Green’ for Start/Reset, ‘Red’ for Shocks
18:22:58.741 → Started
18:23:01.461 → Elapsed time: 2507
18:23:01.461 → Latency: 0m 2s 507ms
18:23:01.494 → Shocks: 1 time(s)
18:23:02.966 → Elapsed time: 4016
18:23:02.999 → Latency: 0m 4s 16ms
18:23:02.999 → Shocks: 2 time(s)
18:23:04.427 → Elapsed time: 5478
18:23:04.461 → Latency: 0m 5s 478ms
18:23:04.461 → Shocks: 3 time(s)
18:23:04.947 → 1 minute
18:23:06.940 → 1 minute
18:23:08.958 → 1 minute
18:23:10.943 → 1 minute

I put 10seconds delay, so it will be easier to test rather than wait for 1 min.

I am very new to this what is the command to stop or reset?