I have a teensy that controls a motor and reads from a ir sensor, GP2Y0E02b. The issue i run into is that at random times the teensy seems to freeze, the main code stops printing but the interrupt routine continue to work. It seems to be that when reading the ir sensor it causes the teensy to freeze. The code below is what I'm using to read the ir values.

#include <i2c_t3.h>
//#include <WSWire.h>
#include <Arduino.h>
uint16_t ReadFirstSensor();
uint16_t ReadSecondSensor();

#define ADDRESS       0x80 >> 1  // Arduino uses 7 bit addressing so we shift 
address right one bit
#define DISTANCE_REG  0x5E
#define SHIFT         0x35 

class GP2Y0E02B {
    int MaxDistance;
   int MinDistance = 27;
float distance = 0;              
byte high, low = 0;              
float shift = 0; 
bool badSensor = false;

  //Serial.println("Sensors Initialized");

float GetRawDistance()
    //if (!badSensor){
      float FirstDistance = ReadFirstSensor(); 
      return FirstDistance; 
    //  return 22000;

void StartSensors()
    //Serial.println("StartSensors Called");
    //Serial.println("Wire.begin() has been called");
    //Serial.println ("beginTrans done");
    //Serial.println ("Shiftwrite done");
    int stat = Wire.endTransmission();
    //Serial.print("endTrans done stat: ");Serial.println(stat);

    int i = 0; 
    while (Wire.available() == 0)
        //Serial.println ("Im trying SPI");
        if (i>30) break;
    if (i<30)
        shift = Wire.read();
        Wire.requestFrom(ADDRESS, 1);
        MaxDistance = GetRawDistance();
        badSensor = false;
        //Serial.println ("couldnt initialize the sensors");
        badSensor = true;

 uint16_t ReadFirstSensor() 
    static int LastDist = 0;
    //Serial.println ("Read First sensor called");
    //Serial.println ("DoneWireWrite");
    int SPIhealth = Wire.endTransmission();
    //Serial.print ("DoneWireEndTransmit: ");Serial.println (SPIhealth);
    if (SPIhealth == 0) Wire.requestFrom(ADDRESS, 2);
    else return LastDist;
    //Serial.println ("DoneWireREquest");

    int EmergBreak = 0;
    while(Wire.available() < 2)
        //Serial.println ("Trying to get sensor reading");
        if (EmergBreak > 20) return LastDist;
    if (EmergBreak > 20);//Serial.println ("Couldnt Read Sensor");
    //else Serial.println ("Done Reading Sensor");

    high = Wire.read();
    low = Wire.read();

    uint16_t rawDistance; 
    rawDistance = (high * 16 + low)/16/(float)pow(2,shift); // Calculate the range in CM 
    //Serial.print("rawDistance: ");Serial.println(rawDistance);
    rawDistance = (uint16_t) low ;
    rawDistance += (uint16_t)high << 8;
    LastDist = rawDistance;

    return rawDistance;

  • 1
    I2C code would typically get "stuck" if the peripheral fails to respond and you don't have a reasonable timeout configured. You should probably figure out how to configure a timeout for the I2C operation. And maybe see if you can debug the conditions under which it occurs. – Chris Stratton Dec 4 '18 at 21:58
  • ill try that out – Jasdeep Dhillon Dec 4 '18 at 22:00
  • 1
    I don't know where to start. The StartSensors function calls Wire.available without a Wire.requestfrom and it will never get to the section with Wire.requestFrom and the variable shift will stay 0. That unused section does a Wire.read first which might return -1 and puts that into the float shift, and after that the Wire.requestFrom is called but the requested data is never read. The function ReadFirstSensor has a while statement without meaning and a calculation for rawDistance has bytes, integers, and float. The I2C bus is not a SPI bus, but there are variables with SPI in their name. – Jot Dec 4 '18 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy