I'm having issues with the sevsegshift library and my other functions. I want to use the clock while also recieving and sending a Json to my website that is connected to the arduino. The clock only works when the other functions are commented out and for the life in me, I can't figure out why it is not working. Here is the code:
#include <ArduinoJson.h>
#include <SevSegShift.h>
// Clock pins
#define SHIFT_PIN_DS 10
#define SHIFT_PIN_STCP 16
#define SHIFT_PIN_SHCP 14
SevSegShift sevseg(SHIFT_PIN_DS, SHIFT_PIN_SHCP, SHIFT_PIN_STCP); // Instantiate a seven segment controller object
// Joystick pins
#define JOYSTICK_PIN_VRX 18
#define JOYSTICK_PIN_VRY 19
#define JOYSTICK_PIN_SW 20
// Buzzer pin
#define BUZZER_PIN 6
// JSON document size
#define JSON_DOCUMENT_SIZE 256
void setup() {
// Initialization logic
Serial.begin(9600);
while (!Serial) continue;
// Clock setup logic
byte numDigits = 4;
byte digitPins[] = { 8 + 2, 8 + 5, 8 + 6, 2 }; // of ShiftRegister(s) | 8+x (2nd Register)
byte segmentPins[] = { 8 + 3, 8 + 7, 4, 6, 7, 8 + 4, 3, 5 }; // of Shiftregister(s) | 8+x (2nd 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(90);
}
void loop() {
sendingJsonData(); // Send joystick data
updateClock(); // Update clock display
receiveJsonData(); // Receive JSON data if available
}
void updateClock() {
static unsigned long timer = millis();
static int deciSeconds = 10000;
if (millis() - timer >= 100) {
timer += 100;
deciSeconds--; // 100 milliseconds is equal to 1 deciSecond
if (deciSeconds == 0) { // Reset to 0 after counting for 1000 seconds.
deciSeconds = 10000;
}
sevseg.setNumber(deciSeconds, 1);
}
sevseg.refreshDisplay(); // Must run repeatedly
}
void sendingJsonData() {
delay(100);
int sensorValueX = map(analogRead(JOYSTICK_PIN_VRX), 0, 1022, 0, 100);
int sensorValueY = 100 - map(analogRead(JOYSTICK_PIN_VRY), 0, 1022, 0, 100);
int sensorValueSwitch = analogRead(JOYSTICK_PIN_SW);
// JSON sending logic
DynamicJsonDocument doc(JSON_DOCUMENT_SIZE);
doc["sensor"] = "joystick";
doc["data"][0] = sensorValueX;
doc["data"][1] = sensorValueY;
doc["data"][2] = sensorValueSwitch != 0 ? 0 : 1;
serializeJson(doc, Serial);
Serial.println();
}
void receiveJsonData() {
delay(100);
if (Serial.available() > 0) {
String s = Serial.readStringUntil('\n');
StaticJsonDocument<JSON_DOCUMENT_SIZE> doc;
DeserializationError error = deserializeJson(doc, s);
if (error) {
logSerial("JSON parse failed");
return;
}
// Handle parsed JSON data here according to your requirements
}
}
void logSerial(const char* message) {
StaticJsonDocument<JSON_DOCUMENT_SIZE> doc;
doc["sensor"] = "message";
doc["data"] = message;
serializeJson(doc, Serial);
Serial.println();
}
If I'm not mistaken, this library uses the microcontroller to multiplex the data to the seven-segment displays, and for it to work correctly, it is necessary to call refreshDisplay() very often (at least tens of times per second). Since you're using connection to website, I'm guessing this causes quite a delay between each refreshDisplay() call, and therefore you're losing the clock. Do yourself a big favor and just use another method of outputting data to the clock, such as the MAX7219 chip, which takes care of multiplexing the LEDs itself without the involvement of the microcontroller. This will make your life a lot easier.
I think @flashko is correct. The seven segment displays are a very sensible to any delays - and your code contains a some delay(100) statements and other time consuming operations.
To be honest, I would say that working with seven segment is not compatible with anything else in the code. So I recommend you to admit a suggestions stated in #3 post
Holy damn, you remade it yourself! Uhm, its for a schoolproject, it's my first time using an arduino. We're supposed to have a 2 way communication system with a browser (therefore Jsons are being sent from both devices to make it work) I use the arduino pro micro.
and if I remove both delay(100); then it works.
As soon as I add something or change something to those three lines, then the display output crashes.
I tried many thing, but I can not make it work.
I suggest to stop using the JSON library. It does not work.
The function sevseg.refreshDisplay(); needs to run in main section of the loop(), so it runs every time. You might have to reduce the number of times that you send something to the computer, to let the sevseg.refreshDisplay(); run more often.
Search here for "json": https://www.arduinolibraries.info/
There are about 40 or 50 libraries for "json".
If you open the Github of a library, then you can see how many stars it has and if Issues are fixed.
There is probably a small and simple version that will work, but I don't want to spent a lot of time to try them.
I suggest to build the output with your own code or with sprintf().
If the received text is always the same format, then you can write your own code to extract the data or use sscanf().
You could add a link to this topic as comment to your sketch and explain that even others could not make the JSON library work for this small Arduino board.