dinsdag 4 juni 2013

Code Arduino Droppen en Detecteren

De Code beschijft dat wanneer de boei via de sensor gedetecteerd wordt de servo wordt aangestuurd om 30 graden te draaien. Als het signaal weg is, dus wanneer er niets wordt gedeteteerd draai die weer 30 graden terug naar de startpositie.
 
 
#include <Servo.h> //include the servo libary
 
const int infraredSensorPin = 4; //connect the signal pin to the digital pin 4
const int speaker = 6; //connect the speaker to pin 6
 
Servo sweepServo;  //create servo object to control a servo
Servo dropServo;  //create servo object to control a servo              
 
const int time1 = 100; //time the sensor has to detact an object
const int time2 = 500; //time set to make shure the same object will not be detected twice
 
int posOpen = 30; //amount of dergees the servo has to travel to for a drop
int posClosed = 130; //amount of degrees the servo has to travel to to close
 
void setup()
{
  Serial.begin(57600); //set up a serial connection
  Serial.println("Start"); //print this tekst
 
  pinMode(infraredSensorPin,INPUT); //infraredSensorPin is an input
 
  dropServo.attach(12); //dropservo is connected to pin 12
  dropServo.write(posClosed); //make sure the drop mechanism is closed
}
 
void loop()
{
  if(digitalRead(infraredSensorPin) == LOW) { //look if the sensor detects anything
    delay(time1); //wait a certen time
    while(digitalRead(infraredSensorPin) == LOW) { //look if the sensor is still detecting a object
     
      tone(speaker, 440, 200); //play a tone
      dropServo.write(posOpen); //open the drop mechanism
      delay(500); //wait for the servo to move
      noTone(speaker); //stop the speaker
     
      dropServo.write(posClosed);} //close the drop mechanism
      delay(500);  //wait for the servo to move
      delay(time2); //wait so the same object won't be detected twice
  }
 
  Serial.print("Infrared Sensor Switch Status:"); //print this tekst
  Serial.println(digitalRead(infraredSensorPin),BIN); //read and print the status of the infrared sensor
}

Geen opmerkingen:

Een reactie posten