when I use the serial.readstring() and copy to char my 4 digit displays is incorrect. The char value is pre-set and works fine but when I enter a new number the number is not displaying correctly. My goal is to be able to create a manually entered number with a letter.
#include <SevSegShift.h>
//https://arduino.stackexchange.com/questions/79739/74hc595-to-4-digit-7-segment-using-sevsegshift-library
#define SHIFT_PIN_SHCP 6
#define SHIFT_PIN_STCP 5
#define SHIFT_PIN_DS 4
String receivedChar;
char displaystring;
char settings[50] = "32p"; ;
boolean newData = false;
SevSegShift sevseg(SHIFT_PIN_DS, SHIFT_PIN_SHCP, SHIFT_PIN_STCP, 1, true);
void setup() {
byte numDigits = 4;
byte digitPins[] = {12, 11, 10, 9}; // These are the PINS of the ** Arduino **
byte segmentPins[] = {0, 1, 2, 3, 4, 5, 6, 7}; // these are the PINs of the ** Shift register **
bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_CATHODE; // 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(100);
Serial.begin(9600);
}
void loop() {
recvOneChar();
showNewData();
sevseg.setChars(settings);
sevseg.refreshDisplay(); // Must run repeatedly; don't use blocking code (ex: delay()) in the loop() function or this won't work right
}
void recvOneChar() {
if (Serial.available() > 0) {
receivedChar = Serial.readString();
// receivedChar.replace("\n","");
newData = true;
}
}
void showNewData() {
if (newData == true) {
strcpy(settings, receivedChar.c_str());
newData = false;
}
}
When I upload the code the number 32p displays on the 7 segment display. However, if I then open Serial Monitor and type 32p then click send I get a partial 3 a partial p. Or it will put the correct number but then put a weird character in the last display. If I put a larger number it doesn't even get close. Do you want screen shots?
I found this example and combined it with my code and it works.
#include <SevSegShift.h>
//https://arduino.stackexchange.com/questions/79739/74hc595-to-4-digit-7-segment-using-sevsegshift-library
#define SHIFT_PIN_SHCP 6
#define SHIFT_PIN_STCP 5
#define SHIFT_PIN_DS 4
//String receivedChar;
//char displaystring;
//char settings[50] = "32p"; ;
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
int dataNumber = 0; // new for this version
SevSegShift sevseg(SHIFT_PIN_DS, SHIFT_PIN_SHCP, SHIFT_PIN_STCP, 1, true);
void setup() {
byte numDigits = 4;
byte digitPins[] = {12, 11, 10, 9}; // These are the PINS of the ** Arduino **
byte segmentPins[] = {0, 1, 2, 3, 4, 5, 6, 7}; // these are the PINs of the ** Shift register **
bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_CATHODE; // 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(100);
Serial.begin(9600);
}
void loop() {
// Serial.println(receivedChars);
sevseg.setChars(receivedChars);
sevseg.refreshDisplay(); // Must run repeatedly; don't use blocking code (ex: delay()) in the loop() function or this won't work right
recvWithEndMarker();
showNewNumber();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
if (Serial.available() > 0) {
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 showNewNumber() {
if (newData == true) {
dataNumber = 0; // new for this version
dataNumber = atoi(receivedChars); // new for this version
Serial.print("This just in ... ");
//Serial.println(receivedChars);
Serial.print("Data as Number ... "); // new for this version
Serial.println(dataNumber); // new for this version
newData = false;
}
}