adding variables in a loop for the naalog

Hi! I created a simple alarm system using arduino board and it’s working perfectly. however, our professor asked me to upgrade it by adding 2 lasers and 2 LDR. the LDR is connected on the A0 analog. my problem is, can I add another variable to the loop for the input of the LDR in the analog?

const int triggeredLED = 7;
const int triggeredLED2 = 8;
const int RedLED = 4;
const int GreenLED = 5;
const int inputPin = A0; //this is the variable for the LDR which receives the laser light
const int speakerPin = 12;
const int armButton = 6;

boolean isArmed = true;
boolean isTriggered = false;
int buttonVal = 0;
int prev_buttonVal = 0;
int reading = 0;
int threshold = 0;

const int lowrange = 2000;
const int highrange = 4000;

void setup(){

pinMode(triggeredLED, OUTPUT);
pinMode(triggeredLED2, OUTPUT);
pinMode(RedLED, OUTPUT);
pinMode(GreenLED, OUTPUT);
pinMode(armButton, INPUT);
digitalWrite(triggeredLED, HIGH);
delay(500);
digitalWrite(triggeredLED, LOW);

calibrate();
setArmedState();
}

void loop(){

reading = analogRead(inputPin);

int buttonVal = digitalRead(armButton);
if ((buttonVal == HIGH) && (prev_buttonVal == LOW)){
setArmedState();
delay(500);
}

if ((isArmed) && (reading < threshold)){
isTriggered = true;}

if (isTriggered){

for (int i = lowrange; i <= highrange; i++)
{
tone (speakerPin, i, 250);
}

for (int i = highrange; i >= lowrange; i–)
{
tone (speakerPin, i, 250);
}

digitalWrite(triggeredLED, HIGH);
delay(50);
digitalWrite(triggeredLED, LOW);
delay (50);
digitalWrite(triggeredLED2, HIGH);
delay (50);
digitalWrite(triggeredLED2, LOW);
delay (50);
}

delay(20);
}

void setArmedState(){

if (isArmed){
digitalWrite(GreenLED, HIGH);
digitalWrite(RedLED, LOW);
isTriggered = false;
isArmed = false;
} else {
digitalWrite(GreenLED, LOW);
digitalWrite(RedLED, HIGH);
tone(speakerPin, 220, 125);
delay(200);
tone(speakerPin, 196, 250);
isArmed = true;
}
}

void calibrate(){

int sample = 0;
int baseline = 0;
const int min_diff = 200;
const int sensitivity = 50;
int success_count = 0;

digitalWrite(RedLED, LOW);
digitalWrite(GreenLED, LOW);

for (int i=0; i<10; i++){
sample += analogRead(inputPin);
digitalWrite(GreenLED, HIGH);
delay (50);
digitalWrite(GreenLED, LOW);
delay (50);
}

do
{
sample = analogRead(inputPin);

if (sample > baseline + min_diff){
success_count++;
threshold += sample;

digitalWrite(GreenLED, HIGH);
delay (100);
digitalWrite(GreenLED, LOW);
delay (100);
} else {
success_count = 0;
threshold = 0;
}

} while (success_count < 3);

threshold = (threshold/3) - sensitivity;

tone(speakerPin, 196, 250);
delay(200);
tone(speakerPin, 220, 125);
}

can I add another variable to the loop for the input of the LDR in the analog?

int reading = 0;
 reading = analogRead(inputPin);

You already have, so what is your actual problem ?

Your code is not easy to understand because the variable names and comments never mention any lasers anywhere! But if you just want to add a second LDR then connect it to pin A1 (call it LDR2pin) then you can have

  int reading2 = 0;
...
  reading2 = analogRead(LDR2pin);

Then you can do what you like with the readings.

If you meant something else try asking the question again a bit more clearly.

Steve

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Does your laser just output a HIGH and LOW signal?
If so you can configure the analog input pins as digital input pins.

Tom.. :slight_smile: