Go Down

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

wsybok

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

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  Serial.begin(9600);
  s.Begin(0,13,12,11,10,9,8,7,6,5,4,3,2);
  s.Brightness(90);
}
void loop() {
// noInterrupts(); 
          delay(50);           
        //  interrupts();
  unsigned int uS = sonar.ping();
  distance= uS/US_ROUNDTRIP_CM;
s.PrintOutput();
  s.NewNum(distance,0);
  }

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.


arduinodlb

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
    digitalWrite(SegmentPins[seg],SegOn);

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

    //Turn the relevant segment off
    digitalWrite(SegmentPins[seg],SegOff);
  }
}
Do not IM me. I will not respond. Ask questions in the forum.

wsybok

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.

arduinodlb

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