Why is my input always High-MEGA BOARD

Hi guys,

Re :drag racing timing program :phototransistor sensors

I loaded a sketch into the Mega for this program :

My issue here per my cousin who is helping me, i have "no knowledge," just trying to learn, he is saying once we load the below sketch, pin 48, input, no voltage , but once we assign pin 48 to "start Lane 1, it goes " high ," and we have continuous voltage, given this is a phototransistor sensors , once the car goes over it he is stating it should trigger a "high and low".
But staying high, any suggestion on what to try to get this to work ?

#include <TM1637Display.h>

// Pines para los displays
#define CLK_REACCION_1 3
#define DIO_REACCION_1 2
#define CLK_CARRERA_1 6
#define DIO_CARRERA_1 7
#define CLK_REACCION_2 8
#define DIO_REACCION_2 9
#define CLK_CARRERA_2 11
#define DIO_CARRERA_2 10

// Pines para los sensores LDR
#define LDR_INICIO_CARRIL_1 A5
#define LDR_FINAL_CARRIL_1 A4
#define LDR_INICIO_CARRIL_2 A6
#define LDR_FINAL_CARRIL_2 A7

// Pines para los LEDs
#define LED_AMARILLO_CARRIL_1 A0
#define LED_VERDE_CARRIL_1 13
#define LED_ROJO_CARRIL_1 12
#define LED_AMARILLO_CARRIL_2 A3
#define LED_VERDE_CARRIL_2 A1
#define LED_ROJO_CARRIL_2 A2

// Pines para los pulsadores de Bracket
#define PULSADOR_BRACKET_CARRIL_1 5
#define PULSADOR_BRACKET_CARRIL_2 4

// Displays
TM1637Display displayReaccion1(CLK_REACCION_1, DIO_REACCION_1);
TM1637Display displayCarrera1(CLK_CARRERA_1, DIO_CARRERA_1);
TM1637Display displayReaccion2(CLK_REACCION_2, DIO_REACCION_2);
TM1637Display displayCarrera2(CLK_CARRERA_2, DIO_CARRERA_2);

// Variables globales
unsigned long tiempoInicioCarrera1 = 0;
unsigned long tiempoInicioCarrera2 = 0;
unsigned long tiempoReaccion1 = 0;
unsigned long tiempoReaccion2 = 0;
unsigned long tiempoCarrera1 = 0;
unsigned long tiempoCarrera2 = 0;
int bracket1 = 0;
int bracket2 = 0;
const int ldrInicio1 = A5;
const int ldrInicio2 = A6;
const int ldrFinal1 = A4;
const int ldrFinal2 = A7;
int valor0inicio1;
int valor0inicio2;
int valor0final1;
int valor0final2;
const int umbral = 50;

bool stage1 = false;
bool stage2 = false;
bool reaccionMedida1 = false;
bool reaccionMedida2 = false;
bool ledVerdeEncendido1 = false;
bool ledVerdeEncendido2 = false;
bool carreraFinalizada1 = false;
bool carreraFinalizada2 = false;

// Variables para la secuencia de luces
unsigned long tiempoInicioSecuencia = 0;
unsigned long tiempoDesdeStage1 = 0;  // Variable para contar el tiempo desde que ambos coches están en stage en el carril 1
unsigned long tiempoDesdeStage2 = 0;  // Variable para contar el tiempo desde que ambos coches están en stage en el carril 2
bool secuenciaEnProceso = false;
int estadoSecuencia1 = 0;
int estadoSecuencia2 = 0;

// Debounce variables
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
const unsigned long debounceDelay = 300;

// Variables para el intervalo de seguridad
unsigned long tiempoUltimaLecturaFinal1 = 0;
unsigned long tiempoUltimaLecturaFinal2 = 0;
const unsigned long intervaloSeguridad = 300; // 300 ms de intervalo de seguridad

// Variables para controlar si se debe seguir leyendo los LDR finales e iniciales
bool seguirLeyendoLDRFinal1 = true;
bool seguirLeyendoLDRFinal2 = true;
bool seguirLeyendoLDRInicio1 = true;
bool seguirLeyendoLDRInicio2 = true;

// Variables para detectar salida en falso
bool salidaEnFalsoDetectada1 = false;
bool salidaEnFalsoDetectada2 = false;

// Funciones
void setup() {
  // Inicialización de pines
  pinMode(LDR_INICIO_CARRIL_1, INPUT);
  pinMode(LDR_FINAL_CARRIL_1, INPUT);
  pinMode(LDR_INICIO_CARRIL_2, INPUT);
  pinMode(LDR_FINAL_CARRIL_2, INPUT);

  pinMode(LED_AMARILLO_CARRIL_1, OUTPUT);
  pinMode(LED_VERDE_CARRIL_1, OUTPUT);
  pinMode(LED_ROJO_CARRIL_1, OUTPUT);
  pinMode(LED_AMARILLO_CARRIL_2, OUTPUT);
  pinMode(LED_VERDE_CARRIL_2, OUTPUT);
  pinMode(LED_ROJO_CARRIL_2, OUTPUT);

  pinMode(PULSADOR_BRACKET_CARRIL_1, INPUT_PULLUP);
  pinMode(PULSADOR_BRACKET_CARRIL_2, INPUT_PULLUP);

  // Inicialización de displays
  displayReaccion1.setBrightness(2);
  displayCarrera1.setBrightness(2);
  displayReaccion2.setBrightness(2);
  displayCarrera2.setBrightness(2);

  // Inicialización de displays
  displayReaccion1.clear();
  displayCarrera1.clear();
  displayReaccion2.clear();
  displayCarrera2.clear();

  valor0inicio1 = analogRead(ldrInicio1);
  valor0inicio2 = analogRead(ldrInicio2);
  valor0final1 = analogRead(ldrFinal1);
  valor0final2 = analogRead(ldrFinal2);

  delay(1000);

  // Mostrar "0" al inicio en displays de tiempo de reacción
  displayReaccion1.showNumberDec(0);
  displayReaccion2.showNumberDec(0);
  displayCarrera1.showNumberDec(0);
  displayCarrera2.showNumberDec(0);

  // Manejar el tiempo de configuración del bracket
  manejarBracketConTiempo(60000); // Hasta 1 minuto para ajustar el bracket

  // Asegurarse de que los displays de tiempo de reacción muestren "0000" al finalizar el ajuste del bracket
  displayReaccion1.showNumberDec(0, true);
  displayReaccion2.showNumberDec(0, true);

  // Iniciar la secuencia de luces solo cuando ambos coches estén en stage
  while (!(stage1 && stage2)) {
    lecturasLDR(); // Actualizar el estado de los coches en stage
  }

  // Registrar el tiempo desde que ambos coches están en stage
  tiempoDesdeStage1 = millis();
  tiempoDesdeStage2 = millis();

  // Iniciar la secuencia de luces
  iniciarSecuenciaDeLuces();
}

void manejarBracketConTiempo(unsigned long tiempoMaximo) {
  unsigned long startTime = millis();
  unsigned long lastButtonPressTime = millis();

  while (millis() - startTime < tiempoMaximo) {
    if (millis() - lastButtonPressTime > 5000) {
      break; // Terminar si no se presiona ningún botón en 5 segundos
    }
    if (manejarBracket()) {
      lastButtonPressTime = millis(); // Actualizar el tiempo de la última pulsación de botón
    }
  }
}

bool manejarBracket() {
  bool buttonPressed = false;

  if (digitalRead(PULSADOR_BRACKET_CARRIL_1) == LOW) {
    if ((millis() - lastDebounceTime1) > debounceDelay) {
      bracket1 += 50;
      displayReaccion1.showNumberDec(bracket1, false);
      lastDebounceTime1 = millis();
      buttonPressed = true;
    }
  }

  if (digitalRead(PULSADOR_BRACKET_CARRIL_2) == LOW) {
    if ((millis() - lastDebounceTime2) > debounceDelay) {
      bracket2 += 50;
      displayReaccion2.showNumberDec(bracket2, false);
      lastDebounceTime2 = millis();
      buttonPressed = true;
    }
  }

  return buttonPressed;
}

void iniciarSecuenciaDeLuces() {
  secuenciaEnProceso = true;
  tiempoInicioSecuencia = millis();
}

void secuenciaDeLuces() {
  unsigned long currentMillis = millis();
  unsigned long tiempoSecuencia1 = 1000 + bracket1;
  unsigned long tiempoSecuencia2 = 1000 + bracket2;

  // Detectar salida en falso mientras está la secuencia en proceso
  int valor1inicio1 = analogRead(ldrInicio1);
  int valor1inicio2 = analogRead(ldrInicio2);

  if (!ledVerdeEncendido1 && !carreraFinalizada1 && stage1 && currentMillis - tiempoDesdeStage1 >= 1 && valor1inicio1 >= valor0inicio1) {
    salidaEnFalsoDetectada1 = true;
    digitalWrite(LED_ROJO_CARRIL_1, HIGH);
    digitalWrite(LED_VERDE_CARRIL_2, HIGH);
    digitalWrite(LED_AMARILLO_CARRIL_1, LOW);  // Apagar LED amarillo
    digitalWrite(LED_AMARILLO_CARRIL_2, LOW);  // Apagar LED amarillo
    displayReaccion1.showNumberDec(8888);
    displayCarrera1.showNumberDec(8888);
    displayReaccion2.showNumberDec(0, true);
    displayCarrera2.showNumberDec(0, true);
    return;
  }

  if (!ledVerdeEncendido2 && !carreraFinalizada2 && stage2 && currentMillis - tiempoDesdeStage2 >= 1 && valor1inicio2 >= valor0inicio2) {
    salidaEnFalsoDetectada2 = true;
    digitalWrite(LED_ROJO_CARRIL_2, HIGH);
    digitalWrite(LED_VERDE_CARRIL_1, HIGH);
    digitalWrite(LED_AMARILLO_CARRIL_1, LOW);  // Apagar LED amarillo
    digitalWrite(LED_AMARILLO_CARRIL_2, LOW);  // Apagar LED amarillo
    displayReaccion2.showNumberDec(8888);
    displayCarrera2.showNumberDec(8888);
    displayReaccion1.showNumberDec(0, true);
    displayCarrera1.showNumberDec(0, true);
    return;
  }

  if (secuenciaEnProceso) {
    // Manejar secuencia para carril 1
    switch (estadoSecuencia1) {
      case 0:
        if (currentMillis - tiempoInicioSecuencia >= tiempoSecuencia1) {
          digitalWrite(LED_AMARILLO_CARRIL_1, HIGH);
          estadoSecuencia1 = 1;
        }
        break;
      case 1:
        if (currentMillis - tiempoInicioSecuencia >= tiempoSecuencia1 + 400) {
          digitalWrite(LED_AMARILLO_CARRIL_1, LOW);
          digitalWrite(LED_VERDE_CARRIL_1, HIGH);
          ledVerdeEncendido1 = true;
          tiempoInicioCarrera1 = millis();
          estadoSecuencia1 = 2;
        }
        break;
    }

    // Manejar secuencia para carril 2
    switch (estadoSecuencia2) {
      case 0:
        if (currentMillis - tiempoInicioSecuencia >= tiempoSecuencia2) {
          digitalWrite(LED_AMARILLO_CARRIL_2, HIGH);
          estadoSecuencia2 = 1;
        }
        break;
      case 1:
        if (currentMillis - tiempoInicioSecuencia >= tiempoSecuencia2 + 400) {
          digitalWrite(LED_AMARILLO_CARRIL_2, LOW);
          digitalWrite(LED_VERDE_CARRIL_2, HIGH);
          ledVerdeEncendido2 = true;
          tiempoInicioCarrera2 = millis();
          estadoSecuencia2 = 2;
        }
        break;
    }

    // Si ambas secuencias han terminado
    if (estadoSecuencia1 == 2 && estadoSecuencia2 == 2) {
      secuenciaEnProceso = false;
    }
  }
}

void lecturasLDR() {
    if (salidaEnFalsoDetectada1 || salidaEnFalsoDetectada2) {
        return;  // Si se ha detectado una salida en falso en algún carril, detener todos los procesos
    }

    unsigned long currentMillis = millis();

    int valor1inicio1 = analogRead(ldrInicio1);
    int valor1inicio2 = analogRead(ldrInicio2);
    int valor1final1 = analogRead(ldrFinal1);
    int valor1final2 = analogRead(ldrFinal2);

    // Detectar si los coches están en stage
    if (seguirLeyendoLDRInicio1 && valor1inicio1 < valor0inicio1 - umbral) {
        stage1 = true;
        displayCarrera1.showNumberDec(0, true);
    }

    if (seguirLeyendoLDRInicio2 && valor1inicio2 < valor0inicio2 - umbral) {
        stage2 = true;
        displayCarrera2.showNumberDec(0, true);
    }

    // Detectar salida en falso para carril 1
    if (!ledVerdeEncendido1 && !carreraFinalizada1 && stage1 && currentMillis - tiempoDesdeStage1 >= 1 && valor1inicio1 >= valor0inicio1) {
        salidaEnFalsoDetectada1 = true;
        digitalWrite(LED_ROJO_CARRIL_1, HIGH);
        digitalWrite(LED_VERDE_CARRIL_2, HIGH);
        displayReaccion1.showNumberDec(8888);
        displayCarrera1.showNumberDec(8888);
        displayReaccion2.showNumberDec(0, true);
        displayCarrera2.showNumberDec(0, true);
        return;
    }

    // Detectar salida en falso para carril 2
    if (!ledVerdeEncendido2 && !carreraFinalizada2 && stage2 && currentMillis - tiempoDesdeStage2 >= 1 && valor1inicio2 >= valor0inicio2) {
        salidaEnFalsoDetectada2 = true;
        digitalWrite(LED_ROJO_CARRIL_2, HIGH);
        digitalWrite(LED_VERDE_CARRIL_1, HIGH);
        displayReaccion2.showNumberDec(8888);
        displayCarrera2.showNumberDec(8888);
        displayReaccion1.showNumberDec(0, true);
        displayCarrera1.showNumberDec(0, true);
        return;
    }

    // Detectar tiempo de reacción
    if (ledVerdeEncendido1 && !reaccionMedida1 && valor1inicio1 >= valor0inicio1) {
        tiempoReaccion1 = currentMillis - tiempoInicioCarrera1;
        displayReaccion1.showNumberDecEx(tiempoReaccion1, 0b10000000, true);
        reaccionMedida1 = true;
        seguirLeyendoLDRInicio1 = false;  // Dejar de leer el LDR de inicio
    }

    if (ledVerdeEncendido2 && !reaccionMedida2 && valor1inicio2 >= valor0inicio2) {
        tiempoReaccion2 = currentMillis - tiempoInicioCarrera2;
        displayReaccion2.showNumberDecEx(tiempoReaccion2, 0b10000000, true);
        reaccionMedida2 = true;
        seguirLeyendoLDRInicio2 = false;  // Dejar de leer el LDR de inicio
    }

    // Detectar fin de carrera con intervalo de seguridad
    if (ledVerdeEncendido1 && seguirLeyendoLDRFinal1 && valor1final1 < valor0final1 - umbral) {
        if (currentMillis - tiempoUltimaLecturaFinal1 > intervaloSeguridad) {
            tiempoCarrera1 = currentMillis - tiempoInicioCarrera1;
            displayCarrera1.showNumberDecEx(tiempoCarrera1, 0b10000000, true);
            seguirLeyendoLDRFinal1 = false;
            ledVerdeEncendido1 = false;
            carreraFinalizada1 = true;
            digitalWrite(LED_VERDE_CARRIL_1, LOW);

            // Verificar si es el primer coche en cruzar la meta
            if (!carreraFinalizada2) {
                digitalWrite(LED_VERDE_CARRIL_1, HIGH);  // Encender LED verde del carril 1
            }
        }
        tiempoUltimaLecturaFinal1 = currentMillis;
    }

    if (ledVerdeEncendido2 && seguirLeyendoLDRFinal2 && valor1final2 < valor0final2 - umbral) {
        if (currentMillis - tiempoUltimaLecturaFinal2 > intervaloSeguridad) {
            tiempoCarrera2 = currentMillis - tiempoInicioCarrera2;
            displayCarrera2.showNumberDecEx(tiempoCarrera2, 0b10000000, true);
            seguirLeyendoLDRFinal2 = false;
            ledVerdeEncendido2 = false;
            carreraFinalizada2 = true;
            digitalWrite(LED_VERDE_CARRIL_2, LOW);

            // Verificar si es el primer coche en cruzar la meta
            if (!carreraFinalizada1) {
                digitalWrite(LED_VERDE_CARRIL_2, HIGH);  // Encender LED verde del carril 2
            }
        }
        tiempoUltimaLecturaFinal2 = currentMillis;
    }
}

void loop() {
  if (salidaEnFalsoDetectada1 || salidaEnFalsoDetectada2) {
    return;  // Si se ha detectado una salida en falso en algún carril, detener el loop
  }

  if (secuenciaEnProceso) {
    secuenciaDeLuces();
  } else {
    lecturasLDR();
  }
}


Hi,

Welcome back!

Thank you for posting your code in code tags. :grinning:

Could you please upload a schematic of your circuit?

If your native language is spanish, you may prefer to post in the international section of the forum. If not, can you please reupload your code in English so that we can understand it?

  • This is possibly due to the way the input is wired.

Which robot has generated this code?

Hi Outbackhut,

For clarification, i assume i did something wrong here, posting my code in tags ? Should I post it another way ?

Regarding the circuit, dont have one , would need him to draw one up.

The issue what we are asking regarding the board and the sketch above, even if we take a phototransistor, separate from the circuit and try it , still not working, remaining high.

Meaning : If we take a random sensor, when we go to the board and check the voltage at say pin 48, no power.
Then when we go into the program set up and assign that sensor to pin 48, green light, power is there, lets say 5v.

I am saying given the sketch or how the Arduino should work, I am asking if there is any suggestion to what we are doing wrong, or maybe it is something in the given sketch that is coded wrong..........where we have constant voltage there ?

In the ideal world, (not sure i am saying it right) once the light source hit the sensor, this is where we get the trigger event of the "High and low ."

So that is not happening, we have a continuous voltage at that pin regardless if connected to the circuit or connected directly to the board.

Thanks Larry, will have him take a look at your reply here and get back to you.

No, you did not do something wrong. It was just a compliment.

Well depending on the circuit, it will either go HIGH->LOW->HIGH or LOW->HIGH->LOW when the race car breaks the phototransistor light beam.

if there is no car it may just stay HIGH or LOW

Hi @mikenyc ,

you could try a simple sketch like described here to check the function of your phototransistor:

https://www.digikey.de/en/maker/blogs/2022/how-to-use-a-phototransistor-with-an-a

The sketch you posted uses the abbreviation LDR which corresponds to a "Light Dependend Resistor".

Read here about the differences:

https://www.electricaltechnology.org/2022/11/difference-between-phototransistor-photoresistor-ldr.html

Good luck!
ec2021

In order to answer your question, we really need to know the type of sensor and how you connected it to the board.
You might need a pullup resistor to get your setup working...
It may very well have reversed logic...
But all that we cannot know without the missing info.

How can we help if we cant know how your circuit is connected?

Where, in your code, is pin 48 declared as an input? What is connected to pin 48?

Hi Guys,

So not to confuse ‘helpers” who are trying to assist.

Board : Mega

Sensor : LDR

Circuit, I don’t have one ( but, outside of the circuit, we hooked up the sensor, directly to board also, same issue, pin 46, 48, etc, once “ assigned “ is showing “ High”

Sketch , listed above in post 1

Why pin, 48: even if pin 46 is selected on the board, under the “ digital “ setting in the program, I assume those pins are “ inputs “ given I can select what I want them to do.

Where in the program is pin 48 noted:, it is not, isn’t that the purpose of “ selecting “ an input, meaning once you select pin 46, 48, etc under a Digital setting, shouldn’t that pin act as an input.

What is the issue I am asking help on : If I select an input pin, either 46, 47, 48, etc from the Digital selection in the Drag Race Program, “ Prior “ to assigning what I want that input to do, “ Start Lane 1, ” There is no power on the pin ( no voltage, confirmed with a voltage meter).

Once I assign pin, 46, 47, 48, etc….in this case, Pin 48, we are asking for any suggestion that we might be overlooking because once “ assigned “ there is continuous power at that pin.

Larry, your point is noted, said he would look into that.

  • Since your sensor is an LDR, the normal way of using these is with a voltage divider and an analog input.
    You will read a value between 0-1023, and use this number in your code.

  • We can connect the LDR to the Arduino in one of two ways as seen below.




  • For volunteers to know what is happening in you project, we need to see your complete schematic.

If you cannot make a schematic, make a picture...

  • Do both :sunglasses:

That is a circuit, and therefore you can make a schematic of said circuit.

Got it, asking him now, unlike me who work at desk, he is in the field working....said he will get a pic to me shortly !

Will have him take a look at your post on the LDR, just note for simplicity sake, we tried phototransistors , Photoresistor , and other sensors....outside of the circuit.. just to try if we got that trigger high and low.....

But, his point from an electronic point is that it is pointless what sensors, there is voltage there....but not to beat a dead horse, will ask him to stop what he is doing to get it posted shortly.

Which way did you connect them?

Please see attached