Make the delay(1) a delayMicroseconds(1)! SPI is fast (most devices support 10MHz). Also digitalWrite takes more than 1us anyhow!
Agreed. Digital read and write are HORRIBLY slow. I replaced some "digitalWrite" lines in a VFD driver with PORT = xxx and got an almost TENFOLD increase in speed!
Are you perhaps thinking of analog reads?
Because digital read is a matter of reading a register.
Analog read takes a bit over 100 microseconds.
This loop in my ultrasonic sensor code runs in just about 1 microsecond per pass. How do I know? I count the passes in echo and I compare them to micros() which is what I use to estimate sound travel distance. The numbers are very close even with many 1000's of passes.
mikros = micros();
// Test distance = (high level time * sound velocity (340M/S) / 2)
echo = 0UL;
while ( ( digitalRead( ECHO )))
{
echo++;
if ( echo > 1000000UL )
{
echo = 0UL;
break;
}
}
mikros = micros() - mikros;
And yeah, I need to add a temperature sensor since Mach 1 varies with temperature.
As now, my project is only good to a bit under an inch where 2mm should be possible.