# Need Fast Pin Polling and Conditional Code

In my project I have the arduino hooked up to a device that gives a voltage drop when a switch is closed. I`m using two arduinos to monitor 15 switches that could be closed at any given second (each yielding an independant voltage drop). These switches are human controlled but the closing of the switches lasts for a very short period (perhaps a tenth of a second). I need to write some code to this arduino that polls these pins very fast and if it detects a voltage drop -- will trigger a digital pin to go to HIGH output. The conditional argument needs to be such that the analogRead is between 700 and 900 and the reaction of the (reactionary) digital pin to HIGH needs to happen (seemingly) simultaneously. Can someone let me know what the fastest code is for polling the 6 analogs plus 1 digital pin and then allaying the results if the condition is met - super super fast? Note--need not go to Serial. :D

(perhaps a tenth of a second).

Is a very very long time to an arduino, there is nothing you have to do special to achieve this.

good to hear.... that the arduino can handle it.... for some reason it`s not handling it yet....i reckon it`s my code.

can someone give me some suggestions for streamlining the process please? does an array work well? how does one set one up?

also, for a function dependant on the value of an (int) how does one accomplish a mathematical equivalent of less than OR greater than at the same time. ie if (v > 799 or less than 900)

Here`s my Shabby code --- What`s wrong with it? What kind of speed of pin polling can I hope
to expect with this form of code? Is there a way to streamline using Arrays? and for the mathematical expressions of If/then i need to do a if “a” is in between 700 and 900 then.

int aSENS1 = 0; // analog pins 0-5
int aSENS2 = 1;
int aSENS3 = 2;
int aSENS4 = 3;
int aSENS5 = 4;
int aSENS6 = 5;
int dSENS7 = 2; // digital input pins 2
int dOUT1 = 5; // digital output pins 5-11
int dOUT2 = 6;
int dOUT3 = 7;
int dOUT4 = 8;
int dOUT5 = 9;
int dOUT6 = 10;
int dOUT7 = 11;
int p = 0;
int q = 0;
int r = 0;
int s = 0;
int t = 0; // numbers to fiddle with and compare ie // if int (y = 0);
int u = 0;
int v = 0;
int y = 0;
int x = 0;
int z = 0;

void setup()
{
pinMode(2, INPUT); // SETTING PINS TO FUNCTION
pinMode(5, OUTPUT); // ANALOG 0-5 DEFAULT IS INPUT
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);

}

void loop()
{
do
{
delay(500);
} while (z < 400);

x = analogRead(aSENS1); // reads the sensor data, constantly polling
y = analogRead(aSENS2); // looping and refreshing

if (x < 968)
{ digitalWrite(dOUT1, HIGH);
delay(150); //looks at analog sensor, if condition met
digitalWrite(dOUT1, LOW); //turns power to digital pin (next 5 also)
delay(50);
}
else if (y < 968)
{ digitalWrite(dOUT2, HIGH); //2nd y
delay(150);
digitalWrite(dOUT2, LOW);
delay(50);
}
else if (v < 968)
{ digitalWrite(dOUT3, HIGH); // 3rd v
delay(150);
digitalWrite(dOUT3, LOW);
delay(50);
}
else if (u < 968)
{ digitalWrite(dOUT4, HIGH);
delay(150); //4th u
digitalWrite(dOUT4, LOW);
delay(50);
}
else if (t < 968)
{ digitalWrite(dOUT5, HIGH);
delay(150); //5th t
digitalWrite(dOUT5, LOW);
delay(50);
}
else if (s < 968)
{ digitalWrite(dOUT6, HIGH);
delay(150); //6th s
digitalWrite(dOUT6, LOW);
delay(50);
}
else if (r == 1)
{ digitalWrite(dOUT7, HIGH);
delay(150); //change to digital read and output
digitalWrite(dOUT7, LOW);
delay(50);
}

}

What is the forst part of the loop about with the while and the 400 and the 1/2 second delay.

What are your swiches doing to ause a voltage xrop but not ground the input?

Bill, the first part of the code (if z less than 400) is for when the appliance is "off".

Bill, I`m not sure I understand your second question.

how does one accomplish a mathematical equivalent of less than OR greater than at the same time. ie if (v > 799 or less than 900)

I suspect you want to do something if v is between 799 and 900 then:-
if( (v>799) && (v<900) ) { // do it

That says only do it if v is greater than 799 AND v is less than 900

Remember dealy() is dead time the arduino doesn’t do anything in that time so you are cutting off the chance of reading anything. The delay(500) after the read cuts out everything for half a second, you don’t want it.
Next time you post code do it between the brackets that pop up with the # icon.

The second question was asking about how you wired it up.

What`s wrong with it?

You cross-posted, and you didn't put it in a code box in either.

the first part of the code (if z less than 400) is for when the appliance is "off".

Wrong.

Gee thanks AWOL. you`re a real helpful sort.

Cross-posted and solved here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1277218786

Cross-posting wastes time - please don't do it.