I have two questions concerning the following bit of code.
val = digitalRead(ultraSoundSignal);
while(val == LOW) {
val = digitalRead(ultraSoundSignal); }
while(val == HIGH) {
val = digitalRead(ultraSoundSignal);
timecount = timecount++ ; }
1.) What is the actual speed of the repetition timecount = timecount++ inside the second while loop? I know that it is neither in milliseconds nor in microseconds.
2.) Is it possible to have the repetition of timecount = timecount++ repeat in millisecond steps? If yes that would help me a lot. How could that be done?
The Arduino executes 16,000,000 (typically) machine instructions per second. If the while loop is doing nothing but incrementing a counter, the counter could go up by 4,000,000 to 6,000,000 each second.
You can use the millis() function and a specified interval, and keep track of the previous time an increment occurred. Each time through the loop, see if enough time has elapsed. If so, increment the value.
Is it possible to have the repetition of timecount = timecount++ repeat in millisecond steps?
How about this? You would have to "tune" the actual value if you wanted to be really close to every millisecond. digitalRead itself probably takes 5 to 10 microseconds to execute, and the loop itself a microsecond or so...
pulseIn() wouldn't work in my case , as I have to manipulate that result later on.
"That" result?
It isn't clear why pulseIn wouldn't work. The pulseIn function returns the time it took for the pulse to occur. Isn't that what you want to know? How can you manipulate time? Do you have a DeLorean? Do you have a big enough flux capacitor?
A big thanks to AWOL. This code works just fine and gives almost the same results as pulseIn().
To answer PaulS's question. I'm developing a musical instrument using 8 different PING))) sensors. If I play it in small rooms, I have to be very careful to limit the maximum range of the sensors (=3,2m) in order to not get a lot of false triggers from previously sent ultrasonic bursts that bounce back and forth in the room. Therefore I limited the range all sensors to 47,5cm. Thats all the range I need. I if understood the concept of *pulseIn()* correctly then it **always** waits until it receives a LOW or otherwise gives its highest value (around 12000[ch956]s). Thats what I referred to.
Sorry to disappoint you, I'm not yet changing our temporal dimension...
Here`s the bit of code limiting the range of the pings:
pinMode(ultraSoundSignal, INPUT);
val = digitalRead(ultraSoundSignal);
while(val == LOW) { // Loop until pin reads HIGH
val = digitalRead(ultraSoundSignal);
startTime = micros(); }
while(val == HIGH) { // Loop until pin reads LOW
val = digitalRead(ultraSoundSignal);
timecount = micros() - startTime;
if(timecount > 2700) { // Limiting the range to 2700[ch956]s
return 0; // which equals approx. 45,7cm
digitalWrite(ultraSoundSignal, LOW);
pinMode(ultraSoundSignal, OUTPUT); }
}
return timecount;
}