Sad Cat Fixer

by Bigcamps in Circuits > Arduino

256 Views, 2 Favorites, 0 Comments

Sad Cat Fixer

cat.png

This project is meant to be the circuitry for a cat toy that would make a sad cat happy. It is intended to pick up the cat's movements with the ultrasonic distance sensor, and when the cat gets close enough the toy would start playtime for the cat. The LED turns on, and the servo motors would activate by changing their position randomly. When this happens it would have simulated the toy if I was able to construct it. What would have happened is the ultrasonic distance sensor would read when the cat walks by, then the servos would move, which would be connected to the LED in some way, thus moving the LED that is now turned on. The LED would have been a laser pointer that the cat would chase. After 10 minutes the LED and servos would turn off and have set time until it could activate again. But since I was unable to build the actual toy, here is how to build the circuit instead.

Supplies

  • Arduino Uno
  • Ultrasonic Distance Sensor
  • Two Servo Motors
  • LED
  • 220 OHM Resistor
  • Wires

Add LED to Circuit

circuit (6).png

It is best to start off with the easiest part of the circuit. This is the LED, it is attached to pin 12, and has a 220-ohm resistor connected to it.

Servos

circuit (5).png

After the LED, add the servos to the circuit. One of them connects to pin 3, power, and ground. The other servo attaches to pin 5, power and ground.

Ultrasonic Distance Sensor

circuit (4).png

Once the servos have been connected to the circuit, it's time to add the ultrasonic distance sensor. The trigger pin attaches to pin 10 on the Arduino, the echo pin attaches to pin 9 on the Arduino, VCC attaches to power, and GND attaches to ground.

End Result of the Product

Bigno humphry.png

Once you connect all your components, the end result should look something like this.

Code for the Ciruit

https://create.arduino.cc/editor/bigcamps/4586c5b0...

If you open that link, it will take you to the code for the circuit.

This code works by first declaring everything it needs to work. First include the servo library since we will need that in order to get the servos moving. Then we declare variables that never change, either setting a component to a pin, or a set time that we will need for the timer. whenever an unsigned long is used, the reason is that we need that variable to hold variable to hold very large numbers since we will be dealing with milliseconds. Then variables are declared that will be updated and changed constantly, and we create an object for both the servo motors.

Now onto the setup function. Here we start up randomSeed, which basically allows us to generate random numbers. It is set to a pin that is not connected to anything externally. After that we attach our two servos to their own pins on the Arduino, and then set them to 0 degrees. To finish off the setup function we make the LED an output.

Lastly is the loop function, which starts off with the code for the ultrasonic distance sensor. What this does is set the trigger pin as an output then turns it off to make sure no reading has been sent out already and delays very shortly to allow for some buffer time. It is then turned on, which sends out its signal, has a small delay, and is immediately turned back off again. Once that happens the echo pin is declared as an input and sets a variable as the time it took for the signal that the trigger pin sent out to return to the echo pin. And finally it sets a different variable to the distance of the object the ultrasonic sensor, the calculation happens in the function at the bottom of the code. Once this calculation is done, an if statement checks to see if the distance is greater than 80 inches, and if it is this is where we get the information for the time. The millis command sets a variable (currentTime) to how long the code has been running, then another variable (eventInterval) is set to currentTime plus eventTime (which is a constant variable set to 15 seconds, this can be changed and set to a longer time in order for the cat to get more playtime, but for testing 15 seconds works well) and this allows for a timer to be set for 15 seconds. All of that will keep looping until the distance of the object, which hopefully means the cat walks by, is less than 80 inches. Once that happens there is a while loop, its conditions being when currentTime is less than eventInterval. When this condition is true the LED turns on, random numbers are set to variable sideDeg and upDeg between either 0 and 120 or 0 and 90, then each servo motor is set to one of those random numbers, which turns it to that number in degrees, and the variable currentTime is updated. Once this condition is no longer met the LED turns off, both servos are set back to 0 degrees, and a delay of 10 seconds occurs so the circuit does not start up again immediately once it turns off (also I just like to think that the circuit was working hard and that the delay is a break for it :) ). Even if the cat remains there after the delay ends, currentTime will never get updated until it moves away, assuring that the circuit will not start up again until the cat leaves then returns. And that is how the circuit works.

Video

Click on the link below and it will take you to a video of the circuit working.

https://drive.google.com/file/d/1chte1ucgImANy-P3B...

Sorry for the poor quality, but when I move the circle, I am moving it from a distance greater than 80 inches to a distance less than 80 inches, then back to greater.