Go Down

Topic: problem with 7 segment and ultrasonic distance sensor (Read 2314 times) previous topic - next topic


hey, I am new for arduino, I am working on the ultrasonic distance sensor display the distance on the a 4 digit 7 segment, but the 7 segment keeping blinking, I am thinking it is because the delay(50) that I add for the distance sensor, here is the code,
Code: [Select]

#include <NewPing.h>
#include <SevSeg.h>
SevSeg s;

long distance;

#define TRIGGER_PIN  53 
#define ECHO_PIN     51 
#define MAX_DISTANCE 200


void setup() {
void loop() {
// noInterrupts(); 
        //  interrupts();
  unsigned int uS = sonar.ping();
  distance= uS/US_ROUNDTRIP_CM;

I use library NewPing for distance sensor and SevSeg for 7 segment, is there any way to only delay sensor not display? I tried to use //noInterrupts();  and interrupts, but I think it just cancel my delay there, is anyone can help me out?
thank you very much and apologize for my bad English.


The problem is the SevSeg::PrintOutput() code. He turns the segments on, waits awhile, then turns them off before returning. The wait seems to be to control brightness. Your delay is after all the digits are turned off.

I would use a different library, or change his library to remove the brightness feature so you can have whatever delays you want in your code.


void SevSeg::PrintOutput(){
  int WaitTime = map(brightness,0,100,1,2000); //Calculate the delay once
  for (byte seg=0;seg<8;seg++) {
    //Turn the relevant segment on

    //For each digit, turn relevant digits on
    for (byte digit=0;digit<NUM_DIGITS;digit++){
      if (lights[digit][seg]==1) {
      //delay(200); //Uncomment this to see it in slow motion
    //Turn all digits off
    for (byte digit=0;digit<NUM_DIGITS;digit++){

    //Turn the relevant segment off
Do not IM me. I will not respond. Ask questions in the forum.


thank you for your help, I will try your code later and post the result, but when I delete delay(50), it will stop blinking and when I change the value of delay(), it will blinking faster or slower, thank you again.


That isn't my code. It's the code from the library, and is the reason you are having a problem.

You need to change the library and remove the part at the end where the library turns off the LCD segments.

Right now, after you return from the library, the LCD segments are turned off. You then delay. This is why it doesn't work. You need to change the library (or use a different library) so that the LCD segments are not turned off. After that, you can use delay() in your code, as the delay will be happening while the LCD segments are on.
Do not IM me. I will not respond. Ask questions in the forum.

Go Up