Sunday, March 12, 2017

Arduino Adventures in Cat Entertainment

Arduinos are little computer boards that come with all sorts of sensors, motors, lights, and other fun add-ons that you can use for projects. Starting with lighting up an LED, the Arduino guides available online for free can get the brain-juices flowing and soon you end up with...well, you end up with this.

The Self-Operated Cat Treat Feeder

Ticky now has a cool fun toy that gives him treats when he pushes a button. The trick is now to get him to relate button-pushing with treat-getting. (And also to keep too many treats from being released out of the tube at once.) He's working on it. We're currently to the "paw at the button and Allison pushes the button for me" phase and it's only day one. Give it time.

Here's a quick breakdown of the parts: 

-  Arduino Uno board
- Breakout breadboard
- 10 kOhm resistor (servo circuit)
- 220 Ohm resistor (LED circuit)
- Pushbutton
- LED 
- 5V servo motor
- various lengths of jumper cable and soldered-on 22 AWG wire

- A crapton of duct tape. Like a LOT of duct tape
- 2 Cereal boxes
- 2 round plastic containers w/lids (I think they had cashews in them originally)
- The cap off of a bottle of Ranch
- A bubble tea straw

- Soldering iron (tiny tip) and solder
- Wire strippers
- Wire cutting pliers
- Scissors
-Exacto knife
- Hot glue gun
- Computer w/(free!) Arduino software installed
- USB cable for the Arudino

Tools I WISH I had purchased for this:
- One of those wire-gripping gooseneck gadgets that I see the technicians at work use, because it is GODAWFUL to try and hold wires in place next to each other with tape.
- Ohmmeter, because it's nice to know that it's your soldering's faulty, not your component, rather than guessing.
- Solid core wire, not stranded wire. I have no idea why I purchased stranded wire. I guess I hated my future self. (Or more likely didn't look at it closely enough when I bought it.)

Ticky says he can solder too. And by solder he means sit on my work area.
I had to sequester him to the bedroom during actual soldering. I was afraid he'd burn himself jumping up to investigate.

I started out with combining the example codes from the Servo Motor example, the Button example and the Blink Without Delay example from the Arduino's Examples folder. I wanted to be able to move the servo motor 90 degrees and back again in a certain length of time when I pushed the button. The motor would have a little plastic arm attached to it which would block the bottom of the treat straw when closed, open when the button was pushed to drop a treat, then close the straw back up. The LED was just a last minute addition because it was neat to have a light indicator when the button was pressed. I'll include the code at the bottom of the post for people who want to try it. I don't have any Fritzing diagram software installed, so you'll just have to figure out how to do (individually) Button Circuit, LED Circuit, and Servo Circuit. They aren't really combined on the board itself. The software handles the inputs and outputs. Just make sure you're following the diagrams from Examples 1, 5, and 8 in the Arduino SIK User Guide:

Then came the hard part: Soldering. I've only soldered maybe three things in my entire life, way back at the beginning of undergrad, and had no idea what I was doing back then either. I practiced first on some lengths of wire before I switched over to my actual hardware. Turns out that was a good choice, as I destroyed several wire ends with over-cooked solder. Once I wasn't routinely frying wires or dribbling solder all over, I soldered wires to the ends of the LED and the pushbutton to extend them out so they could be used off of the breadboard. I also had to tin the ends of the wires that would go into the breadboard because for some reason I bought stranded wire??? I need to get solid core.

I taped everything into the inside of a cereal box, and then cut a slit in the corner for the servo arm to move 90 degrees through. The straw was taped just above that. A hole was cut nearby for the LED to poke through. I duct taped the jumper cables to the breadboard and the Arduino before putting them in the cereal box just to make sure they didn't pop loose.

Top view of cereal box housing
(Arduino power plug needs to be facing up...
That was fixed for this pic. haha.)

Cutting a side door in the housing makes the breadboard easier to get to!

The treat hopper arrangement.
The Cinnamon Toast Crunch flakes look excited about it?
The pushbutton was mounted to an inverted round plastic container, pushbutton poking through a hole cut in that. Then a second plastic container with a hole for the Ranch lid was positioned on top of the first container to act as a guide so the Ranch lid button slid up and down but didn't fall off.

Button Assembly

The Ranch lid was initially too deep, so I cut some cardboard to shove in the bottom of it to make it the correct depth for pushing the button with one bop of a tiny paw.

Then I duct taped everything down so it isn't suuuper easy for Ticky to destroy. He still can if I leave him alone with it, but hey- its prototype stage.

Ticky has been trying it out, but I don't have any good pictures of it yet because I'm trying to keep him from destroying the machinery in search of treats at the moment. I do have a video of it working though! It's apparently too big for Blogger but I've posted it on my Facebook page. I hope to create more cool Arduino stuff in the near future!

Code for the Arduino:

//This code was created to drive a servo motor and light an LED
//with the push of a button.

#include <Servo.h>  // servo library

Servo servo1;  // servo control object
const int pushButton = 2;     //pushbutton on digital pin 2
const int ledPin =  13;      // LED on digital pin 13

void setup()

  // Attach tells the Arduino to begin sending control signals
  // to the servo. Servos require a continuous stream of control
  // signals, even if you're not currently moving them.
  // While the servo is being controlled, it will hold its
  // current position with some force. If you ever want to
  // release the servo (allowing it to be turned by hand),
  // you can call servo1.detach().


  // initialize serial communication at 9600 bits per second:
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
   // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);

void loop()
  int position;
  int buttonState = digitalRead(pushButton);

  // print out the state of the button:
  delay(1);        // delay in between reads for stability
  //Note: Still playing with the above two lines.
  //Can't seem to print in real time with it.

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
       // turn LED on:
    digitalWrite(ledPin, HIGH);
     // Change position if button pressed:
  servo1.write(0);    // Tell servo to go to 0 degrees

  delay(200);         // Pause to give it time to move

   servo1.write(90);    // Tell servo to go to 90 degrees
//  delay(1000);         // Pause to give it time to move
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);


No comments:

Post a Comment