Midterm–Qianjing_Liu

 

 

I made a doll with arduino for midterm. There is a light sensor and a distance sensor in this doll. The eyes of the doll is the distance sensor while the nose of it is the light sensor.  The brightness of the two leds on the doll’s face will change with the light sensor. If the room is dark, the doll will get a red face. I also put a buzzer in the doll’s body, so if the distance sensor get that you are in a short distance, the buzzer will make sound. There is a servo in its tail, so if you are put something near the doll, its tail will move.

8  9 7 6 2  4 1  Photo on 10-12-14 at 18.36

 

 

 

Here is the code:

/*
Multiple tone player

Plays multiple tones on multiple pins in sequence

circuit:
* 3 8-ohm speaker on digital pins 6, 7, and 11

created 8 March 2010
by Tom Igoe
based on a snippet from Greg Borenstein

This example code is in the public domain.

http://arduino.cc/en/Tutorial/Tone4

*/
#include <Servo.h>

Servo myservo;

int pos = 0;
int lightsensor = A0;
const int echopin=2;
const int trigpin=3;
int led = 5;

void setup() {
Serial.begin(9600);
myservo.attach(9);
pinMode(lightsensor,INPUT);
pinMode(echopin,INPUT);
pinMode(trigpin,OUTPUT);
pinMode(led,OUTPUT);
}

void loop() {
int light = analogRead(lightsensor);
digitalWrite(trigpin,LOW);
delayMicroseconds(2);
digitalWrite(trigpin,HIGH);
delayMicroseconds(10);
digitalWrite(trigpin,LOW);
float distance = pulseIn(echopin,HIGH);
distance = distance/58.0;
delay(100);

if(distance<=15){
int sound = map(distance,0,15,1000,100);
for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);
tone(4, sound, 200);
delay(5);
}
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
tone(4, sound, 200);
delay(5);
}
}
int brightness = map(light,0,170,255,0);
if(brightness<0){
brightness=0;
}
analogWrite(led,brightness);
Serial.println(light);
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *