Thursday, 23 February 2017

Arduino ultrasonic sensor code supporting two sensors simultaneously

 #include <NewPing.h>  
 #include <Servo.h>   
 #define SONAR_NUM   2 // Number or sensors.  
 #define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.  
 #define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).  
 #define STATUS 2  
 #define signal 3  
 Servo myservo;  
 unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.  
 unsigned int cm[SONAR_NUM];     // Where the ping distances are stored.  
 uint8_t currentSensor = 0;     // Keeps track of which sensor is active.  
 int pos = 0;  
 int systemStatus=0;  
 int lock=0;  
 NewPing sonar[SONAR_NUM] = {   // Sensor object array.  
  NewPing(12, 11, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.  
  NewPing(10, 9, MAX_DISTANCE)  
 };  
 void setup() {  
   myservo.attach(5);  
   pinMode(STATUS,INPUT);  
  Serial.begin(115200);  
  pingTimer[0] = millis() + 75;      // First ping starts at 75ms, gives time for the Arduino to chill before starting.  
  for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.  
   pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;  
 }  
 void loop() {  
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.  
   if (millis() >= pingTimer[i]) {     // Is it this sensor's time to ping?  
    pingTimer[i] += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged.  
    if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.  
    sonar[currentSensor].timer_stop();     // Make sure previous timer is canceled before starting a new ping (insurance).  
    currentSensor = i;             // Sensor being accessed.  
    cm[currentSensor] = 0;           // Make distance zero in case there's no ping echo for this sensor.  
    sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).  
   //myservo.write(30);  
  }  
  }  
  // The rest of your code would go here.  
 }  
 void echoCheck() { // If ping received, set the sensor distance to array.  
  if (sonar[currentSensor].check_timer())//myservo.write(30);  
   cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;  
 }  
 void oneSensorCycle()   
 { // Sensor ping cycle complete, do something with the results.  
  for (uint8_t i = 0; i < SONAR_NUM; i++)   
  {  
         Serial.print(i);  
         Serial.print("=");  
         Serial.print(cm[i]);  
         Serial.print("cm ");   
         systemStatus=digitalRead(STATUS);  
         lock=digitalRead(signal);  
         if(lock==0)  
         {  
            if((systemStatus==1)&&(cm[1]<=92)||(cm[0]>=111)) //back sensor  
            { myservo.attach(5);  
             myservo.write(300);       // tell servo to go to position in variable 'pos'   
             //delay(10);  
            }  
            if((systemStatus==1)&&(cm[1]>92)||(cm[0]<111))  
            {  
             myservo.detach();       // tell servo to go to position in variable 'pos'   
             // delay(10);  
            }  
            if((systemStatus==0)&&(cm[1]>=63)) //front sensor  
            { myservo.attach(5);  
             myservo.write(30);       // tell servo to go to position in variable 'pos'   
             // delay(10);  
            }  
            if((systemStatus==0)&&(cm[1]<63))  
            {  
             myservo.detach();       // tell servo to go to position in variable 'pos'   
             //delay(10);  
            }  
         }  
         else  
         {  
            myservo.detach();  
         }  
  }  
   Serial.println();  
 }   

1 comment:

  1. Very significant Information for us, I have think the representation of this Information is actually superb one. This is my first visit to your site. Hoarding Cleanup Boothwyn

    ReplyDelete