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);
}
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)