NeTgHoSt
Member
Good evening from California. I have a serious intermittent issue with my Teensy 3.2 regularly freezing in the middle of my code, whereafter it will not reboot, even with a power cycle. Sometimes it will turn back on, but I haven't figured out what is preventing it from rebooting successfully. I posted another thread a little while back, my first post, about it not loading a bitmap image properly. It would load sections of repeating colors the same size as the intended bitmap. It was never resolved, mostly because of this new issue... Here is the photo of that project:
Soon after posting that, my teensy 3.2 began doing this freezing thing. I removed it from that breadboard, and started a fresh project to make sure connections weren't the issue. I removed the thermistor, MicroSD card reader, (only needed to display that color bitmap) added an HC-SR04 Ultrasonic Sensor, and changed out the passive buzzer for an active one; controlled and powered indirectly using a transistor. Also added 2 buttons to switch between functions; my code has evolved quite a bit as my knowledge improved over the last couple weeks. The previous setup used no buttons, as you can see. I also replaced the delay()'s with millis() as suggested in the other thread. Here is the current breadboard setup: (BTW, this setup worked fine for many days of intermittent testing without fail)
I was able to get it to turn on again just now to take 2 photos of the 2 functions running. As I typed all this, the screen blanked out yet again during the idleDisplay function. At the moment, the function is programmed to display the battery level reading (0-1024 on the left, map from 0-100 on the right) from A9 on the OLED in plain text (for debugging):
Here is the other distanceDisplay function, which suprisingly runs fine without fail; even though it would always fail on the last project setup. It only seems to freeze after running the idleDisplay function for some time.
I was super proud of myself for getting this far.. and now I'm feeling quite down, thinking I must've overlooked something important that may be causing these issues that I'm unable to pin down. It seemed to be working fine until I added the battery level reading.. unless that is coincidence. Help would be very much appreciated! Let me know what other information might be needed about my setup. Thank you!!!
Current Code:
How my code works:
It powers up into "mode 0" at startup, the "idleDisplay" mode which calls "idleDisplaySetup" function once to display the red flag, and then returns to "idleDisplay" function to show the voltage readings from A9 to the left, with a % reading to the right. This code repeats (not using "delay()") until the button for "mode 1" is pressed. "Mode 1" runs the "distanceDisplay" function, which calls "distanceDisplaySetup" once, and then loops that code using the newPing library until the "mode 0" button is pressed again, taking it back into the "idleDisplay" function, etc. The buzzer is controlled by "if" statements in the "distanceDisplay" function, using 3 different "if" statements for 3 different beeping speeds, depending on distance. It works as expected, when it's not bricked.
There are some "Buzzer.h" files included in separate tabs to make the "buzzer.on()" functions work. I can include those if needed.
Soon after posting that, my teensy 3.2 began doing this freezing thing. I removed it from that breadboard, and started a fresh project to make sure connections weren't the issue. I removed the thermistor, MicroSD card reader, (only needed to display that color bitmap) added an HC-SR04 Ultrasonic Sensor, and changed out the passive buzzer for an active one; controlled and powered indirectly using a transistor. Also added 2 buttons to switch between functions; my code has evolved quite a bit as my knowledge improved over the last couple weeks. The previous setup used no buttons, as you can see. I also replaced the delay()'s with millis() as suggested in the other thread. Here is the current breadboard setup: (BTW, this setup worked fine for many days of intermittent testing without fail)
I was able to get it to turn on again just now to take 2 photos of the 2 functions running. As I typed all this, the screen blanked out yet again during the idleDisplay function. At the moment, the function is programmed to display the battery level reading (0-1024 on the left, map from 0-100 on the right) from A9 on the OLED in plain text (for debugging):
Here is the other distanceDisplay function, which suprisingly runs fine without fail; even though it would always fail on the last project setup. It only seems to freeze after running the idleDisplay function for some time.
I was super proud of myself for getting this far.. and now I'm feeling quite down, thinking I must've overlooked something important that may be causing these issues that I'm unable to pin down. It seemed to be working fine until I added the battery level reading.. unless that is coincidence. Help would be very much appreciated! Let me know what other information might be needed about my setup. Thank you!!!
Current Code:
Code:
////////////////////////////////////////////////////////////////////////////////////
/////Ultrasonic Distance Meter aka Backup Proximity Alert for vehicular usage///////
////////////////////////////////////////////////////////////////////////////////////
//Libraries
#include <NewPing.h>
#include <Adafruit_GFX.h>
#include <ST7735_t3.h>
#include <SPI.h>
#include "Buzzer.h"
//Compiler / Global Defines
#define TFT_MOSI 11 //Signal input pin (SDA)
#define TFT_SCLK 13 //Clock pin (SCK)
#define TFT_DC 20 //Data/command pin (DC )
#define TFT_CS 15 //Chip select pin (CS )
#define TFT_RST 14 //Reset pin (RST)
#define TFT_BLK 12 //Backlight Control pin (BLK)
#define TRIGGER_PIN 16 // Teensy pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 19 // Teensy pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Max ping distance (in cm). Max distance is 400-500cm.
#define BUZZER_PIN_SIG 6 //Beeper pin
#define battLevelPin A9
//Sensor and Display Connection Setup etc.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
ST7735_t3 tft = ST7735_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
Buzzer buzzer(BUZZER_PIN_SIG);
boolean modeState;
boolean ranDistanceSetup = false;
boolean ranIdleSetup = false;
const unsigned char PROGMEM flag [] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00,
0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFE, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0x00, 0x00, 0x00,
0x0C, 0x3C, 0x3F, 0x00, 0x00, 0x00, 0x0C, 0x3C, 0x3F, 0x00, 0x00, 0x00, 0x0C, 0x3C, 0x3F, 0x00,
0x00, 0x00, 0x0C, 0x3C, 0x3F, 0x00, 0x00, 0x00, 0x0F, 0xC3, 0xC3, 0xFF, 0xFF, 0xF8, 0x0F, 0xC3,
0xC3, 0xFF, 0xFF, 0xFC, 0x0F, 0xC3, 0xC3, 0xF0, 0xF0, 0xFC, 0x0F, 0xC3, 0xC3, 0xF0, 0xF0, 0xFC,
0x0C, 0x3C, 0x3F, 0xF0, 0xF0, 0xFC, 0x0C, 0x3C, 0x3F, 0xF0, 0xF0, 0xFC, 0x0C, 0x3C, 0x3F, 0x0F,
0x0F, 0x0C, 0x0C, 0x3C, 0x3F, 0x0F, 0x0F, 0x0C, 0x0F, 0xC3, 0xC3, 0x0F, 0x0F, 0x0C, 0x0F, 0xC3,
0xC3, 0x0F, 0x0F, 0x0C, 0x0F, 0xC3, 0xC3, 0xF0, 0xF0, 0xFC, 0x0F, 0xC3, 0xC3, 0xF0, 0xF0, 0xFC,
0x0C, 0x3C, 0x3F, 0xF0, 0xF0, 0xFC, 0x0C, 0x3C, 0x3F, 0xF0, 0xF0, 0xFC, 0x0C, 0x3C, 0x3F, 0x0F,
0x0F, 0x0C, 0x0C, 0x3C, 0x3F, 0x0F, 0x0F, 0x0C, 0x0F, 0xFF, 0xFF, 0x0F, 0x0F, 0x0C, 0x0F, 0xFF,
0xFF, 0x0F, 0x0F, 0x0C, 0x0C, 0x03, 0x1E, 0xF0, 0xF0, 0xFC, 0x0C, 0x03, 0x3C, 0xF0, 0xF0, 0xFC,
0x0C, 0x03, 0x70, 0xF0, 0xF0, 0xFC, 0x0C, 0x03, 0xE0, 0xF0, 0xF0, 0xFC, 0x0C, 0x03, 0xFF, 0xFF,
0xFF, 0xFC, 0x0C, 0x03, 0xFF, 0xFF, 0xFF, 0xFC, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void setup() {
pinMode(TFT_BLK, OUTPUT);
digitalWrite(TFT_BLK, HIGH);
pinMode(5, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
tft.initR(INITR_MINI160x80_ST7735S);
tft.setRotation(3);
tft.fillScreen(ST7735_BLACK);
Serial.begin(9600);
Serial.print("TFT Init!");
}
int mode = 0;
void loop() {
if (mode == 0) {
idleDisplay();
} else {
distanceDisplay();
}
if (digitalRead(5) == LOW) {
mode = 0;
Serial.println("mode 0");
}
if (digitalRead(7) == LOW) {
mode = 1;
Serial.println("mode 1");
}
}
void distanceDisplaySetup() {
tft.fillScreen(ST7735_GREEN);
tft.fillRect(2, 2, 156, 76, ST7735_BLACK);
tft.setTextColor(ST7735_YELLOW, ST7735_BLACK);
tft.setCursor(52, 4);
tft.setTextSize(2);
tft.print("Feet:");
tft.setTextSize(5);
tft.drawLine(27, 20, 132, 20, ST7735_GREEN);
tft.drawLine(26, 2, 26, 77, ST7735_GREEN);
tft.drawLine(133, 2, 133, 77, ST7735_GREEN);
tft.setTextColor(ST7735_RED, ST7735_BLACK);
ranDistanceSetup = true;
ranIdleSetup = false;
}
int inchmap = 0;
int inchmapchangevalue = 0;
void distanceDisplay() {
if (ranDistanceSetup == false) {
distanceDisplaySetup();
}
float inchAverage = 0;
for (int i = 1; i < 10; i++) {
inchAverage += sonar.ping_in();
}
float inch = (inchAverage / 10) * 100;
float feet = (inch / 12) / 100;
inchmap = map(inch, 0, 12000, 1, 75);
inchmap = constrain(inchmap, 1, 75);
if (inchmapchangevalue != inchmap) {
tft.fillRect(2, 2, 23, 75, ST7735_RED);
tft.fillRect(134, 2, 23, 75, ST7735_RED);
tft.fillRect(2, 2, 23, inchmap, ST7735_BLACK);
tft.fillRect(134, 2, 23, inchmap, ST7735_BLACK);
tft.drawFloat(feet, 1, 36, 32);
inchmapchangevalue = inchmap;
}
if (inch <= 1200 && inch > 1) {
fastBeep();
}
if (inch <= 3600 && inch > 1200) {
medBeep();
}
if (inch <= 7200 && inch > 3600) {
slowBeep();
}
else {
for (int x = 0; x < 50; x++) {
delay(1);
}
}
}
unsigned int x = 57;
unsigned int y = 16;
void idleDisplaySetup() {
buzzer.off();
tft.fillScreen(ST7735_BLACK);
tft.drawBitmap(x, y, flag, 48, 48, ST7735_RED);
tft.setTextSize(1);
tft.setTextColor(ST7735_YELLOW, ST7735_BLACK);
ranIdleSetup = true;
ranDistanceSetup = false;
}
unsigned int battLevel = 0;
unsigned int battLevelChange = 0;
void idleDisplay() {
if (ranIdleSetup == false) {
idleDisplaySetup();
}
unsigned int battAdcAverage = 0;
for (int i = 1; i < 10; i++) {
battAdcAverage += analogRead(battLevelPin);
}
unsigned int battAdcReading = (battAdcAverage / 10);
unsigned int battLevel = map(battAdcReading, 640, 740, 0, 100);
battLevel = constrain(battLevel, 0, 100);
if (battLevelChange != battLevel) {
tft.setCursor(10, 40);
tft.print(" ");
tft.setCursor(120, 40);
tft.print(" ");
tft.drawNumber(battAdcReading, 10, 40);
tft.drawNumber(battLevel, 120, 40);
for (int x = 0; x < 100; x++) {
delay(1);
}
}
battLevelChange = battLevel;
}
unsigned long slowinterval = 300;
unsigned long medinterval = 150;
unsigned long fastinterval = 75;
unsigned long previousMillis = 0;
bool buzzerState = false;
void slowBeep() {
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) >= slowinterval) {
previousMillis = currentMillis;
if (buzzerState == false) {
buzzerState = true;
}
else {
buzzerState = false;
}
if (buzzerState == true) {
buzzer.on();
}
if (buzzerState == false) {
buzzer.off();
}
}
}
void medBeep() {
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) >= medinterval) {
previousMillis = currentMillis;
if (buzzerState == false) {
buzzerState = true;
}
else {
buzzerState = false;
}
if (buzzerState == true) {
buzzer.on();
}
if (buzzerState == false) {
buzzer.off();
}
}
}
void fastBeep() {
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) >= fastinterval) {
previousMillis = currentMillis;
if (buzzerState == false) {
buzzerState = true;
}
else {
buzzerState = false;
}
if (buzzerState == true) {
buzzer.on();
}
if (buzzerState == false) {
buzzer.off();
}
}
}
How my code works:
It powers up into "mode 0" at startup, the "idleDisplay" mode which calls "idleDisplaySetup" function once to display the red flag, and then returns to "idleDisplay" function to show the voltage readings from A9 to the left, with a % reading to the right. This code repeats (not using "delay()") until the button for "mode 1" is pressed. "Mode 1" runs the "distanceDisplay" function, which calls "distanceDisplaySetup" once, and then loops that code using the newPing library until the "mode 0" button is pressed again, taking it back into the "idleDisplay" function, etc. The buzzer is controlled by "if" statements in the "distanceDisplay" function, using 3 different "if" statements for 3 different beeping speeds, depending on distance. It works as expected, when it's not bricked.
There are some "Buzzer.h" files included in separate tabs to make the "buzzer.on()" functions work. I can include those if needed.