I am trying to run this code. Both loops should run the same thing but on different led's and different sensors. It actually ran the first void loop properly but when i started adding code to do it a second time on different sensors and led's is when I began getting errors.
int threshold = 50;
int ledGreen = 8;
int ledRed = 7;
int ledGreen1 = 12;
int ledRed1 = 13;
int threshold1 = 50;
long randNumber;
boolean sensorTrig = false;
void setup() {
Serial.begin(9600);
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(ledGreen1, OUTPUT);
pinMode(ledRed1, OUTPUT);
}
void loop() {
int sensorValue = analogRead(A0);
//Serial.println(sensorValue);
delay(10);
if (sensorValue < threshold){
if (!sensorTrig) {
randNumber = random(9);
sensorTrig = true;
}
Serial.println(randNumber);
if (randNumber < 5){
digitalWrite(ledGreen, HIGH);
}
else if (randNumber > 4){
digitalWrite(ledRed, HIGH);
}
}
else{
sensorTrig = false;
digitalWrite(ledGreen, LOW);
digitalWrite(ledRed, LOW);
}
}
void loop() {
int sensorValue1 = analogRead(A1);
//Serial.println(sensorValue);
delay(10);
if (sensorValue1 < threshold1){
if (!sensorTrig) {
randNumber = random(9);
sensorTrig = true;
}
Serial.println(randNumber);
if (randNumber < 5){
digitalWrite(ledGreen1, HIGH);
}
else if (randNumber > 4){
digitalWrite(ledRed1, HIGH);
}
}
else{
sensorTrig = false;
digitalWrite(ledGreen1, LOW);
digitalWrite(ledRed1, LOW);
}
}