Arduino Sensor Examples
2 Examples • C/C++
HC-SR04 Distance Sensor
Array Manipulation Functions
File: hc-sr04_distance_sensor.cpp
1/*
2 * HC-SR04 example sketch
3 *
4 * https://create.arduino.cc/projecthub/Isaac100/getting-started-with-the-hc-sr04-ultrasonic-sensor-036380
5 *
6 * by Isaac100
7 */
8
9const int trigPin = 9;
10const int echoPin = 10;
11
12float duration, distance;
13
14void setup() {
15 pinMode(trigPin, OUTPUT);
16 pinMode(echoPin, INPUT);
17 Serial.begin(9600);
18}
19
20void loop() {
21 digitalWrite(trigPin, LOW);
22 delayMicroseconds(2);
23 digitalWrite(trigPin, HIGH);
24 delayMicroseconds(10);
25 digitalWrite(trigPin, LOW);
26
27 duration = pulseIn(echoPin, HIGH);
28 distance = (duration * 0.0343) / 2;
29 Serial.print("Distance: ");
30 Serial.println(distance);
31 delay(100);
32}
File: array_manipulation_functions.cpp
1// Array utility functions
2
3// Function to find maximum value in array
4int findMax(int arr[], int size) {
5 int maxVal = arr[0];
6 for (int i = 1; i < size; i++) {
7 if (arr[i] > maxVal) {
8 maxVal = arr[i];
9 }
10 }
11 return maxVal;
12}
13
14// Function to calculate average of array elements
15float calculateAverage(int arr[], int size) {
16 int sum = 0;
17 for (int i = 0; i < size; i++) {
18 sum += arr[i];
19 }
20 return (float)sum / size;
21}
22
23// Function to reverse array
24void reverseArray(int arr[], int size) {
25 for (int i = 0; i < size / 2; i++) {
26 int temp = arr[i];
27 arr[i] = arr[size - 1 - i];
28 arr[size - 1 - i] = temp;
29 }
30}
31
32// Example usage
33void setup() {
34 int numbers[] = {5, 2, 8, 1, 9};
35 int size = 5;
36
37 Serial.begin(9600);
38 Serial.print("Max value: ");
39 Serial.println(findMax(numbers, size));
40
41 Serial.print("Average: ");
42 Serial.println(calculateAverage(numbers, size));
43}
Collection of Arduino code examples including sensor reading and array manipulation functions.
Adafruit_Sensor: https://github.com/adafruit/Adafruit_Sensor
Related Posts
IC 74173 – 4-Bit D-Type Register with 3-State Outputs
IC 74173 - 4-Bit D-Type Register with 3-State Outputs
TTL Quad D Flip-Flop with Asynchronous Clear and Output Enable
...
DIY Metal Detector Kit
DIY Metal Detector Kit
DC 3V-5V Non-Contact Sensor Module with 60mm Detection Range
Introduction
The DIY Metal Detec...
CNC V3 Shield with 4 A4988 Drivers
CNC V3 Shield with 4 A4988 Drivers
Complete Arduino-compatible CNC controller for 3D printers and milling machines
...
CN3791 12V MPPT Solar Charger Module
CN3791 12V MPPT Solar Charger Module
Maximum Power Point Tracking Solar Charge Controller for Lead-Acid/Lithium Batteries
...
CJMCU-TRRS 3.5mm Jack AV Stereo Module
CJMCU-TRRS 3.5mm Jack AV Stereo Module
Compact breakout board for audio/video signal interfacing with TRRS connectors
...
TTP223 Capacitive Touch Sensor Module (Red)
TTP223 Capacitive Touch Sensor Module (Red)
Single-Key Touch Detection with Digital Output for Arduino and DIY Projects
...
Capacitive Soil Moisture Sensor
Capacitive Soil Moisture Sensor
Corrosion-Resistant Humidity Detection for Plants and Agricultural Applications
Intro...
Recent Comments