Timer problem with Nano v.3 (Pet Robot Toy)

As a beginner myself i like to try some little projects.
Here’s a small taste of a simple Arduino project. The CAPT - Computer aided pet toy :slight_smile:

Using two servos, an Arduino Nano and a cheap “Cat-it Laser Mouse”

Youtube - Computer aided pet toy :slight_smile:

// This code is designed for Nano V.3 
// this code is public domain, enjoy!

#include <Servo.h>

Servo ver; 
Servo hor;
boolean replay = true;

void setup() { 
  ver.attach(2);
  hor.attach(3); 
  pinMode(4, OUTPUT);
  pinMode(13, OUTPUT);
}
void migrate(Servo &myServo, int newPos) {
  int wait=random(40,80); //random (40,80) Testing (10,30)
  int pos = myServo.read(); //Read servo position
  if (pos < newPos) { 
    for (int i=pos; i < newPos; i++) {
      myServo.write(i);
      delay(wait); 
    }
  } else { 
    for (int i=pos; i > newPos; i--) { 
      myServo.write(i);
      delay(wait);
    }
  }
}
    
  
void randomPosition() {
  int rand=random(20,100); //Horzontal range 80 deg 
  migrate(hor, rand);

  rand=random(85,110); //Vertical range around 25 deg 
  migrate(ver, rand);
}
   
void loop() { 
  digitalWrite(4, HIGH);   // turn the Laser on
  digitalWrite(13, HIGH);   // turn onboard led on :-))
  if (replay == true)
  randomPosition();
  delay(1000);
}

Works fine so far…

But here is my problem. :o

I will turn off automatically the toy after a time. I found a tutorial for “How to Construct Time-Sensitive While Loops”

Arduino Programming Tutorials

I try to put in the code in my sketch … It should be stop in 50 seconds, but it will not really work :frowning:

// This code is designed for Nano V.3 
// this code is public domain, enjoy!

#include <Servo.h>

Servo ver; 
Servo hor;
boolean replay = true;

unsigned long time_since_last_reset = 0;
int interval_one = 50000;
int interval_two = 1000;

void setup() { 
  ver.attach(2);
  hor.attach(3); 
  pinMode(4, OUTPUT);
  pinMode(13, OUTPUT);
}

void migrate(Servo &myServo, int newPos) {
  int wait=random(40,80); //random (40,60) (10,30)
  int pos = myServo.read(); //Read servo position
  if (pos < newPos) { 
    for (int i=pos; i < newPos; i++) {
      myServo.write(i);
      delay(wait); 
    }
  } else { 
    for (int i=pos; i > newPos; i--) { 
      myServo.write(i);
      delay(wait);
    }
  }
}
    
  
void randomPosition() {
  int rand=random(20,100); //Horzontal range 80 deg 
  migrate(hor, rand);
  
  rand=random(85,110); //Vertical range around 25 deg
  migrate(ver, rand);
}
   
void loop() { 
    digitalWrite(4, HIGH);   // turn the Laser on
    digitalWrite(13, HIGH);   // turn onboard led on :-))
 time_since_last_reset = millis();
 while((millis() - time_since_last_reset) < interval_one){
  }
  randomPosition();
  delay(1500);

 time_since_last_reset = millis();
 while((millis() - time_since_last_reset) < interval_two){
  }
   replay = false;
 digitalWrite(4, LOW);   // turn the Laser off
 
}

Any help with this code would be greatly appreciated.
Thank you for your time.

50000 won't fit in an int, although the compiler will try and cram it in there for you causing perhaps unexpected results.

Make all your int variables that you're using for measuring time unsigned long instead. And tell the compiler that large constants (such as 50000) are unsigned long by putting a UL suffix on them.

  time_since_last_reset = millis();
  while((millis() - time_since_last_reset) < interval_one){
  }

This loop acts exactly the same as: delay(interval_one);

“It should be stop in 50 seconds, but it will not really work”

The code you wrote will delay your program for the specified interval and then continue. It will NOT stop the program after 50 seconds.

Thanks for the help !

Life can be so easy 8)

if(millis() >= 50000)
{
while(1)
{
}
}
}

Now it stops after 50 seconds ;D

Powering the chip down or putting it to sleep is a lot more effective than running an infinite code-less loop forever:

http://www.gammon.com.au/power

Your loop means that the Arduino is running at full power, but not doing anything useful. Your power consumption is still at max. Powering it down after you don't need it to do anything else is a lot more effective in terms of code and power usage.

AlxDroidDev: Your loop means that the Arduino is running at full power, but not doing anything useful. Your power consumption is still at max. Powering it down after you don't need it to do anything else is a lot more effective in terms of code and power usage.

Thanks for you comment AlxDroidDev. I will try the power down funktion for another project. In my case it doesn't mater. ;) I use a USB batteri pack that turns off when load goes down. When the sketch stops, it take around 60 seconds and the battery pack turns off.

Next idea is to connect a relay to cut the current when the time goes out. To turn on och start the sketch I can use a pushbutton. :cold_sweat: