For this servo motor project, I made an emotion generator that randomly selects an emotion to display along with its corresponding LED color and sound. It’s based on the wide range of emotions we feel on a day to day basis and how these can change quickly depending on random factors.

The code provides a random number within a 6-number range, each emotion is assigned one of the numbers. Over the servo motor there is a wheel with the different emotions which is turned depending on the number that is selected. On the front of the box, there is an opening that only shows the emotion that is outputted at that moment. Once this changes, the LED color matches the emotion’s color and the buzzer outputs a note relevant to the emotion that is being projected.
My original idea for this project was to make a random emotion generator that reflected our feelings when our personal space is invaded. The servo wheel, in addition to the emotions, would have hanging ladder-like barriers to simulate distance between people (the barriers and the sensor). However due to the materials available and the timing, it was not possible to make the Ultrasonic Sensor provide accurate readings within the space of the box.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Include libraries for Servo, Buzzer and Ultrasonic Sensor | |
#include <Servo.h> | |
#include "pitches.h" | |
//Servo | |
Servo myservo; | |
//Servo position | |
int pos = 0; | |
//Buzzer | |
const int buzzer = 8; | |
//RGB LED | |
const int redPin = 3; | |
const int greenPin = 5; | |
const int bluePin = 6; | |
void setup() { | |
// put your setup code here, to run once: | |
myservo.attach(9); | |
pinMode (buzzer, OUTPUT); | |
pinMode (redPin, OUTPUT); | |
pinMode (greenPin, OUTPUT); | |
pinMode (bluePin, OUTPUT); | |
Serial.begin(9600); | |
delay(1000); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
reset(); | |
actServo(); | |
delay(2500); | |
} | |
//Generate a random number that is assigned to an emotion. Define the position of Servo for each emotion. | |
int previousEmotion = 0; | |
void actServo(){ | |
int emotion = random(0,6); | |
//Check if value of emotion is equal to the previous one. If so, change to a new value until it's different. | |
while(emotion == previousEmotion){ | |
emotion = random(0,6); | |
} | |
previousEmotion = emotion; | |
//Angry - Red | |
if (emotion == 0){ | |
pos = 0; | |
myservo.write(pos); | |
angry(); | |
} | |
//Excited - Orange | |
if (emotion == 1){ | |
pos = 36; | |
myservo.write(pos); | |
excited(); | |
} | |
//Happy - Yellow | |
if (emotion == 2){ | |
pos = 72; | |
myservo.write(pos); | |
happy(); | |
} | |
//Envy - Green | |
if (emotion == 3){ | |
pos = 108; | |
myservo.write(pos); | |
envy(); | |
} | |
//Sad - Blue | |
if (emotion == 4){ | |
pos = 144; | |
myservo.write(pos); | |
sad(); | |
} | |
//Peaceful - Purple | |
if (emotion == 5){ | |
pos = 180; | |
myservo.write(pos); | |
peaceful(); | |
} | |
//Serial.println(emotion); | |
} | |
//Following voids call each emotion, their colors and notes. Tone calls buzzer, plays note, duration. | |
void angry(){ | |
analogWrite (redPin, 255); | |
analogWrite (greenPin, 0); | |
analogWrite (bluePin, 0); | |
tone(buzzer, NOTE_GS1, 600); | |
} | |
void excited(){ | |
analogWrite (redPin, 255); | |
analogWrite (greenPin, 30); | |
analogWrite (bluePin, 0); | |
tone(buzzer, NOTE_C6, 600); | |
} | |
void happy(){ | |
analogWrite (redPin, 255); | |
analogWrite (greenPin, 130); | |
analogWrite (bluePin, 0); | |
tone(buzzer, NOTE_E5, 600); | |
} | |
void envy(){ | |
analogWrite (redPin, 3); | |
analogWrite (greenPin, 255); | |
analogWrite (bluePin, 1); | |
tone(buzzer, NOTE_FS2, 600); | |
} | |
void sad(){ | |
analogWrite (redPin, 0); | |
analogWrite (greenPin, 80); | |
analogWrite (bluePin, 255); | |
tone(buzzer, NOTE_C4, 600); | |
} | |
void peaceful(){ | |
analogWrite (bluePin, 255); | |
analogWrite (redPin, 230); | |
analogWrite (greenPin, 1); | |
tone(buzzer, NOTE_D5, 600); | |
} | |
//reset LED to off | |
void reset(){ | |
analogWrite (redPin, 0); | |
analogWrite (greenPin, 0); | |
analogWrite (bluePin, 0); | |
} | |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.