SparkFun Forums 

Where electronics enthusiasts find answers.

General project discussion / help
Did you make a robotic coffee pot which implements HTCPCP and decafs unauthorized users? Show it off here!
By Geeva
#195201
1: Now, I am using Raspberry Pi with Ultrasonic HCSR 04 Sensor. I have written a program for that. I got a proper result, but I am facing some reliability issue with that. So I thought I will use other sensors.

Statement 2: I bought Maxbotics sensor MB 1040, but I don't know how to program. I have tried with the same ultrasonic program, I got readings but that is not proper reading, I have not achieved accuracy. I have written a program in C++. So kindly help me to solve this issue.

Here I have added my sample code, what I have tried. I hope here will get a solution.
Code: Select all
#define TRIGGER_SENSOR 7      // GPIO_7
#define ECHO_SENSOR 15          // GPIO_8

// Distance variable
uint32_t Start_Sensor=0,Stop_Sensor=0;
uint32_t Time_Diff_Sensor=0;
float Range_Sensor=0;
volatile int Flag_Sensor=0;

    pinMode(TRIGGER_SENSOR,OUTPUT);             // Pins assignment
    pinMode(ECHO_SENSOR,INPUT);
    pullUpDnControl(ECHO_SENSOR,PUD_DOWN);

float Sensor::SensorDistance() // Sonar function 
{
            Start_Sensor=0;
            Stop_Sensor= 0;
            Flag_Sensor= 0;
            digitalWrite(TRIGGER_SENSOR,0);
            delayMicroseconds(1);
            digitalWrite(TRIGGER_SENSOR,1);
            delayMicroseconds(10);
            digitalWrite(TRIGGER_SENSOR,0);
            return Range_Sensor;
}

void myInterruptSensor(void) // Interrupt routine for Sensor distance
 {                     // Called whenever the input pin toggles
    uint32_t timeTempSensorultra=micros();
    if(Flag_Sensor==0) // first toggle? note the time
      {
            Start_Sensor=timeTempSensorultra;
            Flag_Sensor=1;
      }
    else // second toggle? compute the distance
      {
            Stop_Sensor=timeTempSensorultra;
            Flag_Sensor=0;
            Time_Diff_Sensor=Stop_Sensor-Start_Sensor;
            Range_Sensor=Time_Diff_Sensor/58.;
       }
  }

bool Sensor::Interrupt_error() 
{
    if(wiringPiISR(ECHO_SENSOR,INT_EDGE_BOTH,&myInterruptSensor) < 0)
        {
            cerr<<"interrupt error ["<<strerror (errno)<< "]:"<<errno<<endl;
            return false;
        }
        return true;
}