Paso 1: Código de Arduino:
/****************************************** PURPOSE: Project FrankensteinCreated by Howard KaplanDATE: 4.22.2015This sketch used the HCSR04.h class and HCSR04.cpp to get the sensor input and stores the current and previous distances using the Serial Monitor - the distance of the detected object is displayed and determined as approaching or retracting based on the current and previous distance. Additional components such as LEDs and Servos are used and their respective pins are turned on and off depending on the range sensor values. The sketch also uses the Servo and Pitches libraries.*******************************************/// Libs#include "HCSR04.h"//include calls the header / class file#include // calls the servo lib files////MUSIC#include "pitches.h"////Range Sensor#define echoPin 13 // This is the echo pin for range sensor#define triggerPin 12 // This is the trigger pin for range sensorHCSR04ProxSensor distanceSensor(echoPin,triggerPin);//here we call the constructor to instantiate a sensor named "distanceSensor"///Music Arrays int melody[] = { NOTE_A6,NOTE_C6,NOTE_E6,//3 NOTE_E6,NOTE_A6,//2 NOTE_C6,NOTE_E6,//2 NOTE_E6,NOTE_A6,//2 NOTE_C6, NOTE_E6,//2 NOTE_E6,NOTE_A6,NOTE_C6,//3 NOTE_E6,NOTE_G6,NOTE_B6,//3 NOTE_D6,//1 0, NOTE_E6,NOTE_D6,NOTE_C6,NOTE_B6,//4 NOTE_A5, NOTE_A5//1/23 };// note durations: 4 = quarter note, 8 = eighth note, etc.:int noteDurations[] = { 4,4,4, 8,4, 4,8, 4,8, 4,8, 4,4,4, 4,4,4, 1, 2, 8,8,8,8, 4,1, }; //// //FAN int fan1 = 4;//Fan on pin 4 //LEDS 2 Pins - 4 Leds int ledReds = 11;//2 red int ledGreens = 10;//2 green //Servos 3 Servo servoEye, servoM; ///servoEye - has 2 servos to 1 pin (right and left eye) int posEye1 = 140; // variable to store the servo position // int musicPin = 9; int pauseBetweenNotes; float distance; //Distance to activate sensor / loop change/***************************setup function****************************************************/void setup() { Serial.begin(9600);//start serial communication //FAN pinMode(fan1,OUTPUT); //digitalWrite(fan1,LOW); //SERVOs servoEye.attach(5); //eye servoM.attach(3); //mouth //set eye start position servoEye.write(140); //set mouth servoM.write(60); //LEDs pinMode(ledReds, OUTPUT); pinMode(ledGreens, OUTPUT); } /***************************main loop*********************************************************/void loop() {/******************SENSOR***************************************/ Serial.print("The distance is : "); distance = distanceSensor.readSensor();//here we call the 'readSensor' method to determine the distance Serial.print(distance);// send the measurement to the serial monitor Serial.println(" cm"); if (distanceSensor.getLastValue() - distance > 1) { Serial.println("object is approaching"); }//here we call the 'getLastValue' method to determine the direction of motion if (distanceSensor.getLastValue() - distance < -1) { Serial.println("object is retracting"); }/***************Main Condition Statement******************************************/ ////FAN on / off if(distance >= 40.00) { digitalWrite(fan1,LOW); //Leds - Green ON Reds OFF digitalWrite(ledGreens, HIGH); digitalWrite(ledReds, LOW); ///Servo Eyes Look servoEyesBacFor(); ///Servo Mouth Close servoM.write(50); }else{ digitalWrite(fan1,HIGH); //Leds - Green OFF Reds ON digitalWrite(ledGreens, LOW); digitalWrite(ledReds, HIGH); //Servo Stop Straight servoEye.write(140); ///Servo Mouth Open servoM.write(80); //Puttin' on the Ritz playMusic(); } }/*****************EYEs FUNCTION****************************************/ void servoEyesBacFor(){ for(posEye1 = 90; posEye1 <= 180; posEye1 += 1) // goes from 0 degrees to 180 degrees { if(distanceSensor.readSensor() <= 40.00){ break; } // in steps of 1 degree servoEye.write(posEye1); // tell servo to go to position in variable 'pos' delay(5); } for(posEye1 = 180; posEye1 >= 90; posEye1-=1) // goes from 180 degrees to 0 degrees { if(distanceSensor.readSensor() <= 40.00){ break; } servoEye.write(posEye1); // tell servo to go to position in variable 'pos' delay(5); } } /***************************************/ void playMusic(){ for (int thisNote = 0; thisNote < 24; thisNote++) { ///Check distance and break out of loop if out of range if(distanceSensor.readSensor() >= 40.00){ thisNote = 0; break; } int noteDuration = 1000/noteDurations[thisNote]; tone(musicPin, melody[thisNote],noteDuration); pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); noTone(musicPin); } } /***********************The End!*************************/