I try to marry two sketches: sonar measurement and a 1.3" SH1106x(?) display.
I am having problems with:
- choice of right constructor
- where to place the display code
- how to make the display showing variable sensor values
- ...
Constructors
Some examples that have worked with these constructors go well on their own with my display:
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // OLEDs without Reset of the Display
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2-Initialisierungsfunktion (U8G2_R0, /* reset=*/ U8X8) _PIN_NONE)
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST); // Fast I2C / TWI
Display example sketches
GyverOLED.h
-> the only which worked immediatly
U8x8lib.h
-> in terms of little space required, but maybe less relevant
U8glib.h
-> recommended by hardware supplier
Sonar scetch "NewPing" modified
// ---------------------------------------------------------------------------
// Calculate a ping median using the ping_timer() method.
// ---------------------------------------------------------------------------
#include <Arduino.h>
#include <U8x8lib.h>
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // OLEDs without Reset of the Display
#include <NewPing.h>
#define ITERATIONS 5 // Number of iterations.
#define TRIGGER_PIN 8 // Arduino pin tied to trigger pin on ping sensor.
#define ECHO_PIN 9 // Arduino pin tied to echo pin on ping sensor.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
unsigned long pingTimer[ITERATIONS]; // Holds the times when the next ping should happen for each iteration.
unsigned int cm[ITERATIONS]; // Where the ping distances are stored.
uint8_t currentIteration = 0; // Keeps track of iteration step.
int prozentWert;
int Empty=25;
int Full=3;
int EmptyPercent=0;
int FullPercent=100;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(115200);
pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
for (uint8_t i = 1; i < ITERATIONS; i++) // Set the starting time for each iteration.
pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
{
u8x8.begin();
u8x8.setPowerSave(0);
}
}
void loop() {
for (uint8_t i = 0; i < ITERATIONS; i++) { // Loop through all the iterations.
if (millis() >= pingTimer[i]) { // Is it this iteration's time to ping?
pingTimer[i] += PING_INTERVAL * ITERATIONS; // Set next time this sensor will be pinged.
if (i == 0 && currentIteration == ITERATIONS - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
sonar.timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance).
currentIteration = i; // Sensor being accessed.
cm[currentIteration] = 0; // Make distance zero in case there's no ping echo for this iteration.
sonar.ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
}
}
// Other code that *DOESN'T* analyze ping results can go here.
}
void echoCheck() { // If ping received, set the sensor distance to array.
if (sonar.check_timer())
cm[currentIteration] = sonar.ping_result / US_ROUNDTRIP_CM;
}
void oneSensorCycle() { // All iterations complete, calculate the median.
unsigned int uS[ITERATIONS];
uint8_t j, it = ITERATIONS;
uS[0] = NO_ECHO;
for (uint8_t i = 0; i < it; i++) { // Loop through iteration results.
if (cm[i] != NO_ECHO) { // Ping in range, include as part of median.
if (i > 0) { // Don't start sort till second ping.
for (j = i; j > 0 && uS[j - 1] < cm[i]; j--) // Insertion sort loop.
uS[j] = uS[j - 1]; // Shift ping array to correct position for sort insertion.
} else j = 0; // First ping is sort starting point.
uS[j] = cm[i]; // Add last ping to array in sorted position.
} else it--; // Ping out of range, skip and don't include as part of median.
}
int aktuellerWert=(uS[it >> 1]);
prozentWert=map(aktuellerWert,Empty, Full,EmptyPercent,FullPercent);
if (aktuellerWert>=25){
Serial.println("Tank leer!");
// WHERE HAS THIS CONTENT TO BE??? ;-)
u8x8.setFont(u8x8_font_8x13_1x2_r );
// THIS IS WHAT I NEED ON DISPLAY: prozentWert
u8x8.drawString(1,0,(prozentWert));
u8x8.drawString(1,2,"% übrig");
u8x8.refreshDisplay(); // only required for SSD1606/7 (unclear, if needed)
}
else if (aktuellerWert<=3){
Serial.println("Tank voll!");
}
else if (aktuellerWert>3<25){
Serial.print("Tank ");
Serial.print(prozentWert);
Serial.print("%");
Serial.println(" voll");
}
}
Trying to marry scetches caused two outcomes: the display shows once an information and the sonar measurement in the background
- works fine
- shows a wrong value continiously and doesn't react on sensor value changes
The display may stay frozen, even after Mega 2560 reset.
Help would be great! ![]()