This is part two of our tutorial to use the LED Matrix on an Arduino UNO R4 WiFi to plot a sensor signal. The first part covers the hardware setup and demo itself, while this part discusses the software and how to make modifications.
Make sure you also check out part one to see a description of the demo itself!
Baseline calculation and displayed range explainedSince we have a limited range - 8 steps of resolution - I chose to calculate an initial baseline that would represent the smallest value on the Y axis based on the current temperature value, scaled by a factor < 1 to make the Y origin slightly below the actual temperature. For the maximum Y value displayed, I simply add a predefined range to the baseline.
The defaults for those two values are defined at the top of the sketch, as such:
static const uint8_t TEMP_RANGE = 5;
...
static const double BASELINE_SCALE_FACTOR = 0.98;
TEMP_RANGE is in degrees celsius, so we're showing 5 degrees Celsius or 9 degrees Fahrenheit. To convert between the two, you can use the following formula:
Tf = Tc * 9 / 5
and
Tc = Tf * 5 / 9
Therefore, if you wanted the range to be 10 degrees F, you could calculate 10 * 5 / 9 = 5.556, and then round up to 6. I may update TEMP_RANGE to a floating point type in the future to allow for better approximation.
The calculation of the minmum and maximum values should then be pretty straightforward:
// Calculate min value (baseline)
bool baselineInitialized = false;
while (!baselineInitialized) {
if (!Sht4xMeasureHighPrecision()) {
delay(500);
continue;
}
minVal = (uint8_t)(BASELINE_SCALE_FACTOR * temperature);
maxVal = minVal + TEMP_RANGE;
Serial.print("minValue: ");
Serial.println(minVal);
baselineInitialized = true;
}
Readout frequencyThe readout frequency is defined in the variable REFRESH_DELAY, which defines the time between measurements. If you'd like the data to scroll faster, decrease this constant, and likewise if you'd like to slow it down, increase it:
static const uint8_t REFRESH_DELAY = 250;
This variable represents a delay in milliseconds. The 250ms roughly corresponds to 4 Hz, although will be slightly lower in real life since the sensor readout and LED matrix update will take a bit of time as well, which is added on top of the delay. This could be worked around by timing the duration of said code, and subtracting it from the delay constant.
Changing the I2C portThe Arduino UNO R4 WiFi has two I2C interfaces, one accessible via the pin headers (called Wire
), and one on the QWIIC connector (called Wire1
). By default, the sketch is configured to use Wire1
. To change this, simply modify the SHT_I2C_INTERFACE constant:
static TwoWire& SHT_I2C_INTERFACE = Wire1; // Wire1 = Uno R4 QWIIC port
The 'fill' parameter of the insert_right(...) functionThe function that updates the data matrix has an ominous fill
parameter that defaults to true
. This variable determines whether each value is shown as a single "pixel" representing its value, or a filled bar chart, like this:
Comments
Please log in or sign up to comment.