Created
October 9, 2014 01:38
-
-
Save tatianajennings/1e3b5b96031dae2d305f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Sweep | |
by BARRAGAN <http://barraganstudio.com> | |
This example code is in the public domain. | |
modified 8 Nov 2013 | |
by Scott Fitzgerald | |
http://arduino.cc/en/Tutorial/Sweep* | |
/*Calibration | |
//created 29 Oct 2008 | |
//By David A Mellis | |
//modified 30 Aug 2011// | |
// By Tom Igoe// | |
//http://arduino.cc/en/Tutorial/Calibration*/ | |
//This example code is in the public domain. */ | |
// sensor reading code + values are from luckylarry.co.uk | |
#include <Servo.h> | |
Servo myservo; // create servo object to control a servo | |
// twelve servo objects can be created on most boards | |
const int ledPin = 9; // pin that the LED is attached to | |
const int servoPin = 10; // pin that the LED is attached to | |
const int actionzone = 100; // we start moving the spider when closer than this | |
const int closest = 24; // this is the minimum distance the prox. sensor can report | |
const float volts = 3.3; // voltage we are powering prox. sensor with - if running 3.3.volts then change 5 to 3.3 | |
int pos = 0; // variable to store the servo position | |
float distance = 0; // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk | |
int action = 0; // this variable controls the servo swing, based on proximity | |
int ledintensity = 255; // initiql LED brightness | |
void setup() | |
{ | |
myservo.attach(servoPin); // attaches the servo on pin X to the servo object | |
Serial.begin(9600); | |
} | |
void loop() { | |
// sensor reading code + values are from luckylarry.co.uk | |
volts = analogRead(A0)*0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3 | |
distance = 65*pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk | |
Serial.println(distance); // this is just to find out min/max proximity values | |
if (distance < actionzone) { | |
ledintensity = 255; // lets keep full brightness on the led | |
analogWrite(ledPin, ledintensity); // turn LED on! | |
myservo.write(0); // tell servo to go to a starting position | |
delay(map(distance, closest, actionzone, 200, 20)); // wait a little, depending on proximity | |
action = map(distance, closest, actionzone, 180, 0); // calculate how much to move the servo | |
for(pos = 0; pos <= action; pos += 1) { // loop to move the servo, in steps of 1 degree | |
myservo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(1); | |
} | |
} else analogWrite(ledPin, 0); // if we are too far, turn LED off | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment