daily pastebin goal
11%
SHARE
TWEET

Sharp sensor on arduino darylrobotproject.wordpress.com

a guest Dec 10th, 2012 654 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define sensorIRleft A1 //Must be an analog pin
  2.  
  3. float sensorValue;
  4. int cm;
  5. int led = 9;
  6. int brightness = 0;
  7.  
  8. void setup() {
  9.   Serial.begin(9600);
  10.   pinMode(led, OUTPUT);
  11. }
  12.  
  13. void loop() {
  14.  
  15.   sensorValue = analogRead(sensorIRleft);
  16.   cm = 10650.08 * pow(sensorValue,-0.935) - 10;
  17.  
  18.   if (cm>200||cm==0) {
  19.     cm = 200;
  20.   }
  21.  
  22.   delay(100);
  23.   Serial.print("CM: ");
  24.   Serial.println(cm);
  25.   brightness = map(cm, 0, 200, 0, 255);
  26.   analogWrite(led, brightness);
  27.   delay(10);
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top