Paso 3: Escribir el código de Arduino
Cuando tengas todo conectado hasta el siguiente paso es conectar el Arduino al ordenador y comenzar a escribir código para él. Puede descargar el código o copiar y pegar desde abajo, pero antes de que se puede ejecutar, debe descargar la librería de NewPing Arduino y descomprimirlo en la carpeta de las bibliotecas de Arduino.
/*BEGIN ARDUINO CODE*/
/*IMPORT NECESSARY LIBRARIES*/<br>#include //Import the "NewPing" library for the Ultrasonic Sensors #include //Import the server library
/*DECLARE ALL VARIABLES*/
int slide = 0; //Slide detector variable
boolean left=false; //Left true/false variable boolean center=false; //Center true/false variable boolean right=true; //Right true/false varialbe
#define leftTrig 8 //left sensor output Arduino pin #define leftEcho 9 //left sensor input Arduino pin #define centerTrig 10 //center sensor output Arduino pin #define centerEcho 11 // center sensor input Arduino pin #define rightTrig 12 //right sensor output Arduino pin #define rightEcho 13 //right sensor input Arduino pin
Servo servoRotate; //Servo that will rotate the lamp Servo servoUpDown; //Servo that will move the lamp up/down
int servoRotatePin = 4; //Rotational servo Arduino pin int servoUpDownPin = 5;//Vertical servo Arduino pin
const int maxD = 20; //cm; maximum distance
long int lastTouch = -1; //ms int resetAfter = 2000; //ms int afterSlideDelay = 500; //ms; all slides ignored after successful slide int afterSlideOppositeDelay = 1500; //left slides ignored after successful right slide
int SLIDELEFT_BEGIN = -1; //Motion was detected from right int SLIDELEFT_TO_CENTER = -2; //motion was detected from right to center
int SLIDENONE = 0; //No motion was detected
int SLIDERIGHT_BEGIN = 1; // motion was detected from left int SLIDERIGHT_T0_CENTER = 2; // motion was detected from left to center
int centerDistance;
/*SETUP SERIAL MONITOR AND SERVOS*/ void setup() { Serial.begin(9600); servoRotate.attach(servoRotatePin, 0, 180); servoUpDown.attach(servoUpDownPin, 0, 180); servoRotate.write(0); }
/*PING ULTRASONIC SENSORS, CALCULATE SLIDE STATUS*/ void loop() { left = ping(leftTrig, leftEcho); //send ping to left sensor center = ping(centerTrig, centerEcho); //send ping to center sensor right = ping(rightTrig, rightEcho); //send ping to right censor centerDistance = getDistance(centerTrig, centerEcho); //get object distance from center sensor //If there is anything detected by the snensors, store the time to "last touch" if (left || center || right){ lastTouch=millis(); } //If the time last sensed is greater than the reset time, reset the slide variable if (millis()-lastTouch>resetAfter) { slide = 0; Serial.println("Reset slide and timer. "); } //Detect a slide to the right if (slide >= SLIDENONE) { if ( (left) && (!right) ) slide = SLIDERIGHT_BEGIN; if (center && (slide == SLIDERIGHT_BEGIN)) slide = SLIDERIGHT_T0_CENTER; if (right && (slide == SLIDERIGHT_T0_CENTER)){ slideNow('R'); } } //Detect a slide to the left if (slide <= SLIDENONE) { if ( right && (!left) ) slide = SLIDELEFT_BEGIN; if (center && slide == SLIDELEFT_BEGIN) slide = SLIDELEFT_TO_CENTER; if (left && slide == SLIDELEFT_TO_CENTER){ slideNow('L'); } } //detect center distance if(slide){ if(center && (!left) && (!right)){ if (centerDistance){ verticalMove(centerDistance); } } } delay(50); //ms }
/*FUNCTION TO SEND OUT A PING*/ boolean ping(int trigPin, int echoPin) //, int ledPin) { int d = getDistance(trigPin, echoPin); //cm boolean pinActivated = false; if (d < maxD){ pinActivated = true; } else { pinActivated = false; } return pinActivated; }
/*FUNCTION TO GET AND CACLULATE DISTANCE*/ int getDistance(int trigPin, int echoPin) { long duration, inches, cm; pinMode(trigPin, OUTPUT); digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH); //inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); //return(inches); return(cm); }
/*CONVERT MICROSECONDS TO INCHES long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; } *
*CONVERT MICROSECONDS TO CENTIMETERS*/ long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; }
/*SLIDE LEFT AND SLIDE RIGHT MOVEMENT FUNCTIONS*/ void slideNow(char direction){ if ('R' == direction){ servoRotation('R'); Serial.println("Rotate Right"); } if ('L' == direction){ servoRotation('L'); Serial.println("rotate Left"); } delay(afterSlideDelay); slide = SLIDENONE; }
/*UP AND DOWN MOVEMENT FUNCTIONS*/ int verticalMove(int originalDistance){ if (originalDistance > 0 && originalDistance <= 10){ servoVertical('D'); Serial.print("Move Down: "); Serial.println(originalDistance); } else if (originalDistance <= 30){ servoVertical('U'); Serial.print("Move Up: "); Serial.println(originalDistance); } delay(afterSlideDelay); slide = SLIDENONE; }
/*SERVO ROTATION FUNCTION*/ void servoRotation(char whichway){ int currentPos = servoRotate.read(); if ('R' == whichway){ servoRotate.write(currentPos + 15); }else if('L' == whichway){ servoRotate.write(currentPos - 15); } }
/*SERVO UP/DOWN FUNCTION*/ void servoVertical(char upOrDown){ int currentPos = servoUpDown.read(); if ('U' == upOrDown){ servoUpDown.write(currentPos - 25); }else if('D' == upOrDown){ servoUpDown.write(currentPos + 25); } }