I need help with my school project using LEDs, LDRs and time

Hello everyone, I need help with my school project.

In my physics class, we are learning how to use arduino to help us understand physics.

Basically, we have an aluminum bar and we are trying to explain why a neodymium magnet goes down slower than an iron ball inside it.

And I’m supposed to program LEDs to turn on (using LDRs) everytime the ball passes through it and write down the seconds, but I can’t use others hardwares, only the serial monitor.

Hardware: 6 LDRs, 6 LEDs and an aluminium bar

So, here is what I need to know:

1- How can I make serial monitor show every 6 LDRs values in every second?

2- I need to program it to write down the seconds in the serial monitor everytime the ball passes through.

Here is what I have so far, it isn’t right, but it’s a start.

int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
int LED4 = 10;
int LED5 = 9;
int LED6 = 8;

int LDR1 = A0;
int LDR2 = A1;
int LDR3 = A2;
int LDR4 = A3;
int LDR5 = A4;
int LDR6 = A6;

int val1;
int val2;
int val3;
int val4;
int val5;
int val6;

void setup (){
  pinMode (LED1, OUTPUT);
  pinMode (LED2, OUTPUT);
  pinMode (LED3, OUTPUT);
  pinMode (LED4, OUTPUT);
  pinMode (LED5, OUTPUT);
  pinMode (LED6, OUTPUT);
  Serial.begin (9600);
}

void loop(){
  val1 = analogRead (LDR1);
  Serial.println (val1);
  delay (1000);
  if (val1 > 50){
    digitalWrite (LED1, HIGH);
  }
  else if (val1 <= 50){
    digitalWrite (LED1, LOW);
  }

  val2 = analogRead (LDR2);
  Serial.println (val2);
  delay (1000);
  if (val2 > 50){
    digitalWrite (LED2, HIGH);
  }
  else if (val2 <= 50){
    digitalWrite (LED2, LOW);
  }

  val3 = analogRead (LDR3);
  Serial.println (val3);
  delay (1000);
  if (val3 > 50){
    digitalWrite (LED3, HIGH);
  }
  else if (val3 <= 50){
    digitalWrite (LED3, LOW);
  }
  
  val4 = analogRead (LDR4);
  Serial.println (val4);
  delay (1000);
  if (val4 > 50){
    digitalWrite (LED4, HIGH);
  }
  else if (val4 <= 50){
    digitalWrite (LED4, LOW);
  }

   val5 = analogRead (LDR5);
  Serial.println (val5);
  delay (1000);
  if (val5 > 50){
    digitalWrite (LED5, HIGH);
  }
  else if (val5 <= 50){
    digitalWrite (LED5, LOW);
  }

   val6 = analogRead (LDR6);
  Serial.println (val6);
  delay (1000);
  if (val6 > 50){
    digitalWrite (LED6, HIGH);
  }
  else if (val6 <= 50){
    digitalWrite (LED6, LOW);
  }
}

I’m new to programming, that is why I need help. If you can help me, I will be very thankful. :smiley:

Get rid of the delays(). Research arduino state machine.

And do all analogReads of the LDRs right after one another, and all dependent processing on them after it. Then the data would be more synchronous. The current serial, delay and digitalWrite commands introduce major lag. Which you don’t want if you want to detect the moment of passage.

Depending on what additional resistor you have with the LDRs (making up voltage dividers) you may instead use them on digital inputs, which are much quicker to sample but are only on and off. But it is indeed best to first know what the voltage levels are when the LDR/LED gates are blocked or open. It needs to match the digital logic thresholds of the pin inputs.

Thanks so much guys, I fixed it :smiley: