Start a loop only when a signal is given

Hi, so I feel as if my problem is a basic one, but I'm really running out of time as the PI of my lab needs the device running ASAP and I have to go back to school in less than a week.

Originally we needed a tracking system, to see how fast a mouse (the mammal) was moving. We use compressed oxygen to levitate a mouse and head fix the mouse in position (Basically, the mouse is on a 2 dimensional treadmill, X and Y.). With the arduino click mouse program, arduino is now sending out information in the form of:

X, Y, Time(s)

Which I then use RS232 Data Logger to take in all the info. So far, it works near flawlessly, except for the initial 2.75 second of recording where both the X and Y coordinates are blank, but I have a feeling that is the mouse, not the board.

The code is:

#include <ps2.h>
/*

  • Pin 2 is the mouse data pin, pin 3 is the clock pin
  • Feel free to use whatever pins are convenient.
    */
    PS2Mouse mouse(3, 2);

void setup()
{
Serial.begin(9600);
mouse.init();
}

void loop()
{
MouseInfo mouseInfo;
mouse.getData(&mouseInfo);

//Cumulative X position
Serial.print(mouseInfo.cX, DEC);
Serial.print(",");
//Cumulatie Y position
Serial.print(mouseInfo.cY, DEC);
Serial.print(",");
Serial.print(millis()*0.001);

Serial.println();
}

So my actual question is, how can I have it so a digital input signal can start and pause the data acquisition? In our lab we are using Tucker Davis Technologies Electrophysiology equipment that can send out both analog and digital TTL pulses? I know from the TDT side I can just send out a constant digital signal, but what would the coding be for the arduino to only go through the loop? I was thinking of plugging a BNST cable into digital port thirteen and using an If... then... statement for the programming. Sorry for asking such basic questions, but like I said, time is just not permitting me to learn the coding as of right now.

Thanks in advance.

If im understanding you correctly, and thats a big IF, you could try doing a digital read on a pin and if pin (say 13)=high then loop. I would use a while loop on that one since if you are logging data I assume you want to log "indefinitely"

I was thinking about that, but wouldn't the timer continue on? Or would it pause until it received the signal again?

I was thinking about that, but wouldn't the timer continue on? Or would it pause until it received the signal again?

What timer? If you are referring to millis(), then, yes it would continue to run.

What you need to do is record when the "start logging" switch is pressed, and use "now - then" as the "time" in the log.

So I've actually just gone an alternative method and just use Matlab to process the data and reset the time. Only thing is now I realize I need two mouse trackers to record coordinates (If I have one mouse, it'll only track the y coordinates accurately, and not x, its complicated to explain.) So the problem is, how do I get two mice to record, from what I understand of the code, it only looks like one will work at a time. Is there anyway to modify the library without going through TOO much trouble?

#include <ps2.h>
/*
 * Pin 2 is the mouse data pin, pin 3 is the clock pin
 * Feel free to use whatever pins are convenient.
 */
  PS2Mouse mouse(3, 2);
  int onPin = 13;
  
  void setup()
  {
  Serial.begin(9600);
  mouse.init();
  //pinMode(onPin, INPUT);
  }

  void loop()
  {
    if (digitalRead(onPin) == HIGH)
      {
      MouseInfo mouseInfo;
      mouse.getData(&mouseInfo);
    
      //Cumulative X position
      Serial.print(mouseInfo.cX, DEC);
      Serial.print(",");
      //Cumulatie Y position
      Serial.print(mouseInfo.cY, DEC);
      Serial.print(",");
      Serial.print(millis()*0.001);
    
      Serial.println(); 
      } 

  }

We use compressed oxygen to levitate a mouse

That sounds incredibly dangerous - can you explain a little more?

So the problem is, how do I get two mice to record, from what I understand of the code, it only looks like one will work at a time.

You have one instance of the PS2Mouse class, connected to pins 2 and 3. You should be able to connect another instance to another pair of pins.

Then, in loop, create another instance of MouseInfo, and call getData() for each mouse.

On the other hand, if you are not getting good values for X AND Y for one mouse, you have a hardware or software problem that should be corrected instead of worked around.

Sorry meant the ball is levitated by air, just been really busy.

Anyways, I really don't understand what I am supposed to do (Sorry for being so needy on this one, its just this is literally the last part of the setup and its been a month since I said I' finish it, and college started making it harder for me to actually learn this coding).

#include <ps2.h>
/*
 * Pin 2 is the mouse data pin, pin 3 is the clock pin
 * Feel free to use whatever pins are convenient.
 */
  PS2Mouse mouse(3, 2);
  PS2Mouse mouse(7, 6);
  int onPin = 13;
  
  void setup()
  {
  Serial.begin(9600);
  mouse.init();
  pinMode(onPin, INPUT);
  }

  void loop()
  {
    if (digitalRead(onPin) == HIGH)
      {
      MouseInfo mouseInfo;
      MouseInfo2 mouseInfo;
      mouse.getData(&mouseInfo);
      mouse.getData(&mouseInfo2);
    
      //Cumulative X position
      Serial.print(mouseInfo.cY, DEC);
      Serial.print(",");
      //Cumulatie Y position
      Serial.print(mouseInfo2.cY, DEC);
      Serial.print(",");
      Serial.print(millis()*0.001);
    
      Serial.println(); 
      } 

  }

Saying "PS2Mouse mouse" is like saying "int i" (except you're defining a mouse rather than an integer). With that in mind, change what you would change if you wanted to make another variable called "int j" or something.

Your constructor is nearly good, but the two mice both have the same name - "mouse"
You'll also have to call everything, such as init() and read(), for both mice.

One potential problem -- for this PS2 keyboard library I'm using, it hard codes in the clock pin (because it's on an interrupt). Make sure that the PS2 library you're using does not do this. (Having you put in the clock pin for the constructor is a good sign, but you never know.)

DC5James05:
We use compressed oxygen to levitate a mouse and head fix the mouse in position ...

I was about to ring up Animal Welfare about that bit.

DC5James05:
... except for the initial 2.75 second of recording where both the X and Y coordinates are blank, but I have a feeling that is the mouse ...

It might not have liked the compressed oxygen.

Joking aside ...

(If I have one mouse, it'll only track the y coordinates accurately, and not x, its complicated to explain.)

Rather than working around that, this is the part you need to fix. Having two instances of a mouse class, which rely on interrupts, is hardly going to increase accuracy.

I struggle to understand what you are doing. You have a living mouse (small furry rodent) levitating on compressed oxygen? The movements of the mouse (the rodent) gets somehow translated into events from the PS2Mouse object in the Arduino sketch? And what does the following sentence mean?

With the arduino click mouse program, arduino is now sending out information in the form of: