#include "pin_configuration.h"
#include <SevSeg.h>
#include <Button.h>
SevSeg sevseg;
Button button(PIN_BUTTON);
enum Mode { temperature,
humidity,
current,
modeCount };
Mode mode = temperature;
long lastSensorUpdate = 0;
const int SENSOR_UPDATE_PERIOD = 500; // milli seconds
void setup();
void loop();
Mode evaluateMode();
void displayValue(Mode mode);
void indicateState(Mode mode);
int readTemperature();
int readHumidity();
double readCurrent();
void setup() {
Serial.begin(9600);
delay(1000);
int NUMBER_OF_DIGITS = 3;
bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
bool updateWithDelays = false; // Default 'false' is Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected. Then, you only need to specify 7 segmentPins[]
sevseg.begin(
COMMON_ANODE,
NUMBER_OF_DIGITS,
PINS_7_SEGMENT_DISPLAY_DIGITS,
PINS_7_SEGMENT_DISPLAY_SEGMENTS,
resistorsOnSegments,
updateWithDelays,
leadingZeros,
disableDecPoint
);
button.begin();
pinMode(PIN_TEMPERATURE, INPUT);
pinMode(PIN_HUMIDITY, INPUT);
pinMode(PIN_TEMPERATURE_LED, OUTPUT);
pinMode(PIN_HUMIDITY_LED, OUTPUT);
pinMode(PIN_CURRENT_LED, OUTPUT);
}
void loop() {
sevseg.refreshDisplay();
// updateDisplay();
mode = evaluateMode();
indicateState(mode);
long now = millis();
if (now < lastSensorUpdate + SENSOR_UPDATE_PERIOD) return;
// Serial.print("current mode: ");
// Serial.println(mode);
lastSensorUpdate = now;
displayValue(mode);
}
// determines and returns the current state
Mode evaluateMode() {
// determine the state based on the current state and the button
// if you exprerience unexpected behaviour of the push button
// look into "debouncing"
if (button.pressed()){
mode = static_cast<Mode>((mode + 1) % modeCount);
Serial.print("next mode: ");
Serial.println(mode);
}
return mode;
}
// indicate the current Mode using LEDs
void indicateState(Mode mode) {
// Serial.print("indicate mode ");
// Serial.println(mode);
digitalWrite(PIN_TEMPERATURE_LED, mode == Mode::temperature ? HIGH : LOW);
digitalWrite(PIN_HUMIDITY_LED, mode == Mode::humidity ? HIGH : LOW);
digitalWrite(PIN_CURRENT_LED, mode == Mode::current ? HIGH : LOW);
}
// read the value according to the provided
// mode and show it on the 7 segment display
void displayValue(Mode mode) {
Serial.print("display value for mode ");
Serial.println(mode);
switch (mode) {
case Mode::temperature:
Serial.println(readTemperature());
sevseg.setNumber(readTemperature());
break;
case Mode::humidity:
Serial.println(readHumidity());
sevseg.setNumber(readHumidity());
break;
case Mode::current:
sevseg.setNumberF(readCurrent(), 1);
Serial.println(readCurrent());
break;
default:
Serial.println("Unknown mode");
}
}
// read the ADC value, convert it to the desired unit (°C) and return it
// returns: int temperature in °C
int readTemperature() {
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
int adcValue = analogRead(PIN_TEMPERATURE);
int temperature = (int)(1 / (log(1 / (1023. / adcValue - 1)) / BETA + 1.0 / 298.15) - 273.15);
return temperature;
}
// read the ADC value, calculate the humidity in % (int) and return it
int readHumidity() {
int adcValue = analogRead(PIN_HUMIDITY);
int humidity = (int)(adcValue*100.0/1023);
return humidity;
}
// read the ADC value, calculate the current in mA (double) and return it
double readCurrent() {
int adcValue = analogRead(PIN_CURRENT);
double current = map(adcValue,0,1023,0,200)/10.0; // in mA
return current;
}