I'm a noobie at programming and µC. I am playing around with an OLED 128x64 and a Nano, and will be sending various short messages to display. The display configured and working fine, but I don't want to repeat the display set up lines every time time I need to write something. I'm having difficult passing a string to a function. I read a bunch of similar threads, but most seemed to address some issue with chaining text.
/*
=======================
Watering system control
=======================
- 5 watering zones garden
- 24VAC solenoid valves
- generic 8 channel relay board
- watering suppressed when raining.
- Manual switch to increase watering during heatwave
*/
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
bool HeatWave = LOW; // initialise for normal weather
const int PlannedHour = 7 * 60; //initialise with normal start time 7 AM
const int BonusStartHour = 20 * 60; // If needed a second watering in the evening at 20:00
const int nZones = 5; // five zone watering system.
const unsigned long ZoneTime = 600000ul; // default watering time for each zone (= 10 min)
String words;
int StartHour = PlannedHour;
int currentTime;
unsigned long ZoneDuration = ZoneTime;
int PumpPin = 7;
int RainPin = 9;
int HeatWavePin = 10;
int ZonePins[] = {
2,
3,
4,
5,
6,
};
/*
Need to initialise the serial interface, for use with the Serial monitor for trouble shooting, and also to interface with the RTC.
Otherwise just set up the pins
*/
void setup() {
Serial.begin(9600);
while (!Serial)
; // wait for serial
delay(200);
display.begin(i2c_Address, true); // Address 0x3C default
//display.setContrast (0); // dim display
display.display();
delay(2000);
// Clear the buffer.
display.clearDisplay();
// text display tests
display.setTextSize(2);
display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
words = "blah Blah Blah";
twoSecondText(words);
for (int i = 0; i < nZones; i++) {
pinMode(ZonePins[i], OUTPUT);
digitalWrite(ZonePins[i], HIGH);
}
pinMode(RainPin, INPUT);
pinMode(HeatWavePin, INPUT);
pinMode(PumpPin, OUTPUT);
digitalWrite(PumpPin, HIGH);
display.setCursor(0, 0);
display.println("Exit Setup");
display.display();
delay(2000);
display.clearDisplay();
display.display();
}
/*
two second text.
*/
void twoSecondText(words) {
display.setCursor(0, 0);
display.println(words);
display.display();
delay(2000);
display.clearDisplay();
display.display();
}
/*
Once the start time for watering is called, based on Heat, decide whether to perform longer duration watering.
The Pump has to be turned on for the full duration, and the Zones get cycled through.
*/
void StartWatering() {
display.println("Start watering");
display.display();
delay(2000);
display.clearDisplay();
display.display();
if (digitalRead(HeatWavePin) == HIGH) {
ZoneDuration = ZoneTime * 1.5;
} else {
ZoneDuration = ZoneTime;
}
digitalWrite(PumpPin, LOW);
for (int i = 0; i < nZones; i++) {
digitalWrite(ZonePins[i], LOW);
display.setTextSize(2);
display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
display.print("Zone:");
display.println(i + 1); // Easier for others to read if the zones are called 1-5 rather than 0-4
display.print("On @: ");
digitalClockDisplay();
display.display();
}
digitalWrite(PumpPin, HIGH);
display.println("Finished watering");
display.display();
}
// just there to make the clock outputs look nice.
void digitalClockDisplay() {
// digital clock display of the time
display.print(hour());
printDigits(minute());
printDigits(second());
display.println();
}
void printDigits(int digits) {
display.print(":");
if (digits < 10)
display.print('0');
display.print(digits);
}
/*
As the watering system will run for years, the RTC doesn't only get interogated one time on setup, but drift in the Aduino's oscillator is avoided by resyncing to the RTC constantly.
The during heatwaves, the system should perform a second watering in the evening.
The morning start time is shifted back an hour on the weekend.
The if on the raim sensor is there to make the program go into "Do nothing" mode when the rain sensor is active.
*/
void loop() {
Serial.println("Start loop");
tmElements_t tm;
RTC.read(tm);
setTime(tm.Hour, tm.Minute, tm.Second, tm.Month, tm.Day, tm.Year);
Serial.print("Rain:");
Serial.println(digitalRead(RainPin));
Serial.print("Heat wave:");
Serial.println(digitalRead(HeatWavePin));
delay(1000);
if (digitalRead(RainPin) == LOW) {
if (tm.Wday == 0 || tm.Wday == 6) {
Serial.println("Weekend");
StartHour = PlannedHour + 60;
} else {
StartHour = PlannedHour;
}
int currentHour = hour();
int currentMinute = minute();
// Calculate the current time in minutes since midnight
currentTime = currentHour * 60 + currentMinute;
if (currentTime == StartHour || (currentTime == BonusStartHour && digitalRead(HeatWavePin) == HIGH)) {
StartWatering();
}
}
}