Having started only recently with Arduino, I had a few problems that like garbled output on a 0.96" OLED ( Garbled output on lower line with .96"OLED - #9 by mvm2 ), a Tone function that crashed the compiler and some stuff that I needed to learn first (like the use of millis and how to memorize button states.
Nevertheless, after spending hours of tinkering I now have what I think is a working basic setup and I could not let that pass by without showing it (the GF really doesn't care....).
It runs on an Arduino Nano using a DHT11 temp&humidity sensor, a ultrasonic ranging sensor, a .96"OLED, a LED and buzzer and two push buttons. It measure distanced with the soundspeed corrected for temp and humidity. A LED and buzzer blink&bleep at an interval which becomes shorter if the measured distance is shorter.
The user also has the ability to switch both the LED and buzzer on or off at will.
Certainly not groundbreaking of course, but so far I am not unhappy with the results.
For those who are interested, here is a picture of the screen at work
and here is the code:
// Project : 1
// Subject : Ultrasonic Short Distance Measuring tool with OLED 128x64-display
// Extra info: Sound speed is adjusted for temperature and humidity for more accuracy
// Made by : Maarten van Maanen - Type-R ICT Service
// Date : 06-07-2022
// Libraries
#include <Wire.h> // Library voor OLED
#include <Adafruit_SSD1306.h> // Library voor SSD1306-OLED
#include <NewTone.h> // Library voor tone (to avoid conflict with NewPing)
// Defines voor OLED
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
// Defines for Ultrasonic
#include "NewPing.h" // Library for Ultrasonic sensor
#include <DHT.h> // Basislibrary voor Ultrasonic sensor
#define Dhttype DHT11 // Selecteren type sensor
#define Dhtpin 2 // Input/output pin for KY015 Temp/humid sensor
DHT dht(Dhtpin,Dhttype); // Initialiseer DHT sensor for normal 16mhz Arduino
// Variables for Temp and Humidity
float Temp = 0; // Temperature
float Humid = 0; // Humidity
float Soundspeed = 0; // Speed of sound corrected
// Variables for distance
byte Triggerpin = 9; // Triggerpin for ultrasonic sensor
byte Echopin = 10; // Echopin for ultrasonic sensor
int MaxDist = 400; // Maximum distance for HC-SR04 sensor
byte Iteraties = 6; // Number of interations for measurement (5)
double Echoduur = 0; // Echotime for distance
float Dist1 = 0; // Calculated distance with default sound speed
float Dist2 = 0; // Calculated distance with correct sound speed
NewPing sonar(Triggerpin,Echopin,MaxDist);
// Other Variabeles
byte Start = 3; // Countdown to start
unsigned long Timecheck = 0; // Variable to check passed time
// Variables for LED light control
byte LedPin = 8; // Pin for LED
byte LedBtnpin = 3; // Pin for LED buttonswitch
byte LedBtnStat = 0; // Status LED buttonswitch
byte LedLight = 1; // 1 is LED on, 0 is LED off
// Variables for Buzzer control
byte BuzPin = 7; // Pin for Buzzer
byte BuzBtnpin = 4; // Pin for Buzzer buttonswitch
byte BuzBtnStat = 0; // Status Buzzer buttonswitch
byte Buzzer = 1; // 1 is Buzzer on, 0 is Buzzer off
// Basis
void setup() {
// Initialiseer KY015 Temp.sensor
dht.begin();
// Initialiseer OLED
display.display();
// Initialiseer LED en Buzzer
pinMode(LedBtnpin, INPUT);
pinMode(LedPin, OUTPUT);
pinMode(BuzBtnpin, INPUT);
pinMode(BuzPin, OUTPUT);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
for(;;); // Don't proceed, loop forever
}
// Welcome text and countdown
display.clearDisplay();
display.drawRect(0,0, display.width(), display.height(), SSD1306_WHITE);
display.drawRect(0,0, display.width(), 15, SSD1306_WHITE);
display.setTextColor(WHITE, BLACK);
display.setCursor(8,4);
display.print(F("Type-R ICT Services"));
display.setCursor(22,21);
display.print(F("Please wait..."));
display.setCursor(22,35);
display.print(F("Starting in:"));
display.display();
for(byte i=0;i<Start;i=i+1) {
display.setCursor(60,49);
display.print(String(Start-i)+" ");
display.display();
delay(1000);
}
// Main text settings
display.setTextSize(1); // Text size
display.setTextColor(WHITE,BLACK);
// Main Display screen
display.clearDisplay();
display.drawRect(0,0, display.width(), display.height(), SSD1306_WHITE);
display.drawRect(0,0, display.width(), 12, SSD1306_WHITE);
display.setCursor(5,2);
display.print(F("LED Sound"));
display.setCursor(5,14);
display.print(F("Temp. C"));
display.setCursor(5,24);
display.print(F("Humidity %"));
display.setCursor(5,34);
display.print(F("VSound Cor m/s"));
display.setCursor(5,44);
display.print(F("Dist. Std cm"));
display.setCursor(5,54);
display.print(F("Dist. Cor cm"));
}
// Main loop
void loop() {
// Start timer
unsigned long Time1 = millis();
byte Wait = 30; // Base delay at end of loop
int Blink = 0; // LED Blink time
// MEASURE TEMP and HUMIDITY
Temp = dht.readTemperature(); // Read Temp
Humid = dht.readHumidity(); // Read Humid
// Corrected sound speed for temp and humidity
Soundspeed = 331.3 + 0.606*Temp + 0.0124*Humid;
// MEASURING DISTANCE
// Average echotime for <interaties>
Echoduur = sonar.ping_median(Iteraties);
// Calculating
Dist1 = Echoduur * 340 * 0.00005; // Distance with default sound speed
Dist2 = Echoduur * Soundspeed * 0.00005; // Distance with corrected sound speed
// Display status LED
display.setCursor(28,2);
if (LedLight == 1) {
display.print(F("ON "));
} else {
display.print(F("OFF"));
}
// Display status Buzzer
display.setCursor(105,2);
if (Buzzer == 1) {
display.print(F("ON "));
} else {
display.print(F("OFF"));
}
// Display Temp
display.setCursor(71,14);
display.print(Temp,1);
// Display Humity
display.setCursor(71,24);
display.print(Humid,1);
// Display corrected speed of sound
display.setCursor(71,34);
display.print(Soundspeed,1);
// Display distance
display.setCursor(71,44);
display.print(F(" "));
display.setCursor(71,54);
display.print(F(" "));
if (Dist1 > MaxDist) {
Blink = 3000;
} else {
display.setCursor(71,44);
display.print(Dist1,1);
display.setCursor(71,54);
display.print(Dist2,1);
Blink = 6*round(Dist1);
}
// Update screen
display.display();
// Check LED-button status
LedBtnStat = digitalRead(LedBtnpin);
if (LedBtnStat == HIGH) {
if (LedLight == 1) {
LedLight = 0;
} else {
LedLight = 1;
}
}
// Check Buzzer-button status
BuzBtnStat = digitalRead(BuzBtnpin);
if (BuzBtnStat == HIGH) {
if (Buzzer == 1) {
Buzzer = 0;
} else {
Buzzer = 1;
}
}
// Check LED-status and change if Bink-time has passed
if (Time1 - Timecheck >= Blink) {
// Save last time changed LEDstate
Timecheck = Time1;
// Blink the LED for 15 ms if LedLight = 1
if (LedLight == 1) {
Wait = Wait - 15;
digitalWrite(LedPin, HIGH);
delay(10);
digitalWrite(LedPin, LOW);
}
// Beep the buzzer for 20 ms if Buzzer = 1
if (Buzzer == 1) {
Wait = Wait - 15;
NewTone(BuzPin,2500,10);
//delay(15);
//noNewTone();
}
}
// Short pauze
delay(Wait);
}
//
//
// End of sketch
//
All I need to now is to get all this soldered on one of those PCB board and design and print a nice 3D printed enclosure for this. But that is the next step....
