I need to speed this code up...can anyone help?

I am polling pins and need to recognize an event that could happen at any time and could last for as little as 10ms. I have written this code, but am not sure what kind of “speed of pin polling” i could expect with it. any ideas? I`d like to speed it up as much as possible and suspect arrays might be the way to go…is that true? How would i do that?

Also, for the mathematical comparative functions of “ie. if (x > 3)” how
can i write an expression that looks to see if the variable is between two numbers. say, within the range of 700-900 and then does an action if it is?

Here`s the code. Thanks to all who put some time and effort into helping me out. I really appreciate it.

Heres my Shabby code --- Whats 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
{
z = analogRead(aSENS1);
delay(500);
} while (z < 400);

x = analogRead(aSENS1); // reads the sensor data, constantly polling
y = analogRead(aSENS2); // looping and refreshing
v = analogRead(aSENS3);
u = analogRead(aSENS4);
t = analogRead(aSENS5);
s = analogRead(aSENS6);
r = digitalRead(dSENS7);

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);
}

}

Well, the number of delays you have in there certainly isn't helping matters...

ya, youre right...maybe...well....but the delays are dependant upon the value of the ints. my thinking is that the arduino will continue polling the pins if the conditions are not met. i need to have the arduino constantly polling the pins for a change in value. it will simply pass these delays if the pin values don`t meet the conditions right? or am i wrong in thinking that? will these delays actually slow up the polling of the pins if the conditions of the IF statements are not met?

What`s wrong with it?

It isn’t posted in a Code box?

do
{
z = analogRead(aSENS1);
delay(500);        
 } while (z < 400);

That one’s a killer. Gets you every time.

Once you've trimmed all the delay()s you could try to reduce the ADC prescaler, which will make analogRead() return faster from its busy-wait loop:

sbi(ADCSRA, ADPS2);
sbi(ADCSRA, ADPS1);
cbi(ADCSRA, ADPS0);  // set ADC prescaler to 64 (default 128)

if 8 bits precision is enough, you can go even lower.

I’d like a better explanation on what you’re trying to accomplish as it’s not readily apparent from the posted code.

Anyway to answer one of your questions.

Also, for the mathematical comparative functions of “ie. if (x > 3)” how
can i write an expression that looks to see if the variable is between two numbers. say, within the range of 700-900 and then does an action if it is?

inline bool trigger(int valueMin, int valueMax, int value) {
      return ((value >= valueMin) ? ((value <= valueMax) ? true : false) : false);
}

Place this routine somewhere above your code.

if ( trigger(700, 900, variable))
{
    // value of 'variable' is greater than, or equal to, 700 but less than, or equal to, 900
    ... do something useful ...
}

By taking out the two delays from every if statement, the loop will run faster. As others have said, it would help if you could let us know what your trying to accomplish. Someone may be able to offer a better way to handle it. Here is an example of speeding up your loop.

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
{
z = analogRead(aSENS1);
delay(500);        
 } while (z < 400);

x = analogRead(aSENS1);       // reads the sensor data, constantly polling
y = analogRead(aSENS2);       // looping and refreshing
v = analogRead(aSENS3);
u = analogRead(aSENS4);
t = analogRead(aSENS5);
s = analogRead(aSENS6);
r = digitalRead(dSENS7);

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

}

This will ensure that and dOUT is on for at least 150 ms. But you may miss an event during that 150 ms. If every if statement evaluated true, your loop would be in delay for 1400 ms every loop.

You are looking for an event that could happen on any one of 7 pins and only last 10 ms. Your dSENS7 could be handled with interupt instead. This would catch that event at almost any time. The rest of your analog reads could be sped up with the information by drhex.