LED panel flickering

Hello all. I have a LED panel, nano, ultrasonic sensor. parking system. The problem is when I get to the STOP distance after 30 sec I have the program clear screen. I does that but when the screen is off I have random pixels lighting up every couple seconds. I think it may have to do with the loop still measuring distance, in the serial monitor I can see it is still measuring distance. The question is, is it a code problem with the loop, or maybe need a resistor in line somewhere?
Here is a copy of the code.

/* Author Erik
 *  parking sensor with seediuno nano
 *  Sept. 2022 V1
 */

#include <DMD2.h>
#include <fonts/SystemFont5x7.h>
SoftDMD dmd(1,1);  // DMD controls the entire display
#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <Ultrasonic.h>
#define RANGERPIN A5
Ultrasonic ultrasonic(RANGERPIN);
int counter = 0; // Counter value to check if the object has stopped moving

void setup() 
{
  Serial.begin(9600);
  dmd.setBrightness(255);
  dmd.selectFont(SystemFont5x7);
  dmd.begin();
  dmd.clearScreen();
 }
 
  void loop() 
{
   int inches {0};
   int new_stop_distance {18}; //enter new stop distance here
   inches = ultrasonic.MeasureInInches( );
   inches = inches - new_stop_distance; 
   
   Serial.print(inches);//0~157 in
   Serial.println(" inch");
   delay(250); 
   dmd.clearScreen();
 
  if (inches > 110){
  dmd.drawString(10,4,"OK",7,SystemFont5x7);
 
}

  else if ((inches < 110)&&(inches >1)){
  dmd.drawString(10,4,String(inches),7, SystemFont5x7);
}

  else if (inches <= 0){
  dmd.drawString (5,4,"STOP",7,SystemFont5x7);
  dmd.drawFilledBox (0,0,3,3);
  dmd.drawFilledBox (0,15,3,12);
  dmd.drawFilledBox (28,3,31,0);
  dmd.drawFilledBox (28,12,31,15);

  if (counter >= 30){  
  Serial.println("No movement detected, turning off the lights");
  dmd.clearScreen();

} else {

  counter++;
}

} else {

counter = 0; // Reset counter if there is a movement

}
}

where do you measure those 30 seconds??

you also clearScreen() at every start of the loop so that does not help with the blinking screen...

and when are you resetting the counter ?

if (counter >= 30){ (30 cycles) its to shut off screen if there is no movement.

30 loop cycles does not mean 30 sec... the longest part is probably the delay(250);

I'd suggest you look at how this counter is managed and cleared

your code would be better structured as a state machine where you display something only once until the state changes

ok, I will start to learn about that. Pretty new at all this. Thanks

@captahole I can't now (little window) but you can, always, and should use the

Tools / Auto Format

in the IDE to put your code into a standard form. That makes it easier to read.

Then

Copy for Forum

and paste it here.

It looks like nothing would ever set counter to 0, but hard to say.

It looks like the loop would continue to crank away, and with the screen clear every loop that @J-M-L sees account for flickering?

Later at the big rig a better look, for now know and use auto format until writing code in a good format becomes automatical.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.