Hello. Trying to learn my syntax. Whewn i tried combining 2 sketches somehow i got my brackets and stuff wrong. Was hoping someone could look at it and help me. Thx in advance!
#include <DHT.h> // include library
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Time.h>
#include <TimeAlarms.h>
//relay pins
const int relayPin = 3;
//dht pin
const int DHTPIN = 12;
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
boolean doneOnce=false;
DHT dht(DHTPIN, DHTTYPE);// Initialize DHT sensor
#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
//int chk;
//********************************/
void setup()
{
pinMode(4,OUTPUT);
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
lcd.begin(16,2);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
dht.begin();
Serial.begin(9600);
Serial.println("In Setup");
void loop() {
Alarm.timerOnce(10,OnceOnly); // called once after 10 seconds
Alarm.delay(1000); // this function MUST be called repeatedly for the alarms/timers to work
if(!doneOnce)
{
doneOnce=true;
Serial.println("This message will only get printed one time");
}
Alarm.timerOnce(10,CycleTimer); // alarm setup repeatedly
return;
//}
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature = ");
Serial.print(t);
Serial.print(" *C ");
Serial.print("Temperature = ");
Serial.print(f);
Serial.print(" *F ");
Serial.print("Humidity = ");
Serial.print(h);
Serial.print(" %\t ");
Serial.println();
delay(2000);
// lcd
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Air Temp = ");
lcd.print(f);
lcd.print("F ");
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Humidity = ");
lcd.print(h);
lcd.print(" %");
if (h >= 66) { // you can change humidity value here - h>=66 to your preffered number
digitalWrite(relayPin, LOW);
}
else {
digitalWrite(relayPin, HIGH);
}
if (t >= 20) {// you can change temperature value here - t>= 30 to your preferred number or change from Celsius to Fahrenheit readings
digitalWrite(relayPin, LOW);
}
else {
digitalWrite(relayPin, HIGH);
}
void OnceOnly()
{
Serial.println("This timer only triggers once");
return;
}
Alarm.delay(1000);
void CycleTimer()
{
Serial.println("Alarm set in loop and so called repeatedly");
return;
}
//}
//loopPart1();
// cycletimer
// delay(1000);// 1 second delay between measurements
// digitalWrite(4,LOW);
// delay(5000);
// digitalWrite(4,HIGH);
// delay(3000);
//}
Where does setup() end? There should be a closing brace }
The same at the end of loop.
You can't define a function inside another, so any function definitions need to be after the final } of loop.
Place your cursor in the Source Code window and press Ctrl-T. That reformats your code and may help you see erroneous code blocks.
Thanks fellas. Got a lot of errors with my brackets fixed. It has 2 major ones left. heres the code and warning.
#include <DHT.h> // include library
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Time.h>
#include <TimeAlarms.h>
//relay pins
const int relayPin = 3;
//dht pin
const int DHTPIN = 12;
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
boolean doneOnce = false;
DHT dht(DHTPIN, DHTTYPE);// Initialize DHT sensor
#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
//int chk;
//********************************/
void setup()
{
pinMode(4, OUTPUT);
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
lcd.begin(16, 2);
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
dht.begin();
Serial.begin(9600);
Serial.println("In Setup");
}
void loop() {
Alarm.timerOnce(10, OnceOnly); // called once after 10 seconds
Alarm.timerOnce(10, CycleTimer); // alarm setup repeatedly
Alarm.delay(1000); // this function MUST be called repeatedly for the alarms/timers to work
if (!doneOnce)
{
doneOnce = true;
Serial.println("This message will only get printed one time");
}
//}
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature = ");
Serial.print(t);
Serial.print(" *C ");
Serial.print("Temperature = ");
Serial.print(f);
Serial.print(" *F ");
Serial.print("Humidity = ");
Serial.print(h);
Serial.print(" %\t ");
Serial.println();
delay(2000);
// lcd
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Air Temp = ");
lcd.print(f);
lcd.print("F ");
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity = ");
lcd.print(h);
lcd.print(" %");
if (h >= 66) { // you can change humidity value here - h>=66 to your preffered number
digitalWrite(relayPin, LOW);
}
else {
digitalWrite(relayPin, HIGH);
}
if (t >= 20) {// you can change temperature value here - t>= 30 to your preferred number or change from Celsius to Fahrenheit readings
digitalWrite(relayPin, LOW);
}
else {
digitalWrite(relayPin, HIGH);
}
void OnceOnly();
{
Serial.println("This timer only triggers once");
return;
}
Alarm.delay(1000);
void CycleTimer();
{
Serial.println("Alarm set in loop and so called repeatedly");
return;
}
}
//loopPart1();
// cycletimer
// delay(1000);// 1 second delay between measurements
// digitalWrite(4,LOW);
// delay(5000);
// digitalWrite(4,HIGH);
// delay(3000);
//}
home/pi/Desktop/growroom/main program/RELAY_DHT_11_SERIAL_DISPLAY_cycle_timer i2c display/RELAY_DHT_11_SERIAL_DISPLAY_cycle_timer/RELAY_DHT_11_SERIAL_DISPLAY_cycle_timer.ino: In function 'void loop()':
[i]RELAY_DHT_11_SERIAL_DISPLAY_cycle_timer:58:22: error: 'OnceOnly' was not declared in this scope
Alarm.timerOnce(10,OnceOnly); // called once after 10 seconds
^
RELAY_DHT_11_SERIAL_DISPLAY_cycle_timer:59:23: error: 'CycleTimer' was not declared in this scope
Alarm.timerOnce(10, CycleTimer); // alarm setup repeatedly
^
Multiple libraries were found for "DHT.h"
Used: /home/pi/Arduino/libraries/DHT_sensor_library
Not used: /home/pi/Arduino/libraries/DHT-sensor-library-master
Multiple libraries were found for "LCD.h"
Used: /home/pi/Arduino/libraries/NewliquidCrystal
Not used: /home/pi/Arduino/libraries/LiquidCrystal
Not used: /home/pi/Arduino/libraries/LiquidCrystal
Not used: /home/pi/Arduino/libraries/LiquidCrystal
Not used: /home/pi/Arduino/libraries/LiquidCrystal
exit status 1
'OnceOnly' was not declared in this scope
Im learning tons as i go so if you could point me in the right direction thatd be great.
Auto Format your code in the IDE.
All function definitions should should now start on the left margin but you will notice that they don't. this is usually a clue to a missing } at the end of a function. Where does your loop() function end ? Presumably it is meant to end after the call to Alarm.delay() but does it ?