-A +A

 

Adding Sensors

Sharp Infra Red Sensor

BIG TEXT | SMALL TEXT

Download ir_sensor01.txt file

// Reading Sharp IR Sensor

// Assumptions: Sensor voltage numbers back range
// from 0 (nothing in front) to ~ 680 (~ 3 inches)

// Variables
int sensorPin = 5;

void setup() {
  Serial.begin(9600); 
}

void loop() {
  Serial.println(analogRead(sensorPin));
}
				

MaxSonar Ultrasonic Sensor

BIG TEXT | SMALL TEXT

Download us_sensor01.txt file

// Reading MaxSonar Ultrasonic Rangefinder Sensor

// Assumptions: Sensor voltage numbers back range
// from 0 (nothing in front) to ~ 300 (~ 3 inches)

int sensorPin = 4;

void setup() {
  Serial.begin(9600); 
}

void loop() {
  Serial.println(analogRead(sensorPin));
}
				

Control­ling the Servo with Sharp IR Sensor

BIG TEXT | SMALL TEXT

Download servo_irsensor01.txt file

#include <Servo.h>  // include servo library
// Controlling servo with Sharp IR Sensor

// Create servo object
Servo myServo; 

// Variables
// Variable to hold sensor's value
int servoValue = 0;
// Variables to hold servo's angles
int currentAngle = 0;
int lastAngle = 0;
// Pin variables
int sensorPin = 5;
int servoPin = 10;
// Timer variables
long timer = 0;
int offset = 10;

// Setup
void setup() {
  Serial.begin(9600); 
  pinMode(servoPin,OUTPUT);
  myServo.attach(servoPin);
  myServo.write(servoValue);
  timer = millis();
}

// Das loop...
void loop() {
  // Assumption: Sensor voltage numbers range
  // from 0 (nothing in front) to ~ 680 (~ 3 inches)
  servoValue = analogRead(sensorPin)/3;  
  Serial.println(servoValue);
  if(timer + offset < millis()) {
    if(lastAngle > servoValue) { 
      currentAngle = currentAngle - 1;
      myServo.write(currentAngle);
      lastAngle = currentAngle;
      timer = millis();
    }
    if(lastAngle < servoValue) {
      currentAngle = currentAngle + 1;
      myServo.write(currentAngle);
      lastAngle = currentAngle;
      timer = millis();
    }
  }
}
				
				
finB+