Teensy 3.0 crashes with lsm303 code integration

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Hatchmanpro
 
Posts: 12
Joined: Thu Sep 26, 2013 4:08 am

Teensy 3.0 crashes with lsm303 code integration

Post by Hatchmanpro »

Hello there! I am working on a fairly involved project, building a music instrument of sorts with some custom hardware. Most of the components in the project though, are boards from Adafruit. Specifically, I am using:

Teensy 3.0
flora lsm303 accelerometer/magnetometer
lipo battery & charger
128 x 32 OLED display - spi
button
led
my custom electronics, feeding into both analogs 0 and 1 between 0-3.3v
midi output via the third serial channel.

The problem is that when I use the lsm.read(); function, (in my case accel.read) it causes otherwise perfectly functioning code to crash the teensy. It stops responding entirely, and the display freezes its current state.

If I run the test code for reading the accelerometer by itself, without using other sensors, It runs fine, and if I do not read from the accelerometer in my main chunk of code it also runs fine. I first assumed I am running out of ram, but it seems to happen the first clock cycle, as the display does not refresh past the first loop. Perhaps it is that I am using 2 channels of serial, spi and i2c communications at the same time?

Anyhow, I am stumped for the night, maybe someone here can help?

Code: Select all

#include <Wire.h>
#include <Time.h> 
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_LSM303.h>

#define OLED_DC 12
#define OLED_CS 10
#define OLED_CLK 13
#define OLED_MOSI 11
#define OLED_RESET 9
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);


Adafruit_LSM303 accel;
int xtilt = 0;
int ytilt = 0;
int ztilt = 0;

int xpitch = 0;
int ycc = 0;

int readNum = 3;
int reading[3];
int meanReading = 0;
int rms = 0;
int lastrms = 0;
int sawrms = 0;
int midirms = 0;

int note = 57;
int lastnote = 0;
int resetMIDI = 4;
int instrument = 118;

int flexCount = 0;
int flex = 0;
int lastFlex = 0;
int thresh = 23;

int cyclecount = 0;
int pastrms[19];

void setup()   {                
  Serial.begin(9600);
  pinMode(4,INPUT_PULLUP);
  
  display.begin(SSD1306_SWITCHCAPVCC);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,11);
  display.println(" ~ BIOJAM");
  display.display();
  delay(1000);
  
  Serial3.begin(31250);
  MIDIwrite((0xC0 | 1),instrument,instrument);
  cc(1,0,0);
}


void loop() {
  display.clearDisplay();
  display.drawLine(0, pastrms[0],  6, pastrms[1], WHITE);
  display.drawLine(7, pastrms[1],  13, pastrms[2], WHITE);
  display.drawLine(14, pastrms[2],  20, pastrms[3], WHITE);
  display.drawLine(21, pastrms[3],  27, pastrms[4], WHITE);
  display.drawLine(28, pastrms[4],  34, pastrms[5], WHITE);
  display.drawLine(35, pastrms[5],  41, pastrms[6], WHITE);
  display.drawLine(42, pastrms[6],  48, pastrms[7], WHITE);
  display.drawLine(49, pastrms[7],  55, pastrms[8], WHITE);
  display.drawLine(56, pastrms[8],  62, pastrms[9], WHITE);
  display.drawLine(63, pastrms[9],  69, pastrms[10], WHITE);
  display.drawLine(70, pastrms[10], 76, pastrms[11], WHITE);
  display.drawLine(77, pastrms[11], 83, pastrms[12], WHITE);
  display.drawLine(84, pastrms[12], 90, pastrms[13], WHITE);
  display.drawLine(91, pastrms[13], 97,pastrms[14], WHITE);
  display.drawLine(98,pastrms[14], 104,pastrms[15], WHITE);
  display.drawLine(105,pastrms[15], 111,pastrms[16], WHITE);
  display.drawLine(112,pastrms[16], 118,pastrms[17], WHITE);
  display.drawLine(119,pastrms[17], 128,pastrms[18], WHITE);
  display.display();
  
  //accel.read();
  
  if((int)accel.accelData.y >= 3000) {
    ytilt = map((int)accel.accelData.y,3072,4096,-1024,0);
  }else {
    ytilt = map((int)accel.accelData.y,0,1024,0,1024);
  }
    
  if((int)accel.accelData.x >= 3000) {
    xtilt = map((int)accel.accelData.x,3072,4096,-1024,0);
  }else {
    xtilt = map((int)accel.accelData.x,0,1024,0,1024);
  }
  
  if((int)accel.accelData.z >= 3000) {
    ztilt = map((int)accel.accelData.z,3072,4096,-1024,0);
  }else {
    ztilt = map((int)accel.accelData.z,0,1024,0,1024);
  }
  
  for(int i = 0; i < readNum; i++){
    reading[i] = sq((analogRead(A0)-(analogRead(A1)-5)));
    meanReading += reading[i];
  }
  meanReading /= readNum;
  rms = sqrt(meanReading);
  rms /= 18;
  rms = sq(rms);
  if(rms < lastrms) {
    sawrms = lastrms - 33;
  } else {
    sawrms = rms;
  }
  lastrms = sawrms;
  midirms = map(sawrms, 0, 1023, 0, 127);
  
  if(midirms >= thresh) {
    flex = 1;
    display.invertDisplay(true);
  }
  else {
    flex = 0;
    display.invertDisplay(false);
  }
  
  if (flex != lastFlex) {
    if (midirms > thresh) {
        flexCount++;
        note = map(xtilt,-1024,1024,51,69);
        noteOn(1,note,127);
        lastnote = note;
    } else {
      noteOff(1,lastnote,0);
    }
  }
  
  lastFlex = flex;
  pastrms[cyclecount] = map(analogRead(0), 0, 1023, 0, 32);
  cyclecount++;
  if(cyclecount >= 19) {
    cyclecount = 0;
  }
  
  Serial.println(sawrms);
  
  ycc = map(ytilt,-1024,1024,0,127);
  cc(1,1,ycc);
}

void noteOn(byte channel, byte note, byte attack_velocity) {
  MIDIwrite( (0x90 | channel), note, attack_velocity);
}

void noteOff(byte channel, byte note, byte release_velocity) {
  MIDIwrite( (0x80 | channel), note, release_velocity);
}

void cc(byte channel, byte command, byte value) {
  MIDIwrite( (0xB0 | channel), command, value);
}

void pitchBend(byte channel, byte lsb, byte msb) {
  MIDIwrite((0xE0 | channel), lsb, msb);
}

void MIDIwrite(byte cmd, byte data1, byte data2) {
  digitalWrite(5, HIGH);
  Serial3.write(cmd);
  Serial3.write(data1);
  
  if( (cmd & 0xF0) <= 0xB0){
    Serial3.write(data2);
  }
  digitalWrite(5, LOW);
}
Above, I have the line accel.read commented out. This compiles and runs as intended, but uncommenting that line renders the device useless until re-flashed.

Any Ideas what else I may be doing ring to cause this?

Also for reference, here is the codespace used up on the teensy according to arduino:

Code: Select all

Binary sketch size: 21,664 bytes (of a 131,072 byte maximum)
Estimated memory use: 4,308 bytes (of a 16,384 byte maximum)
Cheers in advance if anyone has advice!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Teensy 3.0 crashes with lsm303 code integration

Post by adafruit_support_rick »

I don't see a call to accel.begin() in your setup routine.

Code: Select all

  // Try to initialise and warn if we couldn't detect the chip
  if (!accel.begin())
  {
    Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!");
    while (1);
  }

User avatar
Hatchmanpro
 
Posts: 12
Joined: Thu Sep 26, 2013 4:08 am

Re: Teensy 3.0 crashes with lsm303 code integration

Post by Hatchmanpro »

Thank you much Rick!!! it worked like a charm with no fuss. There truly is a time of night where the brain can no longer process the obvious...

I seemed to neglect this line of code, as It didn't register in my mind that an if statement that checks for !accel.begin() was equivalent to writing accel.begin().... I had assumed it did this automatically and the setup in the example code was just a check for sanity on wiring...

Regardless, I will be carrying on, adding in a bunch to make it all work more smoothly, Cheers for the quick response and hooray that it works! One day I'll present my project to show what I'm doing...

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”