RTC Timer, LED staying on


hi guys,

first off warning pretty beginner arduino way have coded may bit cumbersome.

anyway, making controller turn electric blanket on/off either manually or @ times of day depending on shift working. i'm using button select "manually on", "manually off", "night shift" or "day shift". of working nicely struggling relay (i'm using led instead of relay testing) switch off default.

when switch on manually led comes on, off manually led goes off, in other states "night shift" , "day shift" led remains in previous state on.

i know because i'm not forcing off after leaving "manually on" state, cannot seem come way make off default. can force off in loop, switch off after blanketon function run.

any ideas welcome. thanks.

code: [select]

// date , time functions using ds1307 rtc connected via i2c , wire lib
#include <wire.h>
#include "rtclib.h"
#include <time.h>
#include <timealarms.h>
#include <liquidcrystal.h>

rtc_ds1307 rtc;

char daysoftheweek[7][12] = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};


// initialize library numbers of interface pins
liquidcrystal lcd(12, 11, 5, 4, 3, 2);

// variable store state of led
int ledstate;

// variable store state of button
int buttonstate = 0;

// previous state of button
int lastbuttonstate = 0;     

// counter increase button presses
int counter = 0;

// led on pin 10
const int led = 10;

// button on pin 6
const int buttonpin = 6;

void setup() {
 
  while (!serial); // leonardo/micro/zero

  serial.begin(57600);
  if (! rtc.begin()) {
    serial.println("couldn't find rtc");
    while (1);
  }

  if (! rtc.isrunning()) {
    serial.println("rtc not running!");
    // following line sets rtc date & time sketch compiled
    rtc.adjust(datetime(f(__date__), f(__time__)));
  }

  // set lcd's number of columns , rows:
  lcd.begin(16, 2);
 
  // led set output
  pinmode(led, output);

  // button set input
  pinmode(buttonpin, input);

  // counter set 0 begin with
  counter = 0;
}

void loop() {

    datetime = rtc.now();

    // print top line of lcd display
    lcd.setcursor(0,0);
    lcd.print(now.hour(), dec);
    lcd.print(':');
    lcd.print(now.minute(), dec);
   
    // check if button pressed or not
    buttonstate = digitalread(buttonpin);

    // move lcd cursor next row
    lcd.setcursor(0, 1);
     
     // compare buttonstate previous state
  if (buttonstate != lastbuttonstate) {
    // if state has changed, increment counter
    if (buttonstate == high) {
      // if current state low button went off on:
      counter++;     
    }
    }
   
    // 4 different "states" can toggled button press
    if (counter == 0) {
      lcd.print("off");
      blanketoff();
      //digitalwrite (led, low);
      }
   
    else if (counter == 1) {
      lcd.print("on");
      blanketon();
      //digitalwrite (led, high);
      }
   
    else if (counter == 2) {
      lcd.print("6pm night");
      alarm.alarmrepeat(6,0,0,blanketon);
      alarm.alarmrepeat(7,30,0,blanketoff);
      }
   
    else if (counter == 3) {
      lcd.print("6am day");
      alarm.alarmrepeat(20,00,0,blanketon);
      alarm.alarmrepeat(22,00,0,blanketoff);
      }
   
    // reset state 0 , start loop again
    if (counter > 3) {
      counter = 0;
      }
   
    // check if led on
    ledstate = digitalread (led);
   
    // print ledstate lcd screen
    lcd.setcursor(8, 0);
    if (ledstate == 0){
    lcd.print("off");}
    else if (ledstate == 1){
    lcd.print ("on");
    }

    // wait 0.5 seconds , clear screen
    delay(500);
    lcd.clear();
    }

void blanketon(){
      digitalwrite (led, high);
      serial.println("blanketon");
      }
void blanketoff(){
      digitalwrite (led, low);
      serial.println("blanketoff");
      }

you need add more variables record if blanket off (in manual mode) it's within timer period switching auto mode put on.

i have alarm timers call different functions record state. functions call blanketon() or blanketoff() required. user-interface code need call other functions @ auto/manual state plus timer state decide turn on or off.

try split apart reading of buttons , timers actual output functions. main loop should like:

code: [select]
void loop() {
  checkbuttons();
  checktimers();
  setoutputs();
}


Arduino Forum > Using Arduino > Programming Questions > RTC Timer, LED staying on


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???