I'm working on a project that utilizes an external 24Bit ADC on the SPI bus and 3 pushbuttons intended for menu options. Everything works independently, but there's a significant button response delay when all component functions are combined into one program. From what I gather, since I'm using a delay(); for the SPI, the program stops for a specified amount of time. In that delay window, the processor does not see that I've just pushed a button. I would certainly like to use the SPI without using a delay(). Anyone know how to do that? Is it even possible? Here's my code for the SPI:
void loop() {
int i;
long secs;
float mins;
long analogValue;
long in; // incoming serial 32-bit word
long sum = 0;
for (i=0; i<nsamples; i++) {
in = SpiRead();
in &= 0x1FFFFFFF; // force high three bits to zero
in = in>>5; // truncate lowest 5 bits
sum += in;
delay(198); // (msec). Total Looptime: +2 msec (overhead for comms)
}
}
long SpiRead(void) {
long result = 0;
long b;
//long result2 = 0;// MOSI/SDI pin 7 HIGH => 7 Hz, best resolution
digitalWrite(slaveSelectPin,LOW); // take the SS pin low to select the chip
delayMicroseconds(1); // probably not needed, only need 25 nsec delay
b = SPI.transfer(0xff); // B3
result = b<<8;
b = SPI.transfer(0xff); // B2
result |= b;
result = result<<8;
b = SPI.transfer(0xff); // B1
result |= b;
result = result<<8;
b = SPI.transfer(0xff); // B0
result |= b;
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin,HIGH);
return(result);
}
Thanks for the guidance.