I am using the matrix portal to drive 2 led matrix screens (64x32 each). The total configuration is 64x64 . On the top screen I print the time (from an NTP server) and on the bottom screen I print some values from Thingspeak (TS).
The time is updated every second whereas the TS values are updated every 15 seconds.
The problem is the by using the matrix.fillscreen(0) to clear the screen for the time update the TS values are also deleted nad are shown again after 15 seconds when they are updated.
Is there anyway to make the matrix.fillscreen command to clear a specific section of the matrix and not all the matrix or maybe there is another command that I'm missing ?
Ps. I'm using the Arduino IDE to compile.
This is the code so far
Code: Select all
#include <Adafruit_Protomatter.h>
#include <Fonts/FreeSerifItalic9pt7b.h> // Large friendly font
#include <Fonts/TomThumb.h> // small font
#include <WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include "secrets.h"
#include "ThingSpeak.h"
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#if defined(_VARIANT_MATRIXPORTAL_M4_) // MatrixPortal M4
uint8_t rgbPins[] = {7, 8, 9, 10, 11, 12};
uint8_t addrPins[] = {17, 18, 19, 20, 21};
uint8_t clockPin = 14;
uint8_t latchPin = 15;
uint8_t oePin = 16;
#endif
Adafruit_Protomatter matrix(
64, // Width of matrix (or matrices, if tiled horizontally)
6, // Bit depth, 1-6
1, rgbPins, // # of matrix chains, array of 6 RGB pins for each
4, addrPins, // # of address pins (height is inferred), array of pins
clockPin, latchPin, oePin, // Other matrix control pins
true, // No double-buffering here (see "doublebuffer" example)
-2); // Row tiling: two rows in "serpentine" path
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
// Weather station channel details
unsigned long weatherStationChannelNumber = SECRET_CH_ID_WEATHER_STATION;
const char* readAPIKey = SECRET_READ_APIKEY; // your ThingSpeak Read API Key
int statusCode = 0;
int field[8] = {1, 2, 3, 4, 5, 6, 7, 8};
int OutTemp; // Field 1
float pressure; // Field 2
int percentHumid; // Field 3
float BattVolt; // Field 4
float PanelVoltage; // Field 5
float PanelCurrent; // Field 6
const char* ntpServer = "pool.ntp.org";
const long utcOffsetInSeconds = 0;
WiFiUDP udp;
NTPClient timeClient(udp, ntpServer, utcOffsetInSeconds);
void setup() {
Serial.begin(115200); // Initialize serial
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
ThingSpeak.begin(client); // Initialize ThingSpeak
// Connect or reconnect to WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using an open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected");
}
timeClient.begin();
timeClient.update();
// Initialize matrix...
ProtomatterStatus status = matrix.begin();
Serial.print("Protomatter begin() status: ");
Serial.println((int)status);
if (status != PROTOMATTER_OK) {
// DO NOT CONTINUE if matrix setup encountered an error.
while (1);
}
matrix.fillScreen(0); // clear the screen
matrix.setCursor(5, 15); // set the cursor position
matrix.setFont(&TomThumb); // Use nice bitmap font
matrix.setTextColor(RED); // set the text color
matrix.show(); // and show it
}
void loop() {
static unsigned long previousTimeMillis = 0;
unsigned long currentTimeMillis = millis();
if (currentTimeMillis - previousTimeMillis >= 1000) {
previousTimeMillis = currentTimeMillis;
timeClient.update();
String formattedTime = timeClient.getFormattedTime();
matrix.fillScreen(0); // clear the screen
matrix.setCursor(0, 10); // set the cursor position
matrix.setFont(&TomThumb); // Use small font
matrix.setTextColor(WHITE); // set the text color
matrix.print(formattedTime); // display the time
matrix.show(); // and show it
}
static unsigned long previousTSUpdateMillis = 0;
unsigned long currentTSUpdateMillis = millis();
if (currentTSUpdateMillis - previousTSUpdateMillis >= 15000) {
previousTSUpdateMillis = currentTSUpdateMillis;
statusCode = ThingSpeak.readMultipleFields(weatherStationChannelNumber, readAPIKey);
if (statusCode == 200) {
// Fetch the stored data
OutTemp = ThingSpeak.getFieldAsInt(field[0]); // Field 1
pressure = ThingSpeak.getFieldAsFloat(field[1]); // Field 2
percentHumid = ThingSpeak.getFieldAsInt(field[2]); // Field 3
BattVolt = ThingSpeak.getFieldAsFloat(field[3]); // Field 4
PanelVoltage = ThingSpeak.getFieldAsFloat(field[4]); // Field 5
PanelCurrent = ThingSpeak.getFieldAsFloat(field[5]); // Field 6
matrix.fillScreen(0); // clear the screen
// Display on the matrix
matrix.setCursor(0, 38); // set the cursor position
matrix.setFont(&TomThumb); // Use small font
matrix.setTextColor(RED); // set the text color
matrix.print("Out Temp: " + String(OutTemp) + "°C"); // print the value
matrix.setCursor(0, 45); // set the cursor position
matrix.setFont(&TomThumb); // Use small font
matrix.setTextColor(GREEN); // set the text color
matrix.print("Humidity: " + String(percentHumid) + " %"); // print the value
matrix.setCursor(0, 52); // set the cursor position
matrix.setFont(&TomThumb); // Use small font
matrix.setTextColor(BLUE); // set the text color
matrix.print("Pressure: " + String(pressure)); // print the value
matrix.setCursor(0, 59); // set the cursor position
matrix.setFont(&TomThumb); // Use small font
matrix.setTextColor(CYAN); // set the text color
matrix.print("Batt. Volt: " + String(BattVolt) + " V"); // print the value
matrix.show(); // and show it
}
else {
Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
}
}
}