// Changes for Wokwi:
// Imported some libraries, zie tab "Libary Manager"
// NewTone from: https://bitbucket.org/teckel12/arduino-new-tone/wiki/Home
// Uploaded NewTone to this Wokwi project
// Changed #include <NewTone.h> to #include "NewTone.h"
// Changed #define Dhtype DHT11 to DHT22 because Wokwi has only DHT22.
// 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 DHT22 // 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
//