Advertisement
Mihalytch

IR Sensor arduino. For Vetal

Mar 12th, 2014
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. // Simple Proximity Sensor using Infrared
  2.  
  3.  int IRpin = A5;               // IR photodiode on analog pin A0
  4.  int IRemitter = 5;            // IR emitter LED on digital pin 2
  5.  int ambientIR;                // variable to store the IR coming from the ambient
  6.  int obstacleIR;               // variable to store the IR coming from the object
  7.  int value[10];                // variable to store the IR values
  8.  int distance;                 // variable that will tell if there is an obstacle or not
  9.  int prevDistance = -1;
  10.  
  11.  int brightness = 0;
  12.  
  13.  const int measurementsCount = 2;
  14.  int brightnessArr[measurementsCount];
  15.  int currentPoint = 0;
  16.  
  17.  
  18.  void setup() {
  19.    Serial.begin(9600);         // initializing Serial monitor
  20.    pinMode(IRemitter,OUTPUT);  // IR emitter LED on digital pin 2
  21.    digitalWrite(IRemitter,LOW);// setup IR LED as off
  22.    /*
  23.    for (int i = 0; i < measurementsCount; i++) {
  24.      brightnessArr[i] = 0;
  25.    }*/
  26.  }
  27.  
  28.  void loop() {
  29.    distance = readIR(5);       // calling the function that will read the distance and passing the "accuracy" to it
  30.    
  31.    if (distance>=0 && distance!=prevDistance) {
  32.      brightness = map(distance, 0, 150, 0, 150);
  33.    
  34.      analogWrite(10, brightness);
  35.      prevDistance = distance;
  36.    }
  37.    /*
  38.    if (prevDistance != distance) {
  39. //     Serial.println(distance);  // writing the read value on Serial monitor
  40.      prevDistance = distance;
  41.      
  42.      brightness = map(distance, 0, 80, 0, 150);
  43.      
  44.      brightnessArr[currentPoint] = brightness;
  45.      currentPoint = (currentPoint + 1) % measurementsCount;
  46.      int val = delta();
  47.      Serial.println(val);
  48.      if (val > 30) {
  49.        analogWrite(10, 254);
  50.      } else {
  51.        analogWrite(10, 30);
  52.      }
  53.    }
  54.    */
  55.    delay(10);
  56.  }
  57.  
  58.  int delta() {
  59.    int value = 0;
  60.    for (int i = 1; i < measurementsCount; i++) {
  61.      value += abs(brightnessArr[i-1] - brightnessArr[i]);
  62.    }
  63.    return value;
  64.  }
  65.  
  66.  int readIR(int times) {
  67.    int x, r;
  68.    
  69.    for(x=0; x<times; x++) {
  70.      digitalWrite(IRemitter,LOW);    // turning the IR LEDs off to read the IR coming from the ambient
  71.      delay(2);                       // minimum delay necessary to read values
  72.      ambientIR = analogRead(IRpin);  // storing IR coming from the ambient
  73.      
  74.      digitalWrite(IRemitter,HIGH);   // turning the IR LEDs on to read the IR coming from the obstacle
  75.      delay(2);                       // minimum delay necessary to read values
  76.      obstacleIR = analogRead(IRpin); // storing IR coming from the obstacle
  77.      
  78.      value[x] = ambientIR-obstacleIR;
  79.    }
  80.  
  81.    for(int x=0;x<times;x++){         // calculating the average based on the "accuracy"
  82.      distance+=value[x];
  83.    }
  84.    
  85.    return(distance/times);           // return the final value
  86.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement