Hey everyone,
I'm quite new to all the programation of ESP32 and that sort of things and i'm struggling to troubleshoot a bug i've been encountering for a while now :
The onboard LED won't stop blinking after i upload my code and won't do anything else.
For context, i want to make a little accelerometer with a screen to display on a small SH1106 OLED pannel both current speed (m/s) and acceleration(m/s²).
It worked for a while, displaying the sensors' reading on the display and everything seems accurate. And it stopped working as soon as i tried to factorize my code.
Here are my parts :
Here's the latest faulty code :
#include <Wire.h>
// OLED display 128px * 64px
#include <Arduino.h>
#include <U8x8lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
U8X8_SH1106_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
// Accelerometer ADXL345
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
char* float_to_string(float value)
{
char str[24];
return dtostrf(value, 8, 2, str);
}
void display_accel_value(int display_x, int display_y, char[8] prefix, char* value, char[8] suffix)
{
// prefix // value // suffix
u8x8.drawString(0,3,prefix ); u8x8.drawString(2,3,value ); u8x8.drawString(5,3,suffix);
return ;
}
void setup(void)
{
u8x8.begin();
u8x8.setPowerSave(0);
if(!accel.begin())
{
/* There was a problem detecting the ADXL345 ... check your connections */
u8x8.drawString(0,1,"no ADXL345 detected");
u8x8.drawString(0,2,"___________________________________");
while(1);
} else {
u8x8.drawString(0,1,"ADXL345 sensor detected !");
u8x8.drawString(0,2,"___________________________________");
}
accel.setRange(ADXL345_RANGE_16_G);
}
void loop(void)
{
u8x8.setFont(u8x8_font_chroma48medium8_r);
/* Get a new sensor event */
sensors_event_t event;
accel.getEvent(&event);
/* Display the results (acceleration is measured in m/s^2) */
display_accel_value(0, 3, "X : ", float_to_string(event.acceleration.x), " m/s²");
display_accel_value(0, 4, "Y : ", float_to_string(event.acceleration.y), " m/s²");
display_accel_value(0, 5, "Z : ", float_to_string(event.acceleration.z), " m/s²");
u8x8.drawString(0,6,"___________________________________");
display_accel_value(0, 7, "Sum : ", float_to_string(abs(event.acceleration.x) + abs(event.acceleration.y) + abs(event.acceleration.z)), " m/s²");
delay(100);
}
And here's the last working code :
#include <Wire.h>
// OLED display 128px * 64px
#include <Arduino.h>
#include <U8x8lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
U8X8_SH1106_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
// Accelerometer ADXL345
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
// End of constructor list
void setup(void)
{
u8x8.begin();
u8x8.setPowerSave(0);
if(!accel.begin())
{
/* There was a problem detecting the ADXL345 ... check your connections */
u8x8.drawString(0,1,"no ADXL345 detected");
while(1);
}
accel.setRange(ADXL345_RANGE_16_G);
}
void loop(void)
{
u8x8.setFont(u8x8_font_chroma48medium8_r);
/* Get a new sensor event */
sensors_event_t event;
accel.getEvent(&event);
char strx[32];
char stry[32];
char strz[32];
/* Display the results (acceleration is measured in m/s^2) */
u8x8.drawString(0,3,"X : "); u8x8.drawString(4,3, dtostrf(event.acceleration.x, 8, 2, strx));
u8x8.drawString(0,4,"Y : "); u8x8.drawString(4,4, dtostrf(event.acceleration.y, 8, 2, stry));
u8x8.drawString(0,5,"Z : "); u8x8.drawString(4,5, dtostrf(event.acceleration.z, 8, 2, stry));
delay(10);
}
PS : the compilation and upload works and doesn't output any error, but i'm getting this for every build now : Sketch uses 324439 bytes (24%) of program storage space. Maximum is 1310720 bytes. Global variables use 22272 bytes (6%) of dynamic memory, leaving 305408 bytes for local variables. Maximum is 327680 bytes.
PS2: I tried pressing the rst button after uploading with no success sadly ![]()
Thanks in advance for everyone's help !