CatBot help, please- randomize

Hi!
CatBot is two servos moving at random with a laser, to play with your cat. There are a lot of published projects, but they all have one problem.
They all go: Servo1, Servo2, delay.

I want Servo1 and Servo2 to fire independently. I think I could use millis for this, but that’s a lot of code when I want “these two things should happen independently of each other.”

This one works, but only for a few cycles. I think its because i get the two random numbers at the same time. It’s a Nano.
The second one works great, never stops. But it’s a lot of code, and it’s sequential.

#include <Arduino.h>
#include <Servo.h>

Servo vert; //Vertical servo
int servov = 0;


Servo hort; // Horisontal servo
int servoh = 0;

int wait = random(100, 4500);

void setup() {
  vert.attach(9); 
  hort.attach(10);
}



void loop()
  {
    int randh = random(90, 160);
    int randv = random(40, 120);

    vert.write(randv);
    delay(wait);
    hort.write(randh);
    delay(wait);
  }

This was in Italian:

#include <Arduino.h>

#include <Servo.h>

Servo one; //Vertical servo

Servo two; // Horisontal servo

int pos = 0; //store servo possition.

int wait = random(0, 30); //randomize the wait to make it more interesting

void setup() {

  one.attach(9); //attach servos

  two.attach(10);

}

void migrate(Servo &myServo, int newPos)

{

  int pos = myServo.read(); //Read the current 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 randv = random(40, 100); //The range is limited to 60 deg for better cat action

    migrate(one, randv);

    int randh = random(90, 135); //The vertical range is limited to 45 deg also for better cat action.

    migrate(two, randh);

  }

void loop()

  {

    randomPosition();

    delay(random(100, 3000));

  }

I want each servo to have its own random period.

Edited to remove the so called fix for millis() rollover - which would not work!

Still using delays. I don’t want to do that.

If you mean the delay when millis() rolls over, then the code above most likely won’t work. I’ve edited it to remove it.

If you mean that you want theservos to keep moving then:

This is untested, but should work.

Have fun!

Cheers,
Norm.

//===========================================================
// UNTESTED code to randomly move a pair of servos
// controlling a laser cutter for cat target practice!
//
// Norman Dunbar
// 31 October 2020
//
// This code, (if it works!) is public domain.
//===========================================================

// Horizontal and Vertical servos.
servo H, V;

// Define servo minimum and maximum positions.
#define MIN_V 40
#define MAX_V 120

#define MIN_H 90
#define MAX_H 160

void setup() {
    // Whatever you need goes here.

    // Do we need to randomise the randomness? If
    // so, call randomize() passing a suitable value.
    // time(0) usually. 
    randomize(time(0));
}

void loop() {
        // Move H servo to a random position.
        H.write(random(MIN_H, MAX_H));

        // Move V servo to a random position.
        V.write(random(MIN_V, MAX_V));
}

Cheers,
Norm.

Yes, this gets closer. But the H and the V still happen in sequence. It’s always going to be H and then V.
I don’t want to track the servo position, and I don’t want to track the time. As it is, it should stay in one region of a wall. It still hits the ceiling, but I haven’t calibrated that.

You didn’t declare time.
‘Randomize’ is something you can use without a library?

I have the move interval time randomized (0-3000), but I want the H and the V to happen randomly. The H servo and the V servo aren’t in sequence. I want to make two things and then I want those things to actuate randomly relative only to themselves.

A cat will get the hang of the H then V thing.

My doodads came from Ali today, so I can fix the 55g servo to the platforrm. I can finish it now!
I have a problem getting it to run all day. I think the one I took verbatim from https://www.instructables.com/CatBot-Automated-Cat-Laser/
runs all day, but it has a 2-second delay at the end. Any change I make to that makes the program sieze up after a while.

That’s because the Uno can’t do separate tasks in parallel. You could randomise things a bit:

If (random(0,2) ==1) {
    H.write(random(MIN_H, MAX_H));
    V.write(random(MIN_V, MAX_V));
} else {
    V.write(random(MIN_V, MAX_V));
    H.write(random(MIN_H, MAX_H));
}

I doubt your cats will work it out though. :grin:

I know I didn’t, I said it was untested! I didn’t include Servo.h either.

randomSeed() is the Arduino version of randomize(), so use that instead. Example here:

I’ve looked at that code, nothing much can go wrong it appears. I’m wondering what changes you made that lock it up? The migrate() function moves the servos one step at a time then delays for between 30 and 59 milliseconds between steps.

Cheers,
Norm

I was thinking about doing it with an 8266 or 32, using the two processors. Pro moves H and App moves V. But I already have the Nano wired.