Control several Digital Outputs based from Analog Inputs

Hello Everybody,

As a newbie in Arduino projects and also in programming I want to ask for your help regarding to a current project that I’m working on. Situation is as follow:

I’m planing to build a laser counter system for tires where If they pass through a certain path they will be counted. My system contains Arduino Leonardo, 5 Laser pointers, 5 LDRs and a PLC. Lasers send a ligh beam directly to LDRs. At the moment when object passess through then I have no light emitted to LDR so I gather a resistance change for that fraction of time.So I want to set my configuration with LDRs such that I can activate an digital output corresponding to a certain input to PLC. But now my problem is that I can’t take all LDRs state at once and therefore I can’t trigger all digital outputs to arduino. At the moment I have modified a simple program from some pre-tested source codes available on internet but still I can only see the reaction for only on LDR in time I cannot make it to monitor 5 AI and activate 5 DO to send input signals to PLC.

this is my code that I modified and trying to monitor outputs (not complete).

// LDR pin connections
// name = analogpin;

int LDR = 0;
int LDR1 = 2;
int LDR2 = 3;
int LDR3 = 4;
int LDR4 = 5;

int relay=13;
int relay2=12;
int relay3=11;
int relay4=10;
int relay5=9;

// initialize the serial port
// and declare inputs and outputs
void setup()
{
Serial.begin(9600);

}

void loop()
{

int sensorValue = analogRead(LDR);
int sensorValue2 = analogRead(LDR1);
int sensorValue3 = analogRead(LDR2);
int sensorValue4 = analogRead(LDR3);
int sensorValue5 = analogRead(LDR4);

reading =analogRead(LDR4);
if(reading<50)
digitalWrite(relay4, HIGH);
}
else {
digitalWrite(relay4, LOW);

}

}

I would appreciate a lot if somebody could help me to develop this code or some suggestions regarding to coding part (what kind of libraries would be suitable for me at this case).

Are LDR’s fast enough for this ? They are very slow.

What you want to do requires a number of things. For example a table with the pin numbers, and using millis() to keep the relay activated for a certain amount of time. And you need better names for the variables.

You could start by just by only activating the relay according to the LDR.

void loop()
{
  int sensorValue = analogRead(LDR);
  int sensorValue2 = analogRead(LDR1);
  int sensorValue3 = analogRead(LDR2);
  int sensorValue4 = analogRead(LDR3);
  int sensorValue5 = analogRead(LDR4);
 
  if(sensorValue < 50)
    digitalWrite(relay, HIGH);
  else
    digitalWrite(relay, LOW);

  if(sensorValue2 < 50)
    digitalWrite(relay2, HIGH);
  else
    digitalWrite(relay2, LOW);

  if(sensorValue3 < 50)
    digitalWrite(relay3, HIGH);
  else
    digitalWrite(relay3, LOW);

  if(sensorValue4 < 50)
    digitalWrite(relay4, HIGH);
  else
    digitalWrite(relay4, LOW);
}

Thank you for your reply!, I tested the system and although LDRs have some delay they were fast enough for my project. I will make changes to my program based on your suggestions and I will see.

Thanks again!

You have not set your output pins as OUTPUTs.

void setup()
{ 
   Serial.begin(9600);
   pinMode(relay,OUTPUT);
   pinMode(relay2,OUTPUT);
   pinMode(relay3,OUTPUT);
   pinMode(relay4,OUTPUT);
   pinMode(relay5,OUTPUT);
}

Why do you think that you have to switch all relays at exactly the same time? Relays are slow devices, compared to the Arduino clock frequency.

I’d put statements like this into loop()
digitalWrite(relayX, analogRead(sensorX) < 50);
with copy/paste and X replaced by the actual sensor/relay number.

Thank you all guys for your suggestions, where based on them now i solved it! everything works fine!