Something is wrong in my project but cant find out what?
The LCD is printing weird characters , in the serial monitor everything looks fine, and the lcd changes color after the temperatures that I want.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(25, 26, 29, 38, 41, 42);
#define ONE_WIRE_BUS 37 // kommunikationskanalen till pin 37
OneWire oneWire(ONE_WIRE_BUS); // kommunikationen för sonden
DallasTemperature sensors(&oneWire);
DeviceAddress tempsond = { 0x28, 0x57, 0xDA, 0x58, 0x05, 0x00, 0x00, 0xA7 };
void setup(void)
{
Serial.begin(9600); // startar seriella porten
sensors.begin();// Startar klass biblioteket
sensors.setResolution(tempsond, 10); // upplösning på termometern, 9= 0,5grader 10=0,25
pinMode(30, OUTPUT); // utgång för lcd red
pinMode(33, OUTPUT); // utgång för lcd green
pinMode(34, OUTPUT); //utgång för lcd blue
pinMode(45, OUTPUT); //buzzer
lcd.begin(16,2);
lcd.clear();
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
Serial.print("Kommunikationsfel");
}
else {
Serial.print("C: ");
Serial.print(tempC);
Serial.print(" F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
lcd.setCursor(0, 1);
lcd.print(tempC);
lcd.print((char)223);
lcd.print("C");
lcd.print(" ");
lcd.print(DallasTemperature::toFahrenheit(tempC));
lcd.print((char)223);
lcd.print("F");
lcd.setCursor(0, 1);
if ((tempC >=-30) && (tempC<10))
{
digitalWrite(34, HIGH);
digitalWrite(30, LOW);
digitalWrite(33, LOW);
}
else if ((tempC >=10) && (tempC<26))
{
digitalWrite(33, HIGH);
digitalWrite(30, LOW);
digitalWrite(34, LOW);
buzz(45, 400, 500);
}
else if ((tempC >=27) && (tempC<=30))
{
digitalWrite(30, HIGH);
digitalWrite(33, HIGH);
digitalWrite(34, LOW);
buzz(45, 1600, 1000);
}
else if ((tempC >=31) && (tempC<=50))
{
digitalWrite(30, HIGH);
digitalWrite(33, LOW);
digitalWrite(34, LOW);
buzz(45, 2800, 1500);
}
}
}
void loop(void)
{
delay(2000);
Serial.print("Avvakta...\n\r");
sensors.requestTemperatures();
Serial.print("Temperatur: ");
printTemperature(tempsond);
Serial.print("\n\r");
Serial.print("\n\r");
}
void buzz( int targetPin, long frequency, long length) {
long delayValue = 1000000/frequency/2;
long numCycles = frequency * length/ 1000;
for (long i=0; i < numCycles; i++)
{
digitalWrite(targetPin, HIGH);
delayMicroseconds(delayValue);
digitalWrite(targetPin, LOW);
delayMicroseconds(delayValue);
}
}