Paso 13: El código
Para quien no desea descargar el archivo o sólo quiere saber qué está poniendo antes de que él, aquí el código.
/******************************************<br> PURPOSE: Piranha Plant Created by Aldo Reigosa DATE: 12/2014 *******************************************/
#include // Library for the proximity sensor #include // Library for the servo #include // Library for the stepper motor
#define gearratio 64 // 1:64 gear ratio #define echoPin 5 // This is the echo pin #define triggerPin 6 // This is the trigger pin #define servopin 7 // This is the servo pin #define ledpin 12 // This is the LED pin
Servo myservo; // instantiate a Servo object named 'myservo' int x; // Integer used for 1st distance recorded int y; // Integer used for 2nd distance recorded volatile int z=0; // Integer used in the interrupt const int stepsPerRevolution = 2048; // Number of steps to complete a revolution Stepper myStepper(stepsPerRevolution,8,9,10,11); // Pins for the stepper motor
Piranha distanceSensor(echoPin,triggerPin); // The constructor to instantiate a sensor named "distanceSensor"
/***************************setup function*********************************/
void setup() { Serial.begin(115200); // Start serial communication myservo.attach(servopin); // Attaches the servo on pin 7 to the servo object myStepper.setSpeed(0.8*gearratio); // Sets the speed for the stepper motor attachInterrupt(1,Interrupt,RISING); // Attaches the interrupt to pin 3 and activates when it is rising pinMode(ledpin, OUTPUT); // Sets the LED pin as an output pin }
/***************************main loop**********************************************/
void loop() { float distance1 = distanceSensor.readSensor(); // Here we call the 'readSensor' method to determine the distance // and set it equal to distance1 if (distance1 > 0 && distance1 < 100){ // Here we set x=1 if the distance threshold is met x=1; // Otherwise x=0 } else { x=0; } Serial.print("distance1: "); // Prints the distance into the serial monitor. Serial.println(distance1);
delay(1000); // A 1 second delay
float distance2 = distanceSensor.readSensor(); // We take another distance measurement
if (distance2 > 0 && distance2 < 100 && x==1){ // If both the first and second distance are within the threshold myservo.write(180); // it will activate the servo to the 180 degree position and set y to 1 y=1; } else { // If either the first or second distance isn't within the threshold myservo.write(0); // the servo will return to the 0 position and set y to 0 y=0; } Serial.print("distance2: "); // Prints the distance into the serial monitor. Serial.println(distance2);
delay(1000); // Another 1 second delay
float distance3 = distanceSensor.readSensor(); // A final distance measurement labelled as distance3
if (distance3 > 0 && distance3 < 100 && x==1 && y==1 && z==1){ // If all three distances are within the threshold and the interrupt myStepper.step(stepsPerRevolution); // has been activated (thus removing the safety), it will activate delay(2000); // the stepper motor and delay for 2 seconds before resseting the x=0; // variables. Otherwise it will only delay for 1 second and start over. y=0; } else { delay(1000); } Serial.print("distance3: "); // Prints the distance into the serial monitor. Serial.println(distance3);
} void Interrupt(){ // This is the interrupt z=!z; // This will make the variable that change to the opposite value if (z==1){ // When z is equal to 1 it will turn on the LED pin to act as a digitalWrite(ledpin, HIGH); // cautionary warning light saying that it is ready to fire } else { digitalWrite(ledpin, LOW); } Serial.print(z); }