Need help ASAP. Code freezes before reaching setup()

Status
Not open for further replies.

dk12651

Member
So I am working on a school project that is due Friday. The code compiles fine (only gives a warning about comparing signed and unsigned integers). Code will upload just fine. But I get absolutely NOTHING in the serial monitor. I put a few random "Hello World" prints to test. When I use an Uno, I get one Hello World as I should (I know that the Uno will freeze because I cant hook up the CC1101 to it). I am not sure why it is freezing before it gets to setup() when I use the Teensy? I have tested all of the different libraries separately and know they work. So is it possible they are conflicting with each other?

Btw, the project is to make an autonomous lawnmower. :rolleyes:
 

Attachments

  • Mower_Project.ino
    21.8 KB · Views: 79
Code:
    while(!Serial.available()){
    }
If you don't type anything into the serial monitor, this will just hang right here. I think what you meant to use there is this:
Code:
    while(!Serial);

Pete
 
Code:
    while(!Serial.available()){
    }
If you don't type anything into the serial monitor, this will just hang right here. I think what you meant to use there is this:
Code:
    while(!Serial);

Pete

Or better, add a timeout so that it will continue if you are powering it without using the serial monitor:

Code:
    while (!Serial && millis () < 3000U)
        ;
 
If the above does not solve the issue. Some other things to look at, are any global objects have constructors that do anything other than just set some variables can be problematic.

For example if they try to setup hardware, the underlying hardware may not have been initialized yet and trying to access them could fail. Examples that have bit me in the past include doing something like Serial.print in something called by the object creator. In those cases I worked around it by doing something like: if (Serial) Serial.print(x);

Likewise if you try to talk directly to hardware, by reading/writing some of the registers, Access to those registers may not have been setup yet. Example if you are talking directly to lets say Seria3 object and try to set some of the Uart3 registers, you must first enable those registers: SIM_SCGC4 |= SIM_SCGC4_UART2;

Likewise if some of the objects depend on other objects being initialized in some order, that may not happen in the order needed.

I don't see anything obvious like this but I ahve not used all of the libraries you include
 
Thanks for the help. I am now getting some "hello worlds". Code now hangs at compass.init I reconfirmed that the lsm303 code works on its own.
 

Attachments

  • LSM303.cpp
    14.8 KB · Views: 185
  • LSM303.h
    8.2 KB · Views: 70
the LSM303 is using I2C, do you have pullup resistors (e.g., 2K ohm) on the SDA and SCL lines? On UNO, I2C devices can often run on internal pullups, but on teensy you need external pullups.

also sketch is doing attachInterrupt(). For teensy in setup() you need to set the pinMode of the interrupt pins to INPUT before the attachInterrupt() (not required on UNO).

minor: PreviousLEDMillis should be unsigned long

UNO runs at 5v, teensy 3* (which are you using) runs at 3.3v. If some of your devices require 5v, then be aware that only Teensy 3.1, 3.2 and 3.5 have 5v-tolerant digital pins
 
Last edited:
I see you have several Serial.print messages in the init. So where in the init is it hanging?

My gut tells me like manitou that you may have an I2C issue. Maybe PU, maybe hardware not powered right, maybe conflict.

So I would have a tendency to put some print statements in the functions: testReg

Maybe something like:
Code:
int LSM303::testReg(byte address, regAddr reg)
{
  Serial.printf("TR: %d %d: ", address, reg);
  Wire.beginTransmission(address);
  Wire.write((byte)reg);
  if (Wire.endTransmission() != 0)
  {
    Serial.println("Error write");
    return TEST_REG_ERROR;
  }
  Serial.print("* ");
  Wire.requestFrom(address, (byte)1);
  Serial.print("* ");
  if (Wire.available())
  {
    int ret = Wire.read();
    Serial.println(ret, DEC);
    return ret;
  }
  else
  {
    Serial.println("Error Read");
    return TEST_REG_ERROR;
  }
}
This is pretty verbose, but assuming not a complete crash happens, but a hang, there is more or less something printed after each stage of the Wire calls...

Also don't know which Teensy nor what version of Arduino/Teensyduino you are using, but if you are not using current stuff, I would try that...
 
How specifically did you confirm that part works? Small details matter.
Without changing anything on the hardware side, the board works perfectly and gives good readings. Using the same code and library.
the LSM303 is using I2C, do you have pullup resistors (e.g., 2K ohm) on the SDA and SCL lines? On UNO, I2C devices can often run on internal pullups, but on teensy you need external pullups.

also sketch is doing attachInterrupt(). For teensy in setup() you need to set the pinMode of the interrupt pins to INPUT before the attachInterrupt() (not required on UNO).

minor: PreviousLEDMillis should be unsigned long

UNO runs at 5v, teensy 3* (which are you using) runs at 3.3v. If some of your devices require 5v, then be aware that only Teensy 3.1, 3.2 and 3.5 have 5v-tolerant digital pins
I do have 2.2Kohm resistors to SDA and SD0. All devices used are 3.3V.
I see you have several Serial.print messages in the init. So where in the init is it hanging?

My gut tells me like manitou that you may have an I2C issue. Maybe PU, maybe hardware not powered right, maybe conflict.

So I would have a tendency to put some print statements in the functions: testReg

Maybe something like:
Code:
int LSM303::testReg(byte address, regAddr reg)
{
  Serial.printf("TR: %d %d: ", address, reg);
  Wire.beginTransmission(address);
  Wire.write((byte)reg);
  if (Wire.endTransmission() != 0)
  {
    Serial.println("Error write");
    return TEST_REG_ERROR;
  }
  Serial.print("* ");
  Wire.requestFrom(address, (byte)1);
  Serial.print("* ");
  if (Wire.available())
  {
    int ret = Wire.read();
    Serial.println(ret, DEC);
    return ret;
  }
  else
  {
    Serial.println("Error Read");
    return TEST_REG_ERROR;
  }
}
This is pretty verbose, but assuming not a complete crash happens, but a hang, there is more or less something printed after each stage of the Wire calls...

Also don't know which Teensy nor what version of Arduino/Teensyduino you are using, but if you are not using current stuff, I would try that...
Thanks for the suggestion. I will try it out as soon as I can. Im using Teensy 3.2 and Arduino 1.6.11 with Teensyduino ver 1.31.
 
Code:
bool LSM303::init(deviceType device, sa0State sa0)
{
Serial.print("Hello World3");
  // perform auto-detection unless device type and SA0 state were both specified
  if (device == device_auto || sa0 == sa0_auto)
  {
Serial.print("Hello World4");
    // check for LSM303D if device is unidentified or was specified to be this type
    if (device == device_auto || device == device_D)
    {
Serial.print("Hello World5");
      // check SA0 high address unless SA0 was specified to be low
      if (sa0 != sa0_low && testReg(D_SA0_HIGH_ADDRESS, WHO_AM_I) == D_WHO_ID)
      {
Serial.print("Hello World6");
        // device responds to address 0011101 with D ID; it's a D with SA0 high
        device = device_D;
        sa0 = sa0_high;
      }
      // check SA0 low address unless SA0 was specified to be high
      else if (sa0 != sa0_high && testReg(D_SA0_LOW_ADDRESS, WHO_AM_I) == D_WHO_ID)
      {
Serial.print("Hello World7");
        // device responds to address 0011110 with D ID; it's a D with SA0 low
        device = device_D;
        sa0 = sa0_low;
      }
    }
    Serial.print("Hello World8");
    // check for LSM303DLHC, DLM, DLH if device is still unidentified or was specified to be one of these types
    if (device == device_auto || device == device_DLHC || device == device_DLM || device == device_DLH)
    {
      // check SA0 high address unless SA0 was specified to be low
      if (sa0 != sa0_low && testReg(DLHC_DLM_DLH_ACC_SA0_HIGH_ADDRESS, CTRL_REG1_A) != TEST_REG_ERROR)
      {
        // device responds to address 0011001; it's a DLHC, DLM with SA0 high, or DLH with SA0 high
        sa0 = sa0_high;
        if (device == device_auto)
        { 
Serial.print("Hello World9");
I receive up to Hello World 5 but don't get anything after.
And thanks for all the help. I am still pretty new at this. I know my code isn't exactly pretty but I wasn't planning on posting it.:rolleyes:
Is there a page somewhere that lists all the differences between the Arduino and the Teensy? I've had instances of things being slightly different in the past and would like to see everything on a single page...
Also, updated code attached.
 

Attachments

  • Mower_Project.ino
    22 KB · Views: 54
Or maybe the problem is real simple in that Wire.begin is not called until AFTER compas.init is called...
 
Status
Not open for further replies.
Back
Top