I recently purchased a ST7565 LCD, to give me more space to output than my HD44780 4x20 LCD, and am having trouble getting it to play nice. Problem is I'm not a dev by trade, so my code is bad enough to make even the kludgiest code snippeter shivver.
My setup is a pair of ATmega328's talking to each other over the I2C bus.
Arduino 1 has the ST7565 attached, as well as an array of Dallas one-wire temp sensors, and a DHT11 temp/humidity sensor.
Arduino 2 has an ethernet shield, and is the Master on the Wire bus, pulling data from Arduino 1 every 120 seconds, and uploading to a MySQL database via PHP. Arduino 2 works just fine.
In Arduino 1, I am having to do some very very ugly shifting around between ints, strings, and chars, based on how certain devices are read, and how data must be passed. For example, my temp sensors can only be read as ints (or floats). filling an array to send over Wire.send cannot accept ints, so I am filling them to a byte array for sending. then, the ST7565 cannot print an int or byte, only a char, so I am using toCharArray to convert again. So far so good; believe it or not it actually works, and data is passed around as it should.
The problem (Finally...) is that I am finding the display of the ST7565 to be extremely unreliable in my below code. It seems to be more reliable the less I try to display on it. I have to hit the reset on the Arduino several times to get it to come up; sometimes I get nothing, sometimes I get garbage on the screen, and sometimes it works (after several resets). This leads me to believe my code is unspeakably flawed.
Hopefully that helps you to understand how I got in to this despicable code below!
Code: Select all
#include <dht11.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "ST7565.h"
OneWire oneWire(2); // One Wire bus digital pin #
DallasTemperature sensors(&oneWire);
DeviceAddress bdrTemp = { 0x28, 0xD9, 0x2E, 0x59, 0x03, 0x00, 0x00, 0xB9 };
DeviceAddress serverTemp = { 0x28, 0xE6, 0x58, 0x59, 0x03, 0x00, 0x00, 0x81 };
DeviceAddress lrTemp = { 0x28, 0x98, 0x39, 0x59, 0x03, 0x00, 0x00, 0xD6 };
DeviceAddress outsideTemp1 = { 0x28, 0x78, 0x43, 0x59, 0x03, 0x00, 0x00, 0x2F };
int backlight;
int myData[6];
int humidity;
int temp1;
int tempF1;
int tempF2;
int tempF3;
int tempF4;
String tempString1;
String tempString2;
String tempString3;
String tempString4;
String tempString5;
String humString1;
char tempChar1[3];
char tempChar2[3];
char tempChar3[3];
char tempChar4[3];
char tempChar5[3];
char humChar1[3];
dht11 DHT11;
ST7565 glcd(9,8,7,6,5);
void setup()
{
glcd.begin (0x18);
glcd.clear();
// Serial.begin(9600);
Wire.begin(2); // ID the Slave as node 2
sensors.begin();
sensors.setResolution(bdrTemp, 10);
sensors.setResolution(serverTemp, 10);
sensors.setResolution(outsideTemp1, 10);
sensors.setResolution(lrTemp, 10);
Wire.onRequest(requestEvent);
pinMode (3, OUTPUT);
analogWrite(3, 100);
pinMode (14, OUTPUT); // analog pin 0 = +5v for DHT11 sensor
pinMode (17, OUTPUT); // analog pin 3 = GND for DHT11 sensor
digitalWrite (14, HIGH); // set pin to 5v
digitalWrite (17, LOW); // set pin to GND
}
void loop()
{
int chk = DHT11.read(15);
backlight = analogRead(2);
analogWrite (3, backlight / 4);
humidity = (DHT11.humidity);
temp1 = (1.8 * (DHT11.temperature) + 32);
sensors.requestTemperatures();
tempF1 = sensors.getTempF(bdrTemp);
tempF2 = sensors.getTempF(serverTemp);
tempF3 = sensors.getTempF(outsideTemp1);
tempF4 = sensors.getTempF(lrTemp);
myData[0] = temp1;
myData[1] = humidity;
myData[2] = tempF1;
myData[3] = tempF2;
myData[4] = tempF3;
myData[5] = tempF4;
tempString1 = myData[0];
humString1 = myData[1];
tempString2 = myData[2];
tempString3 = myData[3];
tempString4 = myData[4];
tempString5 = myData[5];
tempString1.toCharArray(tempChar1,3);
tempString2.toCharArray(tempChar2,3);
tempString3.toCharArray(tempChar3,3);
tempString4.toCharArray(tempChar4,3);
tempString5.toCharArray(tempChar5,3);
humString1.toCharArray(humChar1,3);
writeLCD();
delay(1000);
}
void requestEvent()
{
Wire.send((byte *) myData, sizeof myData);
}
void writeLCD()
{
glcd.clear();
glcd.drawstring(0,0,"Temperature Gauge");
glcd.drawstring(0,1,"Inside | Outside");
// glcd.drawstring(12,2,"F Bedrm| F ");
// glcd.drawstring(12,3,"% RH |");
glcd.drawstring(0,4,"bdrtemp F | F");
glcd.drawstring(0,5,"svrtemp:");
glcd.drawstring(0,6,"LRtemp:");
glcd.display();
glcd.drawstring(0,2,tempChar1);
glcd.drawstring(0,3,humChar1);
// glcd.drawstring(47,4,tempChar2); //Bedroom
// glcd.drawstring(47,5,tempChar3); // Server
// glcd.drawstring(66,2,tempChar4); // Outside
// glcd.drawstring(47,6,tempChar5);
// glcd.drawline(0, 31, 127, 31, BLACK);
glcd.display();
// delay(1000);
}