1

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 green 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 Grass stimulator. My Arduino registers latencies or elapsed time from pressing green button and subsequent presses red button and 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 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.

Here is 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");
  }}

2
  • Check the blink without delay example. Set a variable when you start the test, and check against the current time (minus 60 seconds) to start the beeper. May 7 at 18:09
  • your code is really badly formatted, which can easily lead to errors ... for example, you should not have two braces }} next to each other
    – jsotola
    May 7 at 19:59
0

The best place to start, is with the BlinkWithoutDelay example in the IDE.

Once you master that concept, you may be tempted to start several different timers. With enough timers, your code will start to look like a bowl of spaghetti.

Next, you could build a generic function which would help to reduce the "spaghetti code".

I would favour an OOP approach.

// Sketch uses 910 bytes (2%) of program storage space.
// Global variables use 18 bytes (0%) of dynamic memory.
// Arduino Uno, IDE 1.8.9
class MillisTimer{

  private:

    unsigned long m_timeInMilliSeconds;
    unsigned long m_previousMillis;
    byte m_timerActive;

  public:

    MillisTimer(unsigned long timeInMilliSeconds):
      m_timeInMilliSeconds(timeInMilliSeconds){
      m_timerActive = 0;
      m_previousMillis = 0;
    }

    bool Update(){
      if(m_timerActive && (millis() - m_previousMillis >= m_timeInMilliSeconds)){
        m_previousMillis += m_timeInMilliSeconds;
        return 1;
      }
      else{
        return 0;
      }
    }

    void Start(){
      m_timerActive = 1;
    }

    void Stop(){
      m_timerActive = 0;
    }

    void ReStart(){
      m_timerActive = 1;
      m_previousMillis = millis();
    }

};

// Make a copy of the object to work with.
MillisTimer SixtySecondTimer(60000);

void setup(){

  // Call this function from a button click
  // to start the 60 second timer.
  SixtySecondTimer.Start();

  // Timer complete indicator.
  pinMode(LED_BUILTIN, OUTPUT);

}

void loop(){

  // Call the update function as fast as possible.
  if(SixtySecondTimer.Update()){
    digitalWrite(LED_BUILTIN, HIGH);
    SixtySecondTimer.Stop();
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.