I am working on a large scale art installation which requires nearly all of the Arduino Mega's digital pins to be in use.
One half of the installation is 25 LDRs. I wish to read these not via analog pins but rather using the RCTime method.
I understand the concept of RCTime for one sensor alone (code from tutorial below), but how could I code, using possibly a for loop, the reading of 25 different LDRs without a massive slow down with RCTime?
int sensorPin = 4; // 220 or 1k resistor connected to this pin
long result = 0;
void setup() // run once, when the sketch starts
{
Serial.begin(9600);
Serial.println("start"); // a personal quirk
}
void loop() // run over and over again
{
Serial.println( RCtime(sensorPin) );
delay(10);
}
long RCtime(int sensPin){
long result = 0;
pinMode(sensPin, OUTPUT); // make pin OUTPUT
digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
delay(1); // wait a ms to make sure cap is discharged
pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low
digitalWrite(sensPin, LOW); // turn pullups off - or it won't work
while(digitalRead(sensPin)){ // wait for pin to go low
result++;
}
return result; // report results
}
I wish to read these not via analog pins but rather using the RCTime method.
I understand the concept of RCTime for one sensor alone (code from tutorial below), but how could I code, using possibly a for loop, the reading of 25 different LDRs without a massive slow down with RCTime?
Please explain what this means. RCTime appears to wait for an RC circuit to charge up, and reports how long that took. If you repeat that 25 times, it will, of course, take 25 times as long.
Hi PaulS,
I wish to use the RCTime method 25 times across 25 individual digital pins. That is to say, one simple RCTime circuit (capacitor + resistor) per digital pin. The tricky part is how do I code the 25 separate pins to all perform individual readings of the 25 digital pin values?
First, charge all 25 pins (set them all high). Wait a couple of mS.
Then, loop setting them all low, and noting the time with micros() (in an array).
Then loop, quickly testing each one in sequence to see if it has gone low yet, and if so remember when that happened. When all are low your array will have the times, per pin, basically in the time it takes for the longest one to discharge.
There would be a smallish error amount introduced due to the time to check all 25 pins. If you used the "fast digital read" method you could probably keep that pretty tight.
I suppose the error amount depends on how long they take to discharge, compared to the loop time.