Hi I am trying to read 2 ultrasonic sensors together, so i have used the pulsein function and modified it in my code to read both sensor,it works but the problem is that i`m not sure if the code is correct because i haven't understood the "// cache the port and bit of the pin in order to speed up the pulse width measuring loop"
and just multiplied it to work for 2 sensors, if someone can explain that to me, will be grateful.
attached is the code:
/*
My attempt for multiple sensors read 29/06/2012
*/
#include "wiring_private.h"
#include "pins_arduino.h"
//define HC-SR04 pins
#define trigPin 22
#define echoPin1 24
#define echoPin2 26
//initialize
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(echoPin2, INPUT);
}
//main program
void loop() {
//define parameters
int duration[2], distance[2];
// send out a ping
digitalWrite(trigPin, LOW);
delayMicroseconds(10);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// measure the returned signal.
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
- or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
- to 3 minutes in length, but must be called at least a few dozen microseconds
- before the start of the pulse. */
// cache the port and bit of the pin in order to speed up the
// pulse width measuring loop and achieve finer resolution. calling
// digitalRead() instead yields much coarser resolution.
uint8_t state=HIGH;
unsigned long timeout=5000;
uint8_t bit1 = digitalPinToBitMask(echoPin1);
uint8_t bit2 = digitalPinToBitMask(echoPin2);
uint8_t port1 = digitalPinToPort(echoPin1);
uint8_t port2 = digitalPinToPort(echoPin2);
uint8_t stateMask1 = (state ? bit1 : 0);
uint8_t stateMask2 = (state ? bit2 : 0);
unsigned long width[2] ={0, 0}; // keep initialization out of time critical area
// convert the timeout from microseconds to a number of times through
// the initial loop; it takes 16 clock cycles per iteration.
unsigned long numloops = 0;
unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
// wait for any previous pulse to end
while ((((*portInputRegister(port1) & bit1) == stateMask1) || ((*portInputRegister(port2) & bit2) == stateMask2)) && (numloops< maxloops))
numloops++;
// wait for the pulse to start
while (((((*portInputRegister(port1) & bit1) != stateMask1) || ((*portInputRegister(port2) & bit2) != stateMask2))&& (numloops< maxloops)) && (numloops< maxloops))
numloops++;
// wait for the pulse to stop
while (((((*portInputRegister(port1) & bit1) == stateMask1) || ((*portInputRegister(port2) & bit2) == stateMask2))) && (numloops< maxloops)){
numloops++;
if ((*portInputRegister(port1) & bit1) == stateMask1)
width[0]++;
if ((*portInputRegister(port2) & bit2) == stateMask2)
width[1]++;
}
Serial.print(width[0]);
Serial.print(" - ");
Serial.println(width[1]);
// // convert the reading to microseconds. The loop has been determined
// // to be 20 clock cycles long and have about 16 clocks between the edge
// // and the start of the loop. There will be some error introduced by
// // the interrupt handlers.
// duration[0]=clockCyclesToMicroseconds(width[0] * 21 + 16);
//
// // convert to distance and display
// distance[0] = (duration[0]/2) / 29.1;
// if (distance[0] >= 100 || distance[0] <= 0){
// Serial.println("Out of range");
// }
// else {
// Serial.print(distance[0]);
// Serial.println(" cm");
// }
delay(100);
}