/*
7 segment with serial input
https://forum.arduino.cc/t/serial-input-basics-updated/382007
*/
#include "SevSeg.h"
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
SevSeg sevseg; // Instantiate a seven segment controller object
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
} else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
sevseg.setChars(receivedChars);
newData = false;
}
}
void setup() {
Serial.begin(9600);
byte numDigits = 4;
byte digitPins[] = {2, 5, 6, 8};
byte segmentPins[] = {3, 7, 10, 12, 13, 4, 9, 11};
bool resistorsOnSegments = true; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_ANODE; // See README.md for options
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
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
// Set initial display text
sevseg.setChars("----");
Serial.println("Ready");
}
void loop() {
recvWithEndMarker();
showNewData();
// Must run frequently
sevseg.refreshDisplay();
}