Defensive Pot Plant: Stupid Pet Trick

After jumping around from one idea to another for quite a bit of time, I finally decided on an idea – I was going to make a creature that would react in a progressively panicked way as something got closer to it. As I coded the project and went through materials to decide what I was going to make, the vague idea of two creatures evolved into that of a pot plant and a seal. As the seal got closer to the plant, the plant would do three things: 1) make more panicked music 2) flash progressively more red lights and 3) get its fists out, ready to fight the seal.

The code for the project was relatively straightforward. I struggled a bit with the tone command — no matter how I wrote the command, the music wouldn’t play — I had tried everything and was about to give up when I realized that the code wasn’t working because I’d put in “>” instead of “<”. The struggle is real!

Here’s the code:

#include <Servo.h>


#include "pitches.h"



const int ledred1 = 3;
const int ledred2 = 6;
const int ledblue1 = 5;
const int ledblue2 = 9;
const int ledred3 = 10;


int sensor = A0;

int ledState3 = LOW;
int ledState4 = LOW;
int ledState5 = LOW;
int ledState6 = LOW;


int melody1[] = {
 NOTE_C5, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_G4, 0, NOTE_B4, NOTE_C5
};

int melody2[] = {
 NOTE_C3, NOTE_G3, NOTE_G4, NOTE_A4, NOTE_GS4, 0, NOTE_A4, NOTE_C5
};

int melody3[] = {
 NOTE_C5, NOTE_G4
};

int noteDurations[] = {
 4, 8, 8, 4, 4, 4, 4, 4
};

Servo myservo1;
Servo myservo2;

int beat_counter = 0;
void setup() {

 pinMode(ledred1, OUTPUT);
 pinMode(ledred2, OUTPUT);
 pinMode(ledblue1, OUTPUT);
 pinMode(ledblue2, OUTPUT);
 pinMode(ledred3, OUTPUT);
 Serial.begin(9600);

 myservo1.attach(12);
 myservo2.attach(11);

 // put your setup code here, to run once:




}

void loop() {


 // put your main code here, to run repeatedly:
 Serial.println(analogRead(sensor));

 if (analogRead(sensor) < 200) {
 digitalWrite(ledblue1, LOW);
 digitalWrite(ledblue2, LOW);
 digitalWrite(ledred1, LOW);
 digitalWrite(ledred2, LOW);
 digitalWrite(ledred3, LOW);

 
 //tone(2, melody1[1], noteDurations[1] * 100);
 for (int counter = 0; counter < 8; counter++) {
 tone(2, melody1[counter], noteDurations[counter] * 150);
 delay(150);
 noTone(2);

 myservo1.write(0);
 delay(10);
 myservo2.write(0);
 }
 }


 else if (analogRead(sensor) < 370) {
 analogWrite(ledblue1, 20);
 analogWrite(ledblue2, 20);
 analogWrite(ledred1, 20);
 analogWrite(ledred2, 20);
 analogWrite(ledred3, 20);


 for (int counter = 0; counter < 8; counter++) {
 tone(2, melody2[counter], noteDurations[counter] * 150);
 
 delay(150);
 noTone(2);

 myservo1.write(90);
 delay(10);
 myservo2.write(90);
 }

 }


 else if (analogRead(sensor) < 500) {
 digitalWrite(ledblue1, HIGH);
 digitalWrite(ledblue2, HIGH);
 digitalWrite(ledred1, HIGH);
 digitalWrite(ledred2, HIGH);
 digitalWrite(ledred3, HIGH);


 for (int counter = 0; counter < 2; counter++) {
 tone(2, melody2[counter], noteDurations[counter] * 150);
 delay(150);
 noTone(2);

 myservo1.write(180);
 delay(10);
 myservo2.write(180);
 }


 }


}

Most of the challenges of this project were hardware-based. Luckily, I found a box with a low-rise and sturdy ceiling, so it could do both the things I wanted it to — support the pot plant on top while still making it easy for the breadboard to lie in there undisturbed.

I soldered the LEDs, the distance sensor, the two Servos and carved out a hole in the middle of the box and came out through the hole in the middle of the pot. It looked like this:

By now, I had realized that the sensor had a very specific range that I wasn’t always able to get the seal within. A solution to this was to use a tube sort of structure which would put the seal directly in the range of the sensor. Another advantage of this structure was that it would make the project a little more intuitive for someone approaching it for the first time.

Annnd this is how the final version looks!

For the future, these are the things I would do better with this project:

  • The music: I would ramp it up to be more distinctive in terms of “happy” and “panicked”. I would also try to get the buzzer outside of the box so that the music is louder.
  • Add more LEDs to make the visual imagery more striking.
  • Make the transition between various stages smoother so that it feels like a smooth transition instead of three clear distinctions between the various stages of panic.