I made some (messy) code to take an ultrasonic sensor and a DHT11 and display the data through serial and my LCD with loading animations and a button that takes the average of the data after repeating the program 5 times. I've checked the wiring and the HelloWorld code works fine which narrows it down to some mistakes I must have made in my code. I am in no way a good programmer and for all I know it could be a very simple problem.
//Ultrasonic Transducer
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <SimpleDHT.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distanceCm, distanceInch;
//DHT11
int pinDHT11 = 8;
SimpleDHT11 dht11;
String dots;
int dotAmmount = 0;
bool testFin = true;
bool firstTest = true;
int startButton = 13;
int startButtonState = 0;
byte temperature = 0;
byte humidity = 0;
byte data[40] = {0};
bool ustEnabled = true;
bool dhtEnabled = true;
//data avaraging
bool Averaging = false;
bool displayEnabled = true;
int dataButton = 3;
int dataButtonState;
int dataAmmount;
int addedAverage;
int DHTAverage;
int tempAverage;
float UTCAverage;
int DHTdata1;
int DHTdata2;
int DHTdata3;
int DHTdata4;
int DHTdata5;
int tempData1;
int tempData2;
int tempData3;
int tempData4;
int tempData5;
float UTCdata1;
float UTCdata2;
float UTCdata3;
float UTCdata4;
float UTCdata5;
void setup() {
Serial.begin(9600);
pinMode(startButton, INPUT);
pinMode(dataButton, INPUT);
//Ultrasonic Transducer
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
startButtonState = digitalRead(startButton);
dataButtonState = digitalRead(dataButton);
if(dataButtonState == HIGH)
{
Averaging = true;
displayEnabled = false;
}
if(startButtonState == HIGH && testFin == true || firstTest == true || ustEnabled == false || dhtEnabled == false || dataButtonState == HIGH && testFin == true)
{
//DHT11
// read with raw sample data.
if (dht11.read(pinDHT11, &temperature, &humidity, data) && dhtEnabled == true) {
//Loading animation
if(dotAmmount == 0){dots = "";}
if(dotAmmount == 1){dots = ".";}
if(dotAmmount == 2){dots = "..";}
if(dotAmmount == 3){dots = "..."; dotAmmount = -1;}
Serial.println("");
Serial.print("Loading" + dots);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Loading" + dots);
delay(500);
dotAmmount += 1;
ustEnabled = false;
return;
} else
{
Serial.println("");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.println(" %");
ustEnabled = true;
lcd.clear();
lcd.setCursor(0,1);
lcd.print((int)temperature); lcd.print(" *C, ");
lcd.print((int)humidity); lcd.print(" %");
delay(1000);
}
if(ustEnabled == true)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
distanceInch = duration*0.0133/2;
}
if(displayEnabled == true)
{
//For Screen
lcd.clear();
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm.");
//For Serial
Serial.print(" Distance: ");
Serial.print(distanceCm);
Serial.print(" cm.");
} else
{
lcd.clear();
if(dotAmmount == 0){dots = "";}
if(dotAmmount == 1){dots = ".";}
if(dotAmmount == 2){dots = "..";}
if(dotAmmount == 3){dots = "..."; dotAmmount = -1;}
Serial.println("");
Serial.print("Loading" + dots);
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Loading" + dots);
dotAmmount += 1;
delay(500);
}
if(Averaging == true)
{
if(dataAmmount == 1){DHTdata1 = (int)humidity; tempData1 = (int)temperature; UTCdata1 = distanceCm;}
if(dataAmmount == 2){DHTdata2 = (int)humidity; tempData2 = (int)temperature; UTCdata2 = distanceCm;}
if(dataAmmount == 3){DHTdata3 = (int)humidity; tempData3 = (int)temperature; UTCdata3 = distanceCm;}
if(dataAmmount == 4){DHTdata4 = (int)humidity; tempData4 = (int)temperature; UTCdata4 = distanceCm;}
if(dataAmmount == 5){DHTdata5 = (int)humidity; tempData5 = (int)temperature; UTCdata5 = distanceCm;}
dataAmmount += 1;
if(dataAmmount == 6)
{
DHTAverage = (DHTdata1 + DHTdata2 + DHTdata3 + DHTdata4 + DHTdata5) / 5;
tempAverage = (tempData1 + tempData2 + tempData3 + tempData4 + tempData5) / 5;
UTCAverage = (UTCdata1 + UTCdata2 + UTCdata3 + UTCdata4 + UTCdata5) / 5;
//Ultrasonic Transducer
lcd.clear();
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(UTCAverage); // Prints the distance value from the sensor
lcd.print(" cm.");
Serial.print(" Distance: ");
Serial.print(UTCAverage);
Serial.print(" cm.");
//DHT11
Serial.println("");
Serial.print(tempAverage); Serial.print(" *C, ");
Serial.print(DHTAverage); Serial.println(" %");
lcd.setCursor(0,1);
lcd.print(tempAverage); lcd.print(" *C, ");
lcd.print(DHTAverage); lcd.print(" %");
dataAmmount = 0;
Averaging = false;
}
}
firstTest = false;
testFin = true;
} else
{
return;
}
}
--Note: The online arduino editor has not been saving correctly, and I have noticed that after coming back to work on coding half the code will have saved and the other half would not.