3461bs-1 only showing one digit at a time

Hi I am brand new to coding and using micontrollers. I have a nano and am trying to make a hit counter that displays to a 4 digit 7 segment display.
Im using the SevSeg library to control the display and im trying to get it to display the counter number.

My problem is it is only showing a single digit either the first digit or the last digit but never the full one. It will sometimes show the full digit but only for a brief second then it disappears and only shows the one or none at all.

Here is my attempted code so far.

* SevSeg Counter Example
 
 Copyright 2020 Dean Reading
 
 This example demonstrates a very simple use of the SevSeg library with a 4
 digit display. It displays a counter that counts up, showing deci-seconds.
 */

#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object

const int knockSensor = A7;  
const int threshold = 1000;
int sensorReading = 0; 
int counter = 0;

void setup() {
  byte numDigits = 4;
  byte digitPins[] = {10,11,12,13};
  byte segmentPins[] = {9,2,3,5,6,8,7,4};
  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);
  Serial.begin(9600);
}

void loop() {
  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);
    if (sensorReading>=threshold){
      counter== counter++;
      Serial.println(counter);
           if (counter==100)
      counter=0;
      sevseg.setNumber(counter);
      sevseg.refreshDisplay();
      
    }
  }

/// END ///

I am completely new to all of this so any help would be greatly appreciated. However don't be rude please, the world is crappy enough don't add to it cause you want to stroke your ego.

After a brief glance, something jumped out at me.

IIRC, the SevSeg library does the digit multiplexing in software?

So the refreshDisplay method must be called often.

Examine your loop() function. I've tidied up the indentation so to make it obvious than the refreshDisplay method is only called when the threshold is exceeded.

void loop() {
   // read the sensor and store it in the variable sensorReading:
   sensorReading = analogRead(knockSensor);
   if( sensorReading >= threshold ) {
      counter == counter++;
      Serial.println(counter);
      if( counter == 100 ) {
         counter = 0;
      }
      sevseg.setNumber(counter);
      sevseg.refreshDisplay(); // <--- this line only executes when the threshold is exceeded
   }
}

That is wrong. The refreshDisplay method must be called EVERY time through the loop in order to do the digit multiplexing. Like so:

void loop() {
   // read the sensor and store it in the variable sensorReading:
   sensorReading = analogRead(knockSensor);
   if( sensorReading >= threshold ) {
      counter == counter++;
      Serial.println(counter);
      if( counter == 100 ) {
         counter = 0;
      }
      sevseg.setNumber(counter);
   }
   sevseg.refreshDisplay(); // <--- this line is executed every time through the loop
}

Also, with such a low baud rate (by today's standards), every time you print the counter value, you're probably going to see glitches on the display. Bump that 9600 up to 115200 and be on the safe side.

1 Like

So i was on the right path just didn't have it lined out properly. Figures lol. I greatly appreciate the help and will try it out.

What does the baud rate do exactly?