Ein Beispiel wie man den LedController nutzen kann um eine Temperatur- und Luftfeuchtigkeitsanzeige mit 7-Segment Anzeigen zu erstellen.
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 17 // Digital pin vebunden mit dem DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
LedController<1,2> control = LedController<1,2>();
#define CS_BOTTOM 16
#define CS_TOP 22
const unsigned int NUMBER_OF_DIGITS = 4;
#define SMOOTHING_BUFFER_SIZE 10
float getRawTemp(){
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
return -1000;
}
return event.temperature;
}
float getRawHumid(){
sensors_event_t event;
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
return -1;
}
return event.relative_humidity;
}
float getTemp(){
static float last_temps[SMOOTHING_BUFFER_SIZE];
static bool firstCall = true;
float new_temp = getRawTemp();
float sum = new_temp;
if(new_temp < -370.0){
sum = 0;
for(unsigned int i = 0; i < SMOOTHING_BUFFER_SIZE;i++){
sum += last_temps[i];
}
}else{
for(unsigned int i = 0; i < SMOOTHING_BUFFER_SIZE-1;i++){
last_temps[i] = firstCall ? new_temp : last_temps[i+1];
sum += last_temps[i];
}
last_temps[SMOOTHING_BUFFER_SIZE-1] = new_temp;
if(firstCall){firstCall = false;};
}
return sum/SMOOTHING_BUFFER_SIZE;
}
float getHumidity(){
static float last_humids[SMOOTHING_BUFFER_SIZE];
static bool firstCall = true;
float new_humid = getRawHumid();
float sum = new_humid;
if(new_humid < 0){
sum = 0;
for(unsigned int i = 0; i < SMOOTHING_BUFFER_SIZE;i++){
sum += last_humids[i];
}
}else{
for(unsigned int i = 0; i < SMOOTHING_BUFFER_SIZE-1;i++){
last_humids[i] = firstCall ? new_humid : last_humids[i+1];
sum += last_humids[i];
}
last_humids[SMOOTHING_BUFFER_SIZE-1] = new_humid;
if(firstCall){firstCall = false;};
}
return sum/SMOOTHING_BUFFER_SIZE;
}
void displayFloat(float value, unsigned int row = 0, unsigned int decimalPlaces = 1,unsigned int digitOffset = 0){
unsigned int total_length = NUMBER_OF_DIGITS;
if(NUMBER_OF_DIGITS<decimalPlaces){return;};
if(value < 0){
control.setChar(row,total_length-1+digitOffset,'-',false);
total_length--;
};
for(unsigned int i = 0; i < decimalPlaces; i ++){
value*=10.0f;
}
unsigned int v = (unsigned int) (value < 0 ? -value : value);
for (unsigned int i = 0; i < total_length;i++){
control.setDigit(row,i+digitOffset,v%10,i == decimalPlaces);
v/=10;
}
}
void printSensorInfo(){
sensor_t sensor;
dht.temperature().getSensor(&sensor);
Serial.println(F("------------------------------------"));
Serial.println(F("Temperature Sensor"));
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
Serial.println(F("------------------------------------"));
dht.humidity().getSensor(&sensor);
Serial.println(F("Humidity Sensor"));
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
Serial.println(F("------------------------------------"));
}
void setup() {
Serial.begin(19200);
dht.begin();
sensor_t sensor;
dht.humidity().getSensor(&sensor);
delayMS = sensor.min_delay / 1000;
controller_configuration<1,2> conf;
conf.SPI_CS = 0;
conf.virtual_multi_row = false;
conf.row_SPI_CS[0] = CS_BOTTOM;
conf.row_SPI_CS[1] = CS_TOP;
conf.useHardwareSpi = true;
conf.debug_output = true;
conf.spiTransferSpeed = 600000;
control.init(conf);
control.setIntensity(0);
for(unsigned int i = 0; i < 8;i++){
control.setRow(0,i,0x00);
control.setRow(1,i,0x00);
}
}
void loop() {
delay(delayMS);
auto temp = getTemp();
Serial.print(F("Temperature: "));
Serial.println(temp);
displayFloat(temp,1,1);
control.setChar(1,7,'t',false);
auto humid = getHumidity();
Serial.print(F("Humidity: "));
Serial.println(humid);
displayFloat(humid,0,1);
control.setChar(0,7,'H',false);
}