Problem when showing srf05 sensor reading on 7 segment displays, common cathode

Hello, good afternoon friends, I am in trouble since I am carrying out a project in which I must show the reading of an srf05 sensor on 3 7-segment displays, and what happens is that the sensor already measures but the desired number is not maintained. see, that is, it shows the same number 3 times, sweeping the 3 displays and I would like to show the reading, if for example it is 739, I can keep it, this is the code I am using

escribe o pega el código aquí
```#include <NewPing.h>

// Definiciones de pines y variables
const int pinTransistor1 = 8;
const int pinTransistor2 = 9;
const int pinTransistor3 = 10; // Asegúrate de que A1 sea un pin digital válido
const int triggerPin = 11;     // Pin de activación del sensor SRF05
const int echoPin = 12;        // Pin de recepción del sensor SRF05

NewPing sonar(triggerPin, echoPin);

// 7-seg array
int sevenSgmnt[10][4] = {
  {0, 0, 0, 0}, // 0 LSB-->
  {0, 0, 0, 1}, // 1
  {0, 0, 1, 0}, // 2
  {0, 0, 1, 1}, // 3
  {0, 1, 0, 0}, // 4
  {0, 1, 0, 1}, // 5
  {0, 1, 1, 0}, // 6
  {0, 1, 1, 1}, // 7
  {1, 0, 0, 0}, // 8
  {1, 0, 0, 1}  // 9
};

// Tiempo de visualización en milisegundos
int t_display = 50;

// Prototipos de funciones
void sevenSgmntwrite(int digit);
void activateDisplays(int digit);

void setup() {
  // Configuración de pines
  pinMode(pinTransistor1, OUTPUT);
  pinMode(pinTransistor2, OUTPUT);
  pinMode(pinTransistor3, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Inicialización de comunicación serial
  Serial.begin(9600);
}

void loop() {
  // Medir la distancia con el sensor SRF05
  int distance = sonar.ping_cm();

  // Convertir la distancia a dígitos individuales
  int digit1 = distance / 100;       // Dígito de las centenas
  int digit2 = (distance / 10) % 10; // Dígito de las decenas
  int digit3 = distance % 10;        // Dígito de las unidades

  // Imprimir la distancia en el puerto serial
  Serial.println(distance);

  // Activar los displays
  activateDisplays(digit1);
  delay(t_display);

  activateDisplays(digit2);
  delay(t_display);

  activateDisplays(digit3);
  delay(t_display);
}

void sevenSgmntwrite(int digit) {
  digitalWrite(4, sevenSgmnt[digit][0]);
  digitalWrite(5, sevenSgmnt[digit][1]);
  digitalWrite(6, sevenSgmnt[digit][2]);
  digitalWrite(7, sevenSgmnt[digit][3]);
}

void activateDisplays(int digit) {
  sevenSgmntwrite(digit);
  digitalWrite(pinTransistor1, HIGH);
  delay(t_display);
  digitalWrite(pinTransistor1, LOW);
  delay(10);

  sevenSgmntwrite(digit);
  digitalWrite(pinTransistor2, HIGH);
  delay(t_display);
  digitalWrite(pinTransistor2, LOW);
  delay(10);

  sevenSgmntwrite(digit);
  digitalWrite(pinTransistor3, HIGH);
  delay(t_display);
  digitalWrite(pinTransistor3, LOW);
  delay(10);
}

please help, I appreciate it

I suggest you use the SevSeg library. It contains code that is tested and known to work correctly. If you use this, it will mean that one less part of your code needs to work correctly with the other parts, increasing the possibility that the whole code will work.

Thankyou for using code tags. But please take more care when using them, because "escribe o pega el código aquí" and "please help, I appreciate it" is not code.

It's fine, but add it and what should I modify the code?

Read and understand the example code that comes with the SevSeg library. Use this as an example of how to make your own code.

1 Like

ok, thanks you

Sorry, but I'm not that expert in Arduino and I can't understand those examples very well. Could you help me with the code I'm showing to see what can be modified?

Don't begin with the code you have posted. Begin with the example code from the SevSeg library. When you have that working, adapt it to read your sensor and display the value.

1 Like

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