Thursday, July 22, 2010

Code

Here is how the arduino is currently setup:

Servo is in pin 9 and the proximity sensor in in analog pin 0.

Code:
#include <Servo.h>

#define proximityPin 0
#define maxCount 40

int proximity = 0;
 
Servo centerHub;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 

boolean state = true; //true = open, false = close
int count = 0;


 
void setup() 
{ 
  centerHub.attach(9);
 // centerHub.write(0); //position 0 is all the way open
 Serial.begin(9600);
} 
 
 
void loop() 
{ 
  proximity = analogRead(proximityPin);
  Serial.println(proximity,DEC);
  //if some one is far away
  if(proximity < 100 && state == false){
    if(count > maxCount){
       //open
       for(pos = 180; pos>=1; pos-=1){                                
          centerHub.write(pos); 
          delay(20);
        } 
        delay(5000);
       state = true;
       count = 0;
    } else {
      count++;
    }
  }
  //if some ones comes close
  else if(proximity > 300 && state == true){
    if(count > maxCount){
       //close
      for(pos = 0; pos < 188; pos += 1) {                                  
        centerHub.write(pos);           
        delay(10);                        
      } 
      delay(5000);
       state = false;
       count = 0;
    } else {
      
      count++;
    }
  }
  //if you are standing the middle
  else {
   count = 0; 
   
  }
  delay(50);

}

No comments:

Post a Comment