SevSegShift issues with other functions

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();
}

Can you post a schematic of your project?
This would make our help much easier.

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.

How is the Arduino connected to a website? I only see JSON getting sent to the serial port.

There is this, for one thing.

Also this

Just via a usb-c cable and paired in the browser

It's not that Im seeing nothing, it just lights up 2 or 3 segments and nothing else, it doesn't respond to any input at all.

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

Are you using UNO?
Are you using 74HC595?

I use an arduino micro pro and 2 74HC595 shift registers

Even without the delay it doesnt work.

This is the same setup with the uno, I however use the micro pro where I have the pins connected to 10, 16 and 14. SevSegShift/examples/SevSegShift_Counter/SevSegShift.png at ShiftRegister · bridystone/SevSegShift · GitHub

Your project in Wokwi simulation:

The example at Github uses "common anode", but you have a "common cathode" display ?

This does not work:

while (!Serial) continue;

Please use:

while (!Serial) delay(10);

This function waits up to 1 second:

Serial.readStringUntil('\n');

You have to find another way.

These three lines interfere with the display:

  DynamicJsonDocument doc(JSON_DOCUMENT_SIZE);
  doc["sensor"] = "joystick";
  serializeJson(doc, Serial);

I assume that it is a memory problem, but I don't know the right way to use the JSON library.

Creating a JSON string on a Arduino Uno seems wrong, it is for a ESP32. Can you create the JSON string on the computer ?

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 I indeed have a common cathode clock!

In the function sendingJsonData(), if I do this:

  static JsonDocument doc;
  doc["sensor"] = "joystick";
  serializeJson(doc, Serial);

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().

For example:

  // JSON sending logic
  Serial.print("{\"sensor\":\"joystick\",\"data\":[");
  Serial.print(sensorValueX);
  Serial.print(",");
  Serial.print(sensorValueY);
  Serial.print(",");
  int z = sensorValueSwitch != 0 ? 0 : 1;
  Serial.print(z);
  Serial.print("]}");
  Serial.println();

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.

To make things easier and avoid problems with the display, I recommend
using the display with the CI TM1637.
Datasheet_TM1637.pdf (688.1 KB)

Or even the assembled module:
image

1 Like

Yeah, I would if I could. At the moment I'm restricted to use other hardware outside of the things in my starterkit.

I'll check it out! Thanks for all the help

Is it your school assignment?

Yes, they gave us the starterkit without specific information or how-to's to make something of it. So the teachers aren't any help either.