IoT Cat Toys
A paired set of two cat toys for long distance friends and their felines. When one cat plays with their toy - the other toy starts to move!
Components and supplies
2
USB wall adapter plug
2
5mm Orange LED
2
Resistor 100 ohm
1
Heat Shrink Tubing, Assorted Sizes
1
Stranded wire
2
5mm Green LED
2
USB to micro USB cable
2
Fast Vibration Sensor Switch
2
Micro servo (TowerPro SG92R)
2
Adafruit Feather HUZZAH with ESP8266 WiFi
Tools and machines
1
Heat Gun
1
Sander
1
Soldering kit
1
Laser cutter (generic)
Apps and platforms
1
Adafruit IO
1
Arduino IDE
Project description
Code
Cat-to-Cat code
cpp
Code using Arduino IDE
1// Combining Inputs and Outputs for paired cat toys 2// Flick (vibration sensor) switch sends command to AIO feed 3// LED and vibrating motor flash/buzz according to feed data 4// 5// Modified from Becky Stern's code by Erica Fink 2023 6// based on examples from Adafruit IO Arduino Library: 7// https://github.com/adafruit/Adafruit_IO_Arduino 8// 9// Adafruit invests time and resources providing this open source code. 10// Please support Adafruit and open source hardware by purchasing 11// products from Adafruit! 12// 13// Written by Todd Treece for Adafruit Industries 14// Copyright (c) 2016 Adafruit Industries 15// Licensed under the MIT license. 16// 17// All text above must be included in any redistribution. 18 19 20/************************ Adafruit IO Configuration *******************************/ 21 22// update with your io.adafruit.com information 23#define IO_USERNAME "enter your user name here" 24#define IO_KEY "enter your key here" 25 26/******************************* WIFI Configuration **************************************/ 27 28// update with your wifi information 29#define WIFI_SSID "enter your wifi name here" 30#define WIFI_PASS "enter your wifi password here" 31 32#include "AdafruitIO_WiFi.h" 33AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS); 34 35/************************ Main Code Starts Here *******************************/ 36 37#include <ESP8266WiFi.h> 38#include <AdafruitIO.h> 39#include <Adafruit_MQTT.h> 40#include <ArduinoHttpClient.h> 41#include <Servo.h> 42 43Servo myservo; // create servo object to control a servo 44// twelve servo objects can be created on most boards 45 46#define ToyLEDsentIndicator_PIN 13 47#define ToyLEDreceivedInidicator_PIN 12 48#define ToyFLICKswitch_PIN 4 // this pin needs pullup capability 49 50// Define some items that will be used to set your FLICK switch into a sleeper mode 51unsigned long previousMillis = 0; // this will store last time FLICKswitch was set “on”/activated 52 53// This constant won’t change. 54// Set the interval to the time you want the toy to "sleep" in between plays (so that the motor is not constantly setting the other toy off) 55// The toy will sleep on reading the FLICKswitch. It is set to sleep for 20 minutes (milliseconds*seconds*minutes) 56const long interval = 1000*60*20; 57 58// set up the 'digital' feed 59// create your own feed on io.adafruit.com and use the name for "toyforcats" here and throughout below 60AdafruitIO_Feed *toyforcatsfeed = io.feed("toyforcatsfeed"); 61 62void setup() { 63 64 myservo.attach(5); // this is the pin number on your board that the servo is connected to 65 66 // set FLICK switch pins as inputs with internal pull-up resistor 67 pinMode(ToyFLICKswitch_PIN, INPUT_PULLUP); 68 69 // set LED pins and motor pin as a digital outputs 70 pinMode(ToyLEDsentIndicator_PIN, OUTPUT); 71 pinMode(ToyLEDreceivedInidicator_PIN, OUTPUT); 72 73 //pinMode(ToyMOTOR_PIN, OUTPUT); 74 75 // start the serial connection 76 Serial.begin(115200); 77 78 // connect to io.adafruit.com 79 Serial.print("Connecting to Adafruit IO"); 80 io.connect(); 81 // set up a message handler for the 'toyforcatsfeed' feed. 82 // the handleMessage function (defined below) 83 // will be called whenever a message is 84 // received from adafruit io. 85toyforcatsfeed->onMessage(handleMessage); 86 87 // wait for a connection 88 while(io.status() < AIO_CONNECTED) { 89 Serial.print("."); 90 delay(500); 91 } 92 93 // we are connected 94 Serial.println(); 95 Serial.println(io.statusText()); 96 97} 98 99void loop() { 100 101 // io.run(); is required for all sketches. 102 // it should always be present at the top of your loop 103 // function. it keeps the client connected to 104 // io.adafruit.com, and processes any incoming data. 105 io.run(); 106 107 //code that needs to be running all the time. 108 // check to see if it's time to read the FLICKswitch; that is, if the difference 109 // between the current time and last time you activated the FLICKswitch is bigger than 110 // the interval at which you want to activate the FLICKswitch. 111 unsigned long currentMillis = millis(); 112 113 if (currentMillis - previousMillis <= interval) { 114 // If it is still less than the interval (20 minutes), then return back to this mini loop (doing nothing) until it has been greater than the interval (20 minutes). 115 return; 116 } 117 118 // grab the current state of the FLICK switch. 119 // we have to flip the logic because we are 120 // using INPUT_PULLUP 121 if(digitalRead(ToyFLICKswitch_PIN) == LOW) { 122 // save the flick activation state to the 'digital' toyforcatsfeed feed on adafruit io 123 Serial.print("sending FLICK switch -> "); 124 //switch 1 for 2 on opposite toy chip 125 Serial.println(2); 126 toyforcatsfeed->save(2); 127 128 digitalWrite(ToyLEDsentIndicator_PIN, HIGH); 129 delay(1000); 130 digitalWrite(ToyLEDsentIndicator_PIN, LOW); 131 132//Reset the sleeper timer so that it has to reach 20 minutes before it reads for a FLICK switch activation again. 133 previousMillis = currentMillis; 134 } 135 136} 137 138// this function is called upon whenever a 'toyforcatsfeed' message 139// is received from Adafruit IO. it was attached to 140// the toyforcatsfeed feed in the setup() function above. 141void handleMessage(AdafruitIO_Data *data) { 142 143 int toyforcatsfeed = data->toInt(); 144 145//switch 2 for 1 on opposite toy chip 146 if (toyforcatsfeed == 1){ //light up the "received" LED and move the motor sequentially 147 Serial.print("received <- "); 148 Serial.println(toyforcatsfeed); 149 digitalWrite(ToyLEDreceivedInidicator_PIN, HIGH); 150 delay(1000); 151 digitalWrite(ToyLEDreceivedInidicator_PIN, LOW); 152 153//move the motor back and forth 3 times 154 Serial.print("received <- "); 155 Serial.println(toyforcatsfeed); 156 myservo.write(50); // tell servo to go to position in variable 'pos' 157 delay(500); 158 myservo.write(0); // tell servo to go to position in variable 'pos' 159 delay(500); 160 myservo.write(50); // tell servo to go to position in variable 'pos' 161 delay(500); 162 163 } else { 164 Serial.print("unexpected value <- "); 165 Serial.println(toyforcatsfeed); 166 } 167}
Downloadable files
Laser cut template for container
Laser cut basswood using the template to layer
https://content.instructables.com/F27/COOV/LPSCOMCP/F27COOVLPSCOMCP.ai
Comments
Only logged in users can leave comments