Hey everyone!
I'm very new to coding and Arduino's so go easy on me! This is also my first post ![]()
I'm currently setting up an Arduino as a plant monitor.
I've decided to go with an Arduino Uno Rev 3, with a Base Shield V2 attached for the Grove sensors/displays I'm using. My Grove OLED displays are 128x64, and there are 3 of them. I'm also using a Grove TH02 sensor and a Grove temperature sensor. There is a soil sensor yet to come, however my main objective is to get the TH02 values to display on an OLED display, and the temperature sensor values to display on another, with the third display free for when the soil sensor arrives. I've made a start on the code (an awful start, but still a start), and would really love some help on getting the TH02 sensor and temperature sensor to display values on 2 seperate OLED displays.
I've followed some steps from: Problem with SSD1306 LCD and U8glib - Displays - Arduino Forum
Please excuse this atrocious code:
#include "U8glib.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
#include <U8glib.h>
#include <Print.h>
#include "clib/u8g.h"
#define _CPP_U8GLIB
#include <Wire.h>
#include <SeeedOLED.h>
#include <TH02_dev.h>
#include "Arduino.h"
#include "Wire.h"
#include <math.h>
int a;
float temperature;
int B=3975; //B value of the thermistor
float resistance;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
extern uint8_t I2C_SLA;
boolean redraw_display_one = false;
boolean redraw_display_two = false;
boolean redraw_display_three = false;
void setup()
{
I2C_SLA = 0x078;
u8g.firstPage();
do
{
draw_message_one();
}
while(u8g.nextPage());
delay(10);
I2C_SLA = 0x07A;
u8g.firstPage();
do
{
draw_message_two();
}
while(u8g.nextPage());
}
void loop()
{
a=analogRead(0);
resistance=(float)(1023-a)*10000/a; //get the resistance of the sensor;
temperature=1/(log(resistance/10000)/B+1/298.15)-273.15;//convert to temperature via datasheet ;
delay(1000);
if (redraw_display_one)
{
I2C_SLA = 0x078;
u8g.firstPage();
do
{
draw_message_two();
}
while(u8g.nextPage());
redraw_display_one = false;
}
if (redraw_display_two)
{
I2C_SLA = 0x07A;
u8g.firstPage();
do
{
draw_message_one();
}
while(u8g.nextPage());
redraw_display_two = false;
}
unsigned long time = millis();
if (time > 5000 && time < 5033)
{
redraw_display_one = true;
redraw_display_two = true;
redraw_display_three = true;
}
}
void draw_message_one()
{
u8g.setFont(u8g_font_fub14);
u8g.drawStr(12, 28, "Temperature");
u8g.setPrintPos(12, 58);
u8g.print(temperature);
}
void draw_message_two()
{
u8g.setFont(u8g_font_fub14);
u8g.drawStr(12, 28, "Goodbye");
u8g.drawStr(12, 58, "World");
}
Feel free to make major modifications or recommendations! Open to all sorts of criticism, especially considering my knowledge of coding and Arduinos is very minimal.
I also haven't added the TH02 code, but here's the example from it's library:
#include <TH02_dev.h>
#include "Arduino.h"
#include "Wire.h"
void setup()
{
Serial.begin(9600); // start serial for output
Serial.println("****TH02_dev demo by seeed studio****\n");
/* Power up,delay 150ms,until voltage is stable */
delay(150);
/* Reset HP20x_dev */
TH02.begin();
delay(100);
/* Determine TH02_dev is available or not */
Serial.println("TH02_dev is available.\n");
}
void loop()
{
float temper = TH02.ReadTemperature();
Serial.println("Temperature: ");
Serial.print(temper);
Serial.println("C\r\n");
float humidity = TH02.ReadHumidity();
Serial.println("Humidity: ");
Serial.print(humidity);
Serial.println("%\r\n");
delay(1000);
}