I am trying the develop a system for irrigating the garden and it has been suggested that I use FSM.
I had a lot of difficulty at first as although I installed the libraries required it still required another library, namely Wprogram.h.
I enventually found that you can use Arduino.h instead and the example program ran OK.
I then proceded to modify a copy of a program that I am working on, to see if I could make it work any better using FSM.
As I only wanted to turn a relay on and off I removed the fade in and fade out code.
I added these lines :
#include <FiniteStateMachine.h>
//initialize states0
State On = State(ledOn);
State Off = State(ledOff);
void ledOn(){led.on();}
void ledOff(){led.off();}
FSM relayStateMachine = FSM(On); //initialize state machine, start in state: On
//In the if that checks the Alarm state:
ledStateMachine.transitionTo(On);
//And another if to switch the relay off:
if(millis() > EndTime)
{
ledStateMachine.transitionTo(Off);
DS3231_clear_a1f();
}
When I try to compile I get the following errors:
rtc_ds3231_alarm12.ino.ino: In function 'void ledOn()':
rtc_ds3231_alarm12.ino.ino:67:14: error: 'led' was not declared in this scope
rtc_ds3231_alarm12.ino.ino: In function 'void ledOff()':
rtc_ds3231_alarm12.ino.ino:68:15: error: 'led' was not declared in this scope
rtc_ds3231_alarm12.ino.ino: In function 'void loop()':
rtc_ds3231_alarm12.ino.ino:116:1: error: 'ledStateMachine' was not declared in this scope
rtc_ds3231_alarm12.ino.ino:123:3: error: 'ledStateMachine' was not declared in this scope
'led' was not declared in this scope
I had assumed that you would need the led.h, so I included that at the beginning.
I also tried to add int led, but that gave the following errors:
rtc_ds3231_alarm12.ino.ino: In function 'void ledOn()':
rtc_ds3231_alarm12.ino.ino:67:18: error: request for member 'on' in 'led', which is of non-class type 'int'
rtc_ds3231_alarm12.ino.ino: In function 'void ledOff()':
rtc_ds3231_alarm12.ino.ino:68:19: error: request for member 'off' in 'led', which is of non-class type 'int'
Why does it not work, as the same lines were in the example?
Is there a library that I am not including?
Can't even test if the code is working for the program, until I sort out these issues.
Please help!
Copy of the code as it stands at the moment:
// rtc_ds3231_alarm7 (Working with sensor)
// during an alarm the INT pin of the RTC is pulled low
//SDA - pin A4
// SCL - pin A5
// this is handy for minimizing power consumption for sensor-like devices,
// since they can be started up by this pin on given time intervals.
#include <FiniteStateMachine.h>
#include <Wire.h>
#include "ds3231.h"
#include <led.h>
#define BUFF_MAX 256
// time when to wake up
uint8_t wake1_HOUR = 17;
uint8_t wake1_MINUTE = 00;
uint8_t wake1_SECOND = 00;
uint8_t wake2_HOUR = 17;
uint8_t wake2_MINUTE = 59;
int relaypin = 10;
int SQWpin = 8;
int tankSensePin = A0;
int curCounter = 0;
int sensorValue = 0;
boolean Alarm1On = false;
boolean Alarm2On = false;
unsigned long StartTime=0;
unsigned long EndTime=0;
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
// constants won't change :
unsigned long prev = 1000, interval = 1000;
void set_alarm(void) {
// flags define what calendar component to be checked against the current time in order
// to trigger the alarm - see datasheet
// A1M1 (seconds) (0 to enable, 1 to disable)
// A1M2 (minutes) (0 to enable, 1 to disable)
// A1M3 (hour) (0 to enable, 1 to disable)
// A1M4 (day) (0 to enable, 1 to disable)
// DY/DT (dayofweek == 1/dayofmonth == 0)
uint8_t flags1[5] = { 0, 0, 0, 1, 1 };
// A2M2 (minutes) (0 to enable, 1 to disable)
// A2M3 (hour) (0 to enable, 1 to disable)
// A2M4 (day) (0 to enable, 1 to disable)
uint8_t flags2[3] = {0, 0, 1};
// set Alarm1
DS3231_set_a1(wake1_SECOND, wake1_MINUTE, wake1_HOUR, 0, flags1);
// activate Alarm1
DS3231_set_creg(DS3231_INTCN | DS3231_A1IE);
// set Alarm2
DS3231_set_a2(wake2_MINUTE, wake2_HOUR, 0, flags2);
// activate Alarm2
DS3231_set_creg(DS3231_INTCN | DS3231_A2IE);
}
const byte NUMBER_OF_STATES = 2; //how many states are we cycling through?
int led;
//initialize states0
State On = State(ledOn);
State Off = State(ledOff);
void ledOn(){led.on();}
void ledOff(){led.off();}
FSM relayStateMachine = FSM(On); //initialize state machine, start in state: On
void setup() {
Serial.begin(9600);
Wire.begin();
pinMode(SQWpin, INPUT);
pinMode(relaypin, OUTPUT);
pinMode(tankSensePin, INPUT);
DS3231_init(DS3231_INTCN);
DS3231_clear_a1f();
DS3231_clear_a2f();
set_alarm();
}
void loop()
{
if(DS3231_triggered_a1()== true){
StartTime=millis();
EndTime=StartTime+60000;
Serial.println(StartTime);
Serial.println(EndTime);
}
//boolean myAlarm1On = false ;
//boolean myAlarm2On = false ;
char buff[BUFF_MAX];
unsigned long now = millis();
boolean Alarm2On = false;
struct ts t;
// once a while show what is going on
if ((now - prev > interval) && (Serial.available() <= 0)) {
DS3231_get(&t);
// display current time
snprintf(buff, BUFF_MAX, "%d.%02d.%02d %02d:%02d:%02d", t.year,
t.mon, t.mday, t.hour, t.min, t.sec);
Serial.println(buff);
//boolean Alarm1On = DS3231_triggered_a1();
boolean Alarm2On = DS3231_triggered_a2();
int tankSenseReading = analogRead(tankSensePin);
Serial.println(tankSenseReading);
if (tankSenseReading>500 && millis() > StartTime && millis() < EndTime) {
// INT/SQW has been pulled low
Serial.println(" -> alarm1 has been triggered");
// digitalWrite(relaypin, HIGH);
ledStateMachine.transitionTo(On);
Serial.println(StartTime);
Serial.println(EndTime);
}
if(millis() > EndTime)
{
ledStateMachine.transitionTo(Off);
DS3231_clear_a1f();
}
prev = now;
}
}