Pet Pals (tamagotchi)


I made pet pal which is similar to what you can find in a tamagotchi and was the original inspiration for this project. I made “Pet Pals” more interactive, forcing the player to physically interact with the pet to keep it alive and happy.
“Pet Pals” has a variety of content to explore, such as a feeding mini game, playing mini game, sleeping and petting actions, as well as a decaying health bar.
Supplies


Here are all the components required for this project. Keep in mind that it is an interactive game, targeted towards kids so utilize lego, cardboard, or any other materials to make the project more aesthetically pleasing.
- Arduino UNO Board
- Micro Servo (3-pin, pack of 4)
- 10uF Capacitor (Electrolytic)
- 100uF Capacitor (Electrolytic)
- Buzzer (Active, 5V)
- LEDs (Pack of 10, Assorted Colors)
- Tactile Push Buttons (Pack of 10)
- Breadboard (Full Size)
- Mini Breadboard
- Male to Female Jumper Wires (Pack of 40)
- Ultrasonic Distance Sensor (HC-SR04)
- Joystick Module
- 330 Ohm Resistors (Pack)
- 10k Ohm Resistors (Pack)
- 555 Timer IC
- 10k Potentiometer
Pet Pal



We first need to make our actual Pet Pal. I’ve made by Pet Pal into a dog but you can do any pet based on your preference. Don't forget to decorate it!
Keep the back of the pal empty so you can add a servo which will to a piece of cardboard sticking acting as a tail. This tail (servo) will be later coded to wag when you successfully take care of the pet or any other actions. This gives the pet much more life, making it feel real.
Happiness Meter

This is your pet’s happiness meter. The point of this contraption is to display the happiness of your pet at all times. For our game to have any purpose, this servo will very slowly keep moving making its way to the far right indicating your pet’s death.
Through coding, we will make it so that winning a mini-game, petting, or putting your pet to sleep will increase this bar towards to the left (indicated by the heart). This mechanism will prompt the user to play a game or care for the pet in order to keep it alive and to the left half of the happiness meter.
Components:
-1x Servo
-Lego (make it look better)
-Paper (Used to indicate what the 2 sides are, the far left in my case represents happiness and the far right represents death).
Code:
Code Explanation:
happinessLevel is the main variable that stores how happy the pet is (0 = sad, 180 = max happiness).
Every 5 seconds (happinessDecayInterval), the updateHappiness() function automatically lowers happiness by happinessDecayStep (3 points) which simulates the pet being bored.
After three decays without interaction, a penalty message is shown in the Serial Monitor. This is to prevent flooding
The function updateHappinessServo() maps the current happiness level to the servo angle, updating the servo motor in the little block (happinessServo) to visually represent how the pet feels (Either loved or lonely).
If the happiness drops to 10 or below, the pet enters a sad/dead state:
A low buzzing tone plays.
The red LED flashes every 300ms.
The increaseHappiness() and decreaseHappiness() functions control how the pet’s happiness changes based on gameplay. These functions are what we will call upon in mini-games or when a sleep or pet action is done to increase the happiness. The decreaseHappiness() function is often used for when the player fails a minigame or when the player plays AND wins a game too much (we will discuss later).
Mini-game 1: Play Mini Game - Simon Says

This is our first mini-game, which will be referred to the “Play” mini-game was inspired by the hit game “Simon Says”. The goal of the game it to immitate a random series of 3 notes played through the buzzer using predetermined buttons which play 1 out of the 3 total pitches each. The player’s job is to press the buttons in the right sequence, matching the melody played. If done correctly, the happiness of the pet will go up, otherwise it will decrease.
Components:
-1x Piezo/Buzzer
-3x Buttons
-3x 10k Resistors
-1x Red led
-1x Green led
-2x 330 resistor (for the leds)
-Wires
Code:
Code Explanation:
Generate Sequence:
generateSequence() creates a pattern of 3 random notes using numbers 0–2.
Each number maps to a button and sound.
Play Sequence:
playSequence() plays the tones using tone(), with delays between them. This is what the player needs to copy.
User Input:
getPlayerInput() waits for the player to press the buttons one by one and checks if it matches the original sequence using waitForButton().
Results:
If correct:
Happy tones play
Tail wags
Happiness increases by +10 (using the increaseHappiness() function)
If the player has played 3 times in a row, the pet gets mad and punishes with -15 happiness (using the decreaseHappiness() function)
If incorrect:
Sad buzzer sound
Red LED
Happiness drops by -10
Mini-Game 2: Feed Mini-game




This mini-game is called the “Feed” mini-game. Like the name suggests, it will be another game in which you feed your pet (not literally). If you feed it according to what it wants (signalled by the red or green led) 3 times in a row, you win the mini-game. Use the potentiometer to move the servo arm over the correct food! A red lit led means he wants sausage and a green lit led means he wants plants. You better hurry though! Because our pet is not very patient…
Components:
-1x Servo
-1x Potentiometer
-Food, either drawn or lego to indicate the targeted positions.
-Lego or cardboard to turn the contraption more appealing and aesthetically pleasing.
-This will use a green and red led but you should already have it wired in step 3.
Code:
Code Explanation:
feedGame() FUNCTION
-This is the game loop that handles:
-Generating a random food request
-Showing LED indicator
-Waiting for a response within time
-Checking success or failure
while (correctStreak < 3) { ... }
-Runs until 3 correct feedings in a row.
while (millis() - startTime < patienceTime) { ... }
-Implements a countdown using millis() instead of delay() which lets the user interact without bugging the program. Its a bit random but its a prebuilt function into Arduino to help avoid these bugs.N
int potVal = analogRead(potPin);
int angle = map(potVal, 0, 1023, 0, 180);
-Maps the potentiometer range (0–1023) to servo angle (0–180°).
-Then compares to target zones:
-Sausage = angle between 30° and 60°
-Plants = angle between 120° and 150°
-This gives the user room for error, while still requiring accuracy.
increaseHappiness() & decreaseHappiness()
-Increases or decreases depending on if you win or not.
if (happiness > maxHappiness) happiness = maxHappiness;
-This makes sure that the happiness meter doesn’t exceed the limit. It is used in every action/mini-game on the pet.
randomSeed(analogRead(A1));
-This line ensures that food choices aren't always the same across resets. Without this, Arduino picks the same sequence every time. This makes it so the player has to pay attention
indicateFood(foodType)
-This turns on the right LED (either red for sausage or green for plant) and keeps tract of the food type.
if (correctStreak == 3)
Once the player gets 3 correct in a row, a happiness increase is given and the game ends. If the player loses, they lose happiness - same principle for all games.
Joystick and Sleep


Your pet is exhausted and wants to sleep. Guide it into the "sleep zone" using the joystick and keep it there for 5 continuous seconds. If you succeed, your pet falls asleep and gains happiness. If you fail by moving too much, the pet stays grumpy.
COMPONENTS:
-1x Joystick module
-Led (should already be wired)
HOW IT WORKS:
The joystick controls your pet’s position. The sleep zone is the center region of the joystick. If you stay centered for 5 seconds straight, you win. If you leave the zone, the timer resets. Green LED = pet is sleeping. Red LED = pet woke up.
CODE:
Code Explanation:
Joystick input:
int joyX = analogRead(joyXPin);
int joyY = analogRead(joyYPin);
-This reads the joystick's horizontal and vertical values. Center is roughly 512. We use a range to define the center zone (450–570) to make it more forgiving.
Sleep zone check:
bool joystickInCenter = (joyX >= centerMin && joyX <= centerMax &&
joyY >= centerMin && joyY <= centerMax);
-Both X and Y must stay in the center range. If either axis drifts too far, you exit the zone.
Time tracker:
if (!inSleepZone) {
sleepStart = millis();
}
-When you first enter the zone, the timer starts. If you stay centered for 5 full seconds (millis() - sleepStart >= 5000), the game is won.
Happiness:
increaseHappiness(15);
The pet gains happiness for winning the game. If you fail, you don’t lose happiness, but you waste time and must try again.
Led Feedback:
Green LED: You're in the zone and making progress
Red LED: You broke the pet’s sleep
JOYSTICK CONTROL:
This code links 3 directions of the joystick to a corresponding game depending on the x or y values (left right bottom). Using lego we can show what direction will led to the start of which mini-game.
555 Led Alternator



To make out project look a bit more vibrant, we can use leds. However, since we don't have any Arduino pins left, we can instead utilize the 555 timer to get a nice led swapper effect. Use the schematic or the photo to follow and create it for yourself!
Components:
-1x 555 Timer
-2x Led
-1x 10 uF capacitor
-1x 100 uF capacitor
-3x 1k resistors
-1x 10k resistor
Distance Sensor - Pet Feature


A distance sensor right above our pet’s head will be our final feature. When the player puts their hand over the pet, in a petting motion, the distance sensor will detect a smaller distance than usual and prompt and tail wag and a smaller increase in happiness!
readDistance() function:
-Sends a 10 microsecond pulse to the sensor trigger pin.
-Waits for the echo pin to go HIGH and measures the pulse duration in microseconds.
-Converts the time into distance using the speed of sound:
-distance (cm)=duration×0.0342distance (cm)=2duration×0.034
-The division by 2 accounts for the pulse traveling to the object and back.
detectPetting() function:
-Calls readDistance() to get the current distance reading.
-Checks if the distance is positive and less than 8 cm (meaning a hand is close enough).
-If so:
-Prints a message to Serial for debugging.
-Plays a happy tone on the buzzer (1000 Hz for 100 ms).
-Calls wagTail() to physically wag the tail servo.
-Calls increaseHappiness(3) to increase the pet’s happiness by 3 points.
-Delays 1 second to prevent the petting event from triggering multiple times too quickly.
Tail wag Code:
void wagTail() {
tailServo.write(60);
delay(150);
tailServo.write(120);
delay(150);
tailServo.write(90);
}
-Moves the servo in a way that imitates a wagging tail
Create




Steps 1-7 have led you to the creation of all the segments of the pet! From here, use jumper wires to hide the bread board and Arduino in the back, make a segment with the led’s and buzzers, bring out the alternating lights. Basically your game functions perfectly but now you must make it look appealing and something thats well put together. This is purely subjective so try your hand on different layouts, designs, etc.
Full Code
There are many little additions or niche concepts i added to give my code some more life. This master code has combined all the previous code for the different steps into one and made it flow much better. I would highly recommend that you work your way alone and find what you like but here is my full code if you wish to get an idea: