This Arduino code uses loops to read 8 HC-SR04 UltraSonic sensors, and write
the pin numbers and the sensor values to the COM port. There is a VB.Net program
running on the PC to read the data from it's own COM port and display it
on the screen. The data is also written to a SQL table, so it is available
to any other VB application.
This Arduino code contains 4 functions:
void setup() [Typical Arduino stuff]
void loop() [Typical Arduino stuff]
TriggerSensor()
GetRawReadings()
/*
This version has most delays() removed
it stores the values in an array
it does not do the conversion math on the arduino,
Conversion math must be done in VB
the multi reading and averaging was reduced from 5 to 3
*/
long BaudRate = 9600;
unsigned long ValuesArray[40];
int FirstPin = 22; // shd be 22 for front left sensor
int LastPin = 37; // shd be 30 for 5 sensors
// When all 8 sensors are installed, chg LastPin to 37
/*
* NBR OF FIRST LAST SENSOR
* SENSORS PIN PIN LOCATION
* ------- ----- ---- --------
* 1 22 23 #1 Front Left
* 2 24 25 #5 Front
* 3 26 27 #2 Front Right
* 4 28 29 #3 Right Rear
* 5 30 31 #4 Left Rear
* 6 32 33 #6 Left Side
* 7 34 35 #7 Right Side
* 8 36 37 #8 Rear
*/
void setup()
{
// setup code , to run once:
//Serial Port begin
Serial.begin (BaudRate);
Serial.println();
Serial.println("Setting Pin Modes");
Serial.println();
//Define inputs and outputs
for(int PinNo = FirstPin; PinNo<= LastPin; PinNo = PinNo + 2)
{
pinMode(PinNo, INPUT);
pinMode(PinNo + 1, OUTPUT);
ValuesArray[PinNo] = 111; // init the array
} // END OF FOR LOOP THAT ITERITATES THRU THE PIN NUMBERS
} // END OF SETUP Function
// ************** Main Loop *********************
void loop()
{
// ************************************************************************
// This loop scans the input pins on the Arduino, to read the UltraSonic sensors.
for(int PinNo = FirstPin; PinNo <= LastPin; PinNo +=2)
{
GetRawReadings(PinNo +1); // must have +1
}
// ************************************************************************
// ************************************************************************
// print values from the array
for(int PinNo = FirstPin; PinNo <= LastPin; PinNo +=2)
{
// Serial.print(PinNo); Serial.print(" "); Serial.print(ValuesArray[PinNo]); Serial.print(",");
Serial.print(PinNo); Serial.print(" "); Serial.print(ValuesArray[PinNo+1]); Serial.print(", ");
}
Serial.println(); // New line
// ************************************************************************
} // End of MAIN loop
// If the program if forced to wait for pulses that never arrive,
// because the sensor is not there, it REALLY slows down the program
// Maybe periodicly check all the sensors, and set the program
// to skip over missing sensors
unsigned long GetRawReadings(int PinNo)
{
// The input pin is 1 less than the output pin
int InPin = PinNo - 1;
unsigned long duration ;
// interupts must be ON for pulseIn() to work
interrupts(); // Turn On Interupt handling
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
TriggerSensor(PinNo);
duration = 0;
if (duration > 170)
{
ValuesArray[PinNo]=duration; // microSeconds
};
} // END OF GetRawReadings
// ************************************************************************
// *** This function triggers the sensor ***
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
int TriggerSensor(int PinNo)
{
digitalWrite(PinNo, LOW);// A short LOW pulse beforehand to ensure a clean HIGH pulse:
delayMicroseconds(5);
digitalWrite(PinNo, HIGH);// The 10 microsecond Trigger
delayMicroseconds(10);
digitalWrite(PinNo, LOW);// Turn off the trigger
} // END OF TriggerSensor()
// ************************************************************************
Read eight HC-SR04 Sensors using Arduino. ZoeTheRobot.Net 2023