I want to write the distance information coming from HC-SR04 on the 12x8 matrix LEDs of Uno R4 as scrolling numbers. But what I see is random scrolling garbled symbols, occasionally legible numbers here and there. And the scrolling animation is also stuttery.
What am I doing wrong?
#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"
ArduinoLEDMatrix matrix;
const int trigPin = 13;
const int echoPin = 12;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
matrix.begin();
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
// Make it scroll!
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textScrollSpeed(50);
// add the text
matrix.textFont(Font_5x7);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(distance);
matrix.endText(SCROLL_LEFT);
matrix.endDraw();
}
There's something odd here. I took the Arduino example, which worked fine. I then removed a few of the leading and trailing spaces from the Hello World! string and oddball characters started showing up.
which, after running addr2line points to line 257 in ArduinoGraphics.cpp, or:
const uint8_t* b = _font->data[c];
which is in the text(...) function that converts each character in the string to its bitmap and adds it to the buffer. At first glance, it might be that somehow the loop has gone past the end of the string and is indexing nonsense data. Why it's doing that... I haven't figured out.