• Pinterest - Black Circle
  • Facebook Basic Black
  • LinkedIn - Black Circle
  • Twitter Basic Black
  • Instagram Basic Black

Arduino Toy

A small toy with an arduino board inside. Its eyes is a designed distance sensor, and its nouse is a light sensor. When somebody comes to it, its tail will sway with a motor, which gets input data from the distance sensor. The light sensor will control the two red led lights on its face.

Process and Prototype
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);
}