int sirc_code(int pin, int bits)
{
  set_io_dt(CLKFREQ/1000000);
  set_io_timeout(CLKFREQ/50);

  unsigned int irPulse;                
  int irCode = 0;
  if(tLimit) t = CNT;
  do{
    irPulse = pulse_in(pin, 0); 
    if(tLimit)
    {
      if((CNT - t) >= tLimit) return -1;
    }  
  }while((irPulse <= 2000) || (irPulse >= 2800));
  
  for(int i = 0; i < bits; i++)
  {
    irPulse = pulse_in(pin, 0);
    if((irPulse > 1000) && (irPulse < 1400)) irCode |= (1 << i);
    if((irPulse < 300) || (irPulse > 1400)) return -1;
    if(tLimit)
    {
      if((CNT - t) >= tLimit) return -1;
    }  
  }
  return irCode;
}
Beispiel #2
0
static uint8_t ReadDigitalIr(uint8_t index)
{
    uint32_t pulse;
    
    set_output(DIGITAL_IR_PIN_START + index, OUTPUT);
    usleep(10);
    
    // units of pulse are microseconds
    pulse = pulse_in(DIGITAL_IR_PIN_START + index, LOW);
    
    // White surfaces reflect more light than black, so, when directed towards a white surface, 
    // the capacitor will discharge faster than it would when pointed towards a black surface.
    
    // pulses longer than 3 seconds mean nothing was found
    // shorter pulses mean no detection
    // longer pulses mean detection
    // might need some calibration to determine the threshold
    
    // Convert to milliseconds
    pulse /= 1000;
    
    if (pulse <= 500 || pulse >= 3000)
    {
        return 0;
    }
    
    return 1;
}
int UltraSonicSensor::getDistance(){
  pulse_out(pin,10);
  int velocity = 331.5 + (0.6 * temperature);
  int travelTime = pulse_in(pin,1);  
  int distance = (velocity * travelTime * 100)/(2 * 1000000);
  if(2 < distance && distance < 330){
      return distance;
  }else{
      return -1;
  }
}  
Beispiel #4
0
void ultrasonic_run()
{
  while(1)
  {
    high(PIN_RANGE_TRIG);
    waitcnt(CNT + CLKFREQ/100000); // 10 uS
    low(PIN_RANGE_TRIG);

    int duration = pulse_in(PIN_RANGE_ECHO, 1)/58;

    if (duration > 400)
      duration = 400;
    range = duration;
    waitcnt(CNT + CLKFREQ/17);
  }
}
Beispiel #5
0
static float ReadUltrasonic(uint8_t addr)
{
    // Set pins directions
    set_direction(MUX_ADDR_START, OUTPUT);
    set_direction(MUX_ADDR_START + 1, OUTPUT);
    set_direction(MUX_ADDR_START+ 2, OUTPUT);
    set_direction(MUX_ADDR_START + 3, OUTPUT);
    set_direction(TRIG_PIN, OUTPUT);
    set_direction(ECHO_PIN, INPUT);

    // Set the mux based on sensor index
    set_outputs(MUX_ADDR_END, MUX_ADDR_START, (int) addr); 

    // Pulse the trigger pin
    low(TRIG_PIN);
    pulse_out(TRIG_PIN, 10);

    // Read in echo pulse
    uint32_t pulse = pulse_in(ECHO_PIN, HIGH);

    return pulse/58.0;    
}