Having had issues where the amount of pulses received when more than one sensor is covered, I have completely rewritten the code. It doesn't work.
What I'm trying to do here is have it so that should one LDR be detected as being sat on, the arduino scans the remainder, to see if any other LDR is sat on, and if it is, the arduino scans the remainder, to see if any other LDR is sat on... and so on.
At each stage a separate pulse is emitted so that it confirms that an additional seat has been taken.
I'm not really sure why this isn't working, looking at my code, but it seems to me that it probably has something to do with my definition of i, j, k etc, because now when I look at the serial monitor, I only get printed out the value of A0 (the first LDR) as my value of now*. What I had hoped is that making it so that j could not equal i still let i use all the values, but it knows that if j can't equal i then i can't equal j. I think if this problem is resolved it will most likely sort out my other problem, because the code is already considerably slower to the point where it ought to register each 'sitting'.*
Please help if you can!
```
*const byte numberOfSensors = 10;
int ldr_pins[numberOfSensors] = {
A0, A1, A2, A3, A4, A5, A6, A7, A8, A9};
int now[numberOfSensors] = {
0,0,0,0,0,0,0,0,0,0};
int then[numberOfSensors] = {
1000,1000,1000,1000,1000,1000,1000,1000,1000,1000};
int counterPin = A10; //Sets Analog 10 to Counter
const int THRESH = 200; //Sets threshold value to 200
void setup(){
pinMode(counterPin, OUTPUT); //Sets Counter to OUTPUT
pinMode(13, OUTPUT); //Sets Pin 13 the LED to Output
Serial.begin(9600);
}
void loop(){
int flag0 = false;
int flag1 = false;
int flag2 = false;
int flag3 = false;
int flag4 = false;
int flag5 = false;
int flag6 = false;
int flag7 = false;
int flag8 = false;
for (int i = 0; i < numberOfSensors; i++){
now[i] = analogRead(ldr_pins[i]);
for (int j = 0; j < numberOfSensors != i; j++) {
now[j] = analogRead(ldr_pins[j]);
for (int k = 0; k < numberOfSensors != i != j; k++) {
now[k] = analogRead(ldr_pins[k]);
for (int l = 0; l < numberOfSensors != i != j !=k; l++) {
now[l] = analogRead(ldr_pins[l]);
for (int m = 0; m < numberOfSensors != i != j !=k !=l; m++) {
now[m] = analogRead(ldr_pins[m]);
for (int n = 0; n < numberOfSensors != i != j !=k !=l !=m; n++) {
now[n] = analogRead(ldr_pins[n]);
for (int o = 0; o < numberOfSensors != i != j !=k !=l !=m !=n; o++) {
now[o] = analogRead(ldr_pins[o]);
for (int p = 0; p < numberOfSensors != i != j !=k !=l !=m !=n !=o; p++) {
now[p] = analogRead(ldr_pins[p]);
for (int q = 0; q < numberOfSensors != i != j !=k !=l !=m !=n !=o !=p; q++) {
now[q] = analogRead(ldr_pins[q]);
for (int r = 0; r < numberOfSensors != i != j !=k !=l !=m !=n !=o !=p !=q; r++) {
now[r] = analogRead(ldr_pins[r]);
Serial.print ("now [i] = ");
Serial.println (now [i]); //debugging
if (now[i] > (then[i] + THRESH) ) {
digitalWrite(counterPin, HIGH);
digitalWrite(13, HIGH);
Serial.println ("Someone sat on the bench."); // more debugging
flag0 = true;
}
else {
digitalWrite(counterPin, LOW);
digitalWrite(13, LOW);
}
then[i] = now[i];
if (flag0 && (now[j] > (then[j] + THRESH))) {
digitalWrite(counterPin, HIGH);
digitalWrite(13, HIGH);
Serial.println ("Someone sat on the bench."); // more debugging
flag1 = true;
}
else {
digitalWrite(counterPin, LOW);
digitalWrite(13, LOW);
}
then[j] = now[j];
if (flag1 && (now[k] > (then[k] + THRESH))) {
digitalWrite(counterPin, HIGH);
digitalWrite(13, HIGH);
Serial.println ("Someone sat on the bench."); // more debugging
flag2 = true;
}
else {
digitalWrite(counterPin, LOW);
digitalWrite(13, LOW);
}
then[k] = now[k];
if (flag2 && (now[l] > (then[l] + THRESH))) {
digitalWrite(counterPin, HIGH);
digitalWrite(13, HIGH);
Serial.println ("Someone sat on the bench."); // more debugging
flag3 = true;
}
else {
digitalWrite(counterPin, LOW);
digitalWrite(13, LOW);
}
then[l] = now[l];
if (flag3 && (now[m] > (then[m] + THRESH))) {
digitalWrite(counterPin, HIGH);
digitalWrite(13, HIGH);
Serial.println ("Someone sat on the bench."); // more debugging
flag4 = true;
}
else {
digitalWrite(counterPin, LOW);
digitalWrite(13, LOW);
}
then[m] = now[m];
if (flag4 && (now[n] > (then[n] + THRESH))) {
digitalWrite(counterPin, HIGH);
digitalWrite(13, HIGH);
Serial.println ("Someone sat on the bench."); // more debugging
flag5 = true;
}
else {
digitalWrite(counterPin, LOW);
digitalWrite(13, LOW);
}
then[n] = now[n];
if (flag5 && (now[o] > (then[o] + THRESH))) {
digitalWrite(counterPin, HIGH);
digitalWrite(13, HIGH);
Serial.println ("Someone sat on the bench."); // more debugging
flag6 = true;
}
else {
digitalWrite(counterPin, LOW);
digitalWrite(13, LOW);
}
then[o] = now[o];
if (flag6 && (now[p] > (then[p] + THRESH))) {
digitalWrite(counterPin, HIGH);
digitalWrite(13, HIGH);
Serial.println ("Someone sat on the bench."); // more debugging
flag7 = true;
}
else {
digitalWrite(counterPin, LOW);
digitalWrite(13, LOW);
}
then[p] = now[p];
if (flag7 && (now[q] > (then[q] + THRESH))) {
digitalWrite(counterPin, HIGH);
digitalWrite(13, HIGH);
Serial.println ("Someone sat on the bench."); // more debugging
flag8 = true;
}
else {
digitalWrite(counterPin, LOW);
digitalWrite(13, LOW);
}
then[q] = now[q];
if (flag8 && (now[r] > (then[r] + THRESH))) {
digitalWrite(counterPin, HIGH);
digitalWrite(13, HIGH);
Serial.println ("Someone sat on the bench."); // more debugging
}
else {
digitalWrite(counterPin, LOW);
digitalWrite(13, LOW);
}
then[r] = now[r];
}
}
}
}
}
}
}
}
}
}
}*
```