merging button sketches and DS3231 sketches controlling same "LED"
hello,
i'm trying write sketch aquarium controller. have of running i'm having real hard time combining individual sketches "i" wrote (i plagiarized existing sketches).
the basic concept full sketch :
- equipment controlled rtc on / off period. let's on 12.00pm 8.00pm
- while equipment in on, have 2 buttons options
i) button "cleaning" turn off equipment when pushed , turn on when pushed again.
ii) button "feeding" turn off equipment when pushed , turn on after given time.
i have more 1 equipment i'm keeping things simple now. since can't make work 1 thing!
hardware used rtc ds3231 , arduino uno r3 special edition. have led connected pin 9 represneting equipement.
the issue i'm having merging 2 separate sketches.
my "time" sketch (i apologize unconventional coding syntax). (skimmer equipment name used.)
and the code 2 push buttons. behavior isn't have expected works close enough. ( sorry again coding syntax.)
when moving button function ( variables) time function, button render inactive. declare push button in void setup(). pushing button not turn off led. looking around seems should using sort of "state machine" have no clue start. state machine right way go? little guidance appreciated. above sketches took me 1 months to"figure" out. complete newbie here ....
hopefully didn't bored guys deaf , might pointers...
thanks.
i'm trying write sketch aquarium controller. have of running i'm having real hard time combining individual sketches "i" wrote (i plagiarized existing sketches).
the basic concept full sketch :
- equipment controlled rtc on / off period. let's on 12.00pm 8.00pm
- while equipment in on, have 2 buttons options
i) button "cleaning" turn off equipment when pushed , turn on when pushed again.
ii) button "feeding" turn off equipment when pushed , turn on after given time.
i have more 1 equipment i'm keeping things simple now. since can't make work 1 thing!
hardware used rtc ds3231 , arduino uno r3 special edition. have led connected pin 9 represneting equipement.
the issue i'm having merging 2 separate sketches.
my "time" sketch (i apologize unconventional coding syntax). (skimmer equipment name used.)
code: [select]
#include <ds3231.h>
// init ds3231 using hardware interface
ds3231 rtc(sda, scl);
// init time-data structure
time t;
// declaring pin equipment
const int skimmer = 9; // skimmer on pin 9
void setup()
{ // setup serial connection
serial.begin(115200);
// declaring equiment:
pinmode(skimmer, output);
// initialize rtc object
rtc.begin();
// following lines can uncommented set date , time
//rtc.setdow(wednesday); // set day-of-week sunday
//rtc.settime(12, 0, 0); // set time 12:00:00 (24hr format)
//rtc.setdate(1, 1, 2014); // set date january 1st, 2014
}
// setting lights on / off period ( skimmer on same time period)
// declare start time // declare off time
int lightonhour = 12; int lightoffhour = 20;
int lightonmin = 29; int lightoffmin = 59;
void lightp () { // managing light on , off, skimmer on same timer.
// converting time xxxx number ie 1508, done ease time comparison
// current time conversion
int thournow = t.hour;
int tminnow = t.min;
int tnow = thournow * 100 + tminnow;
// light on/off time conversion
int ton = lightonhour * 100 + lightonmin;
int toff = lightoffhour * 100 + lightoffmin;
if ( ((tnow >= ton) && (tnow < toff)) ) { // defining time period when lights on
digitalwrite (skimmer, high);
}
else {
digitalwrite (skimmer, low); // otherwise lights off
}
}
// function print time in serial monitor.
void printingtime() {
// printing time on serial monitor
serial.print(t.hour, dec);
serial.print(":");
serial.print(t.min, dec);
serial.print(":");
serial.print(t.sec, dec);
serial.println(" ");
}
void loop() {
// data ds3231 , real time clock
t = rtc.gettime();
// calling printing time function
printingtime();
// running different function
lightp();
}
and the code 2 push buttons. behavior isn't have expected works close enough. ( sorry again coding syntax.)
code: [select]
// declaring pin equipement
const int skimmer = 9; // skimmer on pin 9
const byte feedingbutton = 4; // our button pin
const int cleaningbutton = 2; // cleaning button connected pin 2
void setup()
{
// setup serial connection
serial.begin(115200);
// declaring equiment:
pinmode(skimmer, output);
pinmode(feedingbutton, input); // push button declared input
pinmode(cleaningbutton, input); // push button declared input
// having skimmer high, used check program without clock running
digitalwrite( skimmer, high);
}
//start of feeding function
// declaring feeding button variable
unsigned long feedingpushedmillis; // when button released
unsigned long feedingstartedat; // when feeding started
unsigned long feedingdelay = 5000; // how long feeding ( filter , skimmer powered off)
unsigned long feedingoffdelay = 50; // debouncing time
bool feedingready = false; // flag when button let go
bool feedingstate = false; // feeding state on or not.
// end of feeding variables
void feedingp () {
unsigned long currentmillis = millis();
// check button
if (digitalread(feedingbutton) == high) {
// update time when button pushed
feedingpushedmillis = currentmillis;
feedingready = true; // allow go through next if loop when button released.
}
// make sure code isn't checked until after button has been let go
if (feedingready) {
//this typical millis code here:
if ((currentmillis - feedingpushedmillis) >= feedingoffdelay) {
// okay, enough time has passed since button let go.
digitalwrite(skimmer, low);
// setup our next if loop
feedingstate = true;
// save when equipement turned off
feedingstartedat = currentmillis;
// wait next button press, exit if loop
feedingready = false;
}
}
// see if watching time turn off led
if (feedingstate) {
// okay, led on, check long
if ((currentmillis - feedingstartedat) >= feedingdelay) {
feedingstate = false;
digitalwrite(skimmer, high);
}
}
}
// declaring cleaning button variable
int cleaningstate ; // cleaning state true or false
int cleaningbuttonstate ; // read state of button
int lastcleaningbuttonstate = low; // last reading input pin
unsigned long lastcleaningtime = 0;
unsigned long cleaningdelay = 50;
void cleaningp () {
// read state of switch local variable
int cleaningreading = digitalread(cleaningbutton);
// check see if pressed button
// (i.e. input went low high), and you've waited
// long enough since last press ignore noise:
// if button state changed, due noise or pressing:
if (cleaningreading != lastcleaningbuttonstate) {
lastcleaningtime = millis(); // reset debouncing timer
}
if ((millis() - lastcleaningtime) > cleaningdelay) {
// whatever reading at, it's been there longer
// debounce delay, take actual current state:
if (cleaningreading != cleaningbuttonstate) { // if button state has changed:
cleaningbuttonstate = cleaningreading;
if (cleaningbuttonstate == high) { // change equipment on off if new button state high
cleaningstate = !cleaningstate;
// set equipement
digitalwrite(skimmer, cleaningstate);
}
}
}
// save reading. next time through loop,
// it'll lastcleaningbuttonstate:
lastcleaningbuttonstate = cleaningreading;
}
void loop() {
// running different function
feedingp();
cleaningp();
}
when moving button function ( variables) time function, button render inactive. declare push button in void setup(). pushing button not turn off led. looking around seems should using sort of "state machine" have no clue start. state machine right way go? little guidance appreciated. above sketches took me 1 months to"figure" out. complete newbie here ....
hopefully didn't bored guys deaf , might pointers...
thanks.
i suspect what's happening (although didn't post combined code) although might checking buttons in feedingp() , cleaningp(), , switching skimmer on, when lightp() runs turns them straight off again based on time of day.
you try boolean flag, called buttonoverride, , set true in feedingp() , cleaningp() when either turns skimmer on. use flag's state control if lightp() should turn skimmer off, flag being set in feedingp() or cleaningp() taking priority. remember set false when feedingp() , cleaningp() have done do.
(very off top of head, ymmv.)
you try boolean flag, called buttonoverride, , set true in feedingp() , cleaningp() when either turns skimmer on. use flag's state control if lightp() should turn skimmer off, flag being set in feedingp() or cleaningp() taking priority. remember set false when feedingp() , cleaningp() have done do.
(very off top of head, ymmv.)
Arduino Forum > Using Arduino > Programming Questions > merging button sketches and DS3231 sketches controlling same "LED"
arduino
Comments
Post a Comment