Hello,
I want to connect 4 multiplexed HC-SR04 sensors, and
display this onto an LCD screen.
On the Arduino Due.
I use this code:
//connect HC-SR04 5v to Arduino 5v and GND to Arduino GND
/*shift register pin connections
Pin 1-7 and Pin 15 - outputs, HC-SR04 "trig" pins
Pin 8 and 13 to GND
Pin 10 and 16 to 5v
Pin 11, 12, 14 see below
Pin 9 - not used
*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
long duration, inches, cm;
int indec, cmdec;
int inchconv = 147; // ratio between puls width and inches
int cmconv = 59; // ratio between pulse width and cm
int distCm[] = {0, 0, 0, 0, 0, 0, 0, 0};
const int numSens = 1; //number of sensors used
const int clockPin = 8;//IC pin 11
const int latchPin = 9;//IC pin 12
const int dataPin = 7;//IC pin 14
const int echo = 6; //connect "Echo" pin from all Sensors to this pin
byte leds = 0;
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(latchPin, OUTPUT);
pinMode(echo, INPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}//end setup
void loop() {
digitalWrite(dataPin, LOW);
delayMicroseconds(2);
digitalWrite(dataPin, HIGH);
delayMicroseconds(10);
digitalWrite(dataPin, LOW);
// read the length of the return pulse on Echo output
duration = pulseIn(echo, HIGH);
scan();
int i;
for (i = 0; i < numSens; i++) {
lcd.setCursor (0, 0);
lcd.print(i + 1);
lcd.print("-");
lcd.print(distCm[i]);
lcd.print("cm\t");
}//end for
lcd.print("\n");
delay(500);
}
//end loop
void scan() {
leds = 0;
int i;
int timePulse;
//call function "updateShiftRegister" to cycle through all sensors
updateShiftRegister();
//cycles 4 times for the 4 sensors -can extend up to 8 for one shift register
for (i = 0; i < numSens; i++) {
bitSet(leds, i);
updateShiftRegister();
delayMicroseconds(10);
bitClear(leds, i);
updateShiftRegister();
//connect sensor "trigger" pins to IC pin 7,6,5,4 = array num 0,1,2,3 - can extend up to 8
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
distCm[i] = ((pulseIn(echo, HIGH, 25000)) / 2) / 29;
}//end for
}//end scan
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}//end shift
It only shows one value at the screen, but how do I get all the values from all
the sensors onto the screen ?
Thank you in advance !