I'm working on a little project that takes the current temperature from a DHT11 temperature/humidity sensor and prints the temperature graphically to the built-in display.
It seems like every few updates, there will be one randomly flickering pixel. It gets super bright for a fraction of a second, and is a pretty unpleasant visual effect. Can anyone take a look at my code and suggest reasons why this might be happening? Or is it a hardware effect?
#include "Arduino_LED_Matrix.h"
ArduinoLEDMatrix matrix;
#include "DHT.h"
#define DHTPIN A0
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
byte result[8][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
byte zero[8][6] = {
{0,0,0,0,0,0},
{0,1,1,1,1,0},
{0,1,0,0,1,0},
{0,1,0,0,1,0},
{0,1,0,0,1,0},
{0,1,0,0,1,0},
{0,1,1,1,1,0},
{0,0,0,0,0,0}
};
byte one[8][6] = {
{0,0,0,0,0,0},
{0,0,1,0,0,0},
{0,1,1,0,0,0},
{0,0,1,0,0,0},
{0,0,1,0,0,0},
{0,0,1,0,0,0},
{0,1,1,1,0,0},
{0,0,0,0,0,0}
};
// Number 2
byte two[8][6] = {
{0,0,0,0,0,0},
{0,1,1,1,0,0},
{0,0,0,1,0,0},
{0,0,0,1,0,0},
{0,0,1,0,0,0},
{0,1,0,0,0,0},
{0,1,1,1,1,0},
{0,0,0,0,0,0}
};
// Number 3
byte three[8][6] = {
{0,0,0,0,0,0},
{0,1,1,1,0,0},
{0,0,0,1,0,0},
{0,0,1,1,0,0},
{0,0,0,0,1,0},
{0,0,0,0,1,0},
{0,1,1,1,0,0},
{0,0,0,0,0,0},
};
// Number 4
byte four[8][6] = {
{0,0,0,0,0,0},
{0,1,0,1,0,0},
{0,1,0,1,0,0},
{0,1,1,1,0,0},
{0,0,0,1,0,0},
{0,0,0,1,0,0},
{0,0,0,1,0,0},
{0,0,0,0,0,0}
};
// Number 5
byte five[8][6] = {
{0,0,0,0,0,0},
{0,1,1,1,1,0},
{0,1,0,0,0,0},
{0,1,1,1,0,0},
{0,0,0,0,1,0},
{0,0,0,0,1,0},
{0,1,1,1,0,0},
{0,0,0,0,0,0}
};
// Number 6
byte six[8][6] = {
{0,0,0,0,0,0},
{0,1,1,1,1,0},
{0,1,0,0,0,0},
{0,1,1,1,0,0},
{0,1,0,0,1,0},
{0,1,0,0,1,0},
{0,1,1,1,0,0},
{0,0,0,0,0,0}
};
// Number 7
byte seven[8][6] = {
{0,0,0,0,0,0},
{0,1,1,1,1,0},
{0,0,0,0,1,0},
{0,0,0,1,0,0},
{0,0,1,0,0,0},
{0,0,1,0,0,0},
{0,0,1,0,0,0},
{0,0,0,0,0,0}
};
// Number 8
byte eight[8][6] = {
{0,0,0,0,0,0},
{0,1,1,1,1,0},
{0,1,0,0,1,0},
{0,1,1,1,1,0},
{0,1,0,0,1,0},
{0,1,0,0,1,0},
{0,1,1,1,1,0},
{0,0,0,0,0,0}
};
// Number 9
byte nine[8][6] = {
{0,0,0,0,0,0},
{0,1,1,1,1,0},
{0,1,0,0,1,0},
{0,1,1,1,1,0},
{0,0,0,0,1,0},
{0,0,0,0,1,0},
{0,1,1,1,0,0},
{0,0,0,0,0,0}
};
byte blank[8][6] = {
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0}
};
// Subroutine to take two 8x6 bitmaps and combine them into an 8x12 frame
void combineDigits(byte result[8][12], byte digit1[8][6], byte digit2[8][6]) {
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 6; ++j) {
result[i][j] = digit1[i][j];
}
}
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 6; ++j) {
result[i][j + 6] = digit2[i][j];
}
}
}
// Define an arbitrary lastTemperature
int lastTemperature = 70;
// Subroutine to take a temperature and show it on the matrix
void displayTemperature(int temperature) {
// Don't do anything unless the temperature has changed
if (lastTemperature != temperature){
// Separate the temperature digits
int digit1 = temperature / 10;
int digit2 = temperature % 10;
// Define arrays for the digits
byte digit1Array[8][6];
byte digit2Array[8][6];
// Array to index what digit corresponds to what array
byte (*digitArrays[])[6] = {zero, one, two, three, four, five, six, seven, eight, nine};
// Ensure digit1 and digit2 are within the valid range
if (digit1 >= 0 && digit1 <= 9 && digit2 >= 0 && digit2 <= 9) {
// Copy the digit arrays to digit1Array and digit2Array
byte digit1Array[8][6];
byte digit2Array[8][6];
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 6; ++j) {
digit1Array[i][j] = digitArrays[digit1][i][j];
digit2Array[i][j] = digitArrays[digit2][i][j];
}
}
combineDigits(result, digit1Array, digit2Array);
matrix.renderBitmap(result, 8, 12);
}
}
lastTemperature = temperature;
}
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
matrix.begin();
}
void loop() {
float h = dht.readHumidity();
int t = dht.readTemperature(true); // The true parameter passed is to read as Fahrenheit, the default is Celsius
// Display the temperature on the matrix
displayTemperature(t);
Serial.println(t);
delay(2000);
}