My work for Midterm is a toy. The concept is, “Dark Gives Me Life”.
It’s a ghost toy for Halloween. When night (dark) comes, the ghost will be alive.
There is a photocell sensor to detect the light. If the light is weak, it can give signal to Arduino. Then, Arduino will turn on the LEDs, piezo speaker and servo. So the ghost can shake, scream and blink his green evil eyes.
There are 1 photocell for detecting the light, 1 force sensor for detecting people pressing his hand. And 2 LEDs (for blinking eyes), 1 servo (for shaking head) and 1 piezo speaker (for screaming) as outputs.
code is here,
#include <Servo.h>
Servo myservo;
int speaker = 9;
int ledPin1 = 6;
int ledPin2 = 5;
void setup(){
Serial.begin(9600);
myservo.attach(3);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(speaker, OUTPUT);
}
void loop(){
noTone;
int sensorValue = analogRead(A0);
int sensorValue2 = analogRead(A1);
// Serial.println(sensorValue);
// Serial.println(sensorValue2);
delay(10);
//turn off the light
if(sensorValue < 50){
// myservo.write(180);
int thisPitch = map(sensorValue, 0, 100, 1000, 1500);
tone(speaker, thisPitch, 800);
for(int fadeValue = 0; fadeValue <= 255; fadeValue +=1){
int i = 0;
myservo.write(i++);
analogWrite(ledPin1, fadeValue);
// delay(3);
}
for(int fadeValue = 255; fadeValue >= 0; fadeValue -=1){
int i = 90;
myservo.write(i–);
analogWrite(ledPin1, fadeValue);
// delay(3);
}
}
//press
if(sensorValue2 > 200){
int powerValue = map(sensorValue2, 400, 900, 1000, 2000);
tone(speaker, powerValue, 800);
}
}