maandag 10 juni 2013

Uiteindelijke Code Arduino

Voor het optimaliseren van de code voor de dropservo en de sensor, heb ik de graden van de hoek moeten aanpassen, de speaker uit de code moeten halen (omdat we een nieuwe speaker hebben) en de tijden van de delays aan moeten passen.

Hieronder is de definitieve code te zien:

#include <Servo.h> //include the servo libary
const int infraredSensorPin = 4; //connect the signal pin to the digital pin 4
const int servo = 8; //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 = 1000; //time the sensor has to detact an object
const int time2 = 5000; //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(8); //dropservo is connected to pin 8
  dropServo.write(posClosed); //make sure the drop mechanism is closed
}

void loop()
{
  if(digitalRead(infraredSensorPin) == HIGH) { //look if the sensor detects anything
    delay(time1); //wait a certen time
    while(digitalRead(infraredSensorPin) == HIGH) { //look if the sensor is still detecting a object
     
      dropServo.write(posOpen); //open the drop mechanism
      delay(100); //wait for the servo to move
           
      dropServo.write(posClosed);} //close the drop mechanism
      delay(100);  //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