He's plotting some readings from a ultrasound sensor. It's rolling but and the plot will move around according to the distance of the object from the sensor.
I'm using a SSD1309 display. So my display is a bit bigger.
I think my question is: What is going on here?
int i;
for (i = 0; i < 127; i++) {
display.drawLine(i, 64 - distances[i], (i + 1), 64 - distances[i + 1], WHITE);
distances[i] = distances[i + 1];
}
distances[127] = distance;
When I put this in, it crashes the Arduino. Is the for loop just drawing the initial plot? The author tries to explain it but seems like he just rolls over quickly through it.
What's the easiest way to do a rolling graph?
My code is here:
#include <Arduino.h>
#include <U8x8lib.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
U8G2_SSD1309_128X64_NONAME2_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
const int trigPin = 6;
const int echoPin = 7;
float duration, distance;
int distances[128];
void setup() {
u8g2.begin();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop(void) {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2; // reports in centimeters
Serial.print("Distance: ");
Serial.println(distance);
u8g2.setCursor(0,10);
u8g2.print("Distance: " ); u8g2.print(distance,2); u8g2.print(" cm");
drawline();
u8g2.sendBuffer(); // transfer internal memory to the display
u8g2.display();
delay(1000);
}
void drawline(){
int i;
for (i = 0; i < 127; i++) {
u8g2.drawLine(i, 64 - distances[i], (i + 1), 64 - distances[i + 1]);
distances[i] = distances[i + 1];
}
distances[127] = distance;
}
The loop function takes care of that. The "distances" data array is initialized to zeros, but after 128 iterations of loop(), it will be filled with presumably valid data.
The code is correct but the first 127 plots (after program startup) will have mostly zero entries.
Who knows? You forgot to describe the symptoms of the crash. Explain what you expected the program to do and what it did instead. Describe or post examples of the output.
Why did you switch to U8G2? Maybe you've configured it incorrectly.
The project is supposed to work as above in the link. The Arduino plots the output of the ultrasonic sensor.
I started with u8x8 because I've used it in the past. Then I realized I can't draw lines plus the sample code in the like uses u8g2 library.
By crashing I mean the Arduino stops responding. No lights. Nothing on the serial output. Lcd is dark. I can comment out the drawline function and it works again.
A simulation of your sketch results in compile errors stating the functions you call do not exist. Is it possible you are using a non-standard U8g library?
sketch.ino: In function 'void loop()':
sketch.ino:27:8: error: 'class U8X8_SSD1306_128X64_NONAME_HW_I2C' has no member named 'clearBuffer'; did you mean 'clearLine'?
u8g2.clearBuffer(); // clear the internal memory
^~~~~~~~~~~
clearLine
sketch.ino:46:8: error: 'class U8X8_SSD1306_128X64_NONAME_HW_I2C' has no member named 'sendBuffer'
u8g2.sendBuffer(); // transfer internal memory to the display
^~~~~~~~~~
sketch.ino: In function 'void drawline()':
sketch.ino:55:10: error: 'class U8X8_SSD1306_128X64_NONAME_HW_I2C' has no member named 'drawLine'; did you mean 'drawTile'?
u8g2.drawLine(i, 64 - distances[i], (i + 1), 64 - distances[i + 1]);
^~~~~~~~
drawTile
Error during build: exit status 1