Advertisement
talofer99

Untitled

Apr 17th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. #define NUMBER_OF_SENSORS 7 // Number of ultrasonic sensors
  2. #define MAX_SENSOR_MEASUREMENT 35 // max distance for read for the sensor (in cm)
  3.  
  4.  
  5. // for ultrasonic
  6. int sensorsPins[NUMBER_OF_SENSORS] = {2, 11, 12, A0, A1, A2, A3}; //define the pins for the sensors
  7. const int TRIG_PIN = 13; // TRIGGER pin
  8. volatile int currentSensor = 0; // current sensor been red
  9. boolean sensorState[NUMBER_OF_SENSORS];//holds the sensor state to create a "snap shot" of the state of all the sensors after full loop over them - so we can choose the higher one.
  10. long duration, distanceCm; // declare var for sensor read
  11.  
  12.  
  13. // VOID SETUP
  14. void setup() {
  15.  
  16.   // setup for ultrasonic
  17.   pinMode(TRIG_PIN, OUTPUT);
  18.   for (int i = 0; i < NUMBER_OF_SENSORS; i++) {
  19.     pinMode(sensorsPins[i], INPUT);
  20.   } //end for
  21.  
  22.  
  23.  
  24. } // end void setup
  25.  
  26.  
  27. void loop() {
  28.  
  29.   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  30.   digitalWrite(TRIG_PIN, LOW);
  31.   delayMicroseconds(2);
  32.   digitalWrite(TRIG_PIN, HIGH);
  33.   delayMicroseconds(10);
  34.   digitalWrite(TRIG_PIN, LOW);
  35.   duration = pulseIn(sensorsPins[currentSensor], HIGH, 4000);
  36.  
  37.   // convert the time into a distance
  38.   distanceCm = duration / 29.1 / 2 ;
  39.  
  40.   // if the distnace is between 40 and more then 0 (also the vlaue that OUT OF RANGE returns) show true - as sensor is showing movment.
  41.   if (distanceCm < MAX_SENSOR_MEASUREMENT && distanceCm > 0) {
  42.     sensorState[currentSensor] = true; // set true
  43.   } else {
  44.     sensorState[currentSensor] = false; // set false
  45.   }  //end if
  46.  
  47.  
  48.   // move to next sensor
  49.   currentSensor = (currentSensor + 1) % NUMBER_OF_SENSORS;
  50.  
  51.  
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement