1

I am trying to create a code which prints to the LCD a message once a certain time is passed.

#include <Time.h>
#include <TimeLib.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

void setup() {
  lcd.begin(16, 2);
  setTime(02, 10,00, 14, 03, 2016); 

  //pinMode(13, OUTPUT);
  pinMode(2, INPUT);
  digitalWrite(2, LOW);
  attachInterrupt(0, blink, CHANGE);
}

void loop() {
  lcd.setCursor(0, 1);
  char time_out[16];
  sprintf(time_out, "%02u:%02u:%02u",hour(),minute(),second());//use %x to get hex 
  lcd.print(time_out);  

  int totalSeconds = (hour() * 3600) + (minute() * 60) + second();  
  lcd.print(totalSeconds);
  if(totalSeconds > 7806) {
    digitalWrite(2, HIGH);
  }
}

void blink() {
  lcd.print("time passed");
}

The setup uses the Time lib to set a date. It also sets pin 2 as an INPUT and sets it it LOW. The attachInterrupt checks if pin 2 (0), changes, then function blink should execute.

In the loop, I am creating a string that prints out the time and displaying it to the LCD. I am also converting the time to seconds, so that I can easily compare two times. After 6 seconds (the timer starts at 7800), I am changing the value of pin 2 to HIGH.

This should indicate a change in its state and should and trigger blink, but it does not. Can anyone spot the mistake I made? Thanks in advance.

  • 1
    Check what actually happens if you write to a pin that is defined as input. This does not work as you are assuming. – Mikael Patel Apr 8 '17 at 15:04
  • Which board are you using ? – Prayuktibid Apr 8 '17 at 15:19
1

Since I don't have enough reputations to comment on your question I am going to drop my two cents here.

Since your question is a little bit vague, I am going to suppose that you don't want any external interrupts thus, you don't need to use the attachInterrupt function. I am going to suppose that there is no need to use timers' interrupts as well.

Have a look at the following code:

#include <Time.h>
#include <TimeLib.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

void setup() {
  lcd.begin(16, 2);
  setTime(02, 10,00, 14, 03, 2016); 
}

void loop() {
  char time_out[16];
  lcd.setCursor(0, 1);
  sprintf(time_out, "%02u:%02u:%02u",hour(),minute(),second());//use %x to get hex 
  lcd.print(time_out);  

  int totalSeconds = (hour() * 3600) + (minute() * 60) + second();  
  lcd.print(totalSeconds);
  if(totalSeconds > 7806) {
    blink();
  }
}

void blink() {
  lcd.print("time passed");
}

You should know the following:

Commands inside void loop() is going to be executed for ever again and again. Especially lcd.print(time_out);. You had better refresh the LCD every 1 second.

After 6 seconds blink() is going to be executed many times (maybe forever) since it is inside the loop and if(totalSeconds > 7806) is going to be always true.

If you have any question let me know in the comment section.

1
  1. To do what you want, pin 2 would need to be an output. As an input you have no control over its state when you write it low. It may be hanging there floating in a high state and when you write high to it and turn on the pullup that may or may not actually be a change of state.

  2. Doing a print to the LCD may not work in an ISR. Remember that during the ISR all other interrupts are disabled and depending on how your lcd library works it may not print anything from inside the ISR.

  3. Even if it does print "time passed" upon being triggered by the line at the end of loop, a few microseconds later loop would repeat and that would be overwritten by the stuff that loop writes to the screen. So even if it did work you'd never know.

Instead of having your ISR print something to the screen, have it turn on an led or something so you know it actually triggered.

Try this instead and see if it is closer to what you want: (uncompiled and untested as I don't have the libraries)

#include <Time.h>
#include <TimeLib.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

boolean passed = false;

void setup() {
  lcd.begin(16, 2);
  setTime(02, 10, 00, 14, 03, 2016);

  //pinMode(13, OUTPUT);
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
  attachInterrupt(0, blink, CHANGE);
}

void loop() {
  if (passed) {
    lcd.setCursor(0, 1);
    lcd.print("time passed");
    while(1);  // freeze here time is up
  }
  else {
    lcd.setCursor(0, 1);
    char time_out[16];
    sprintf(time_out, "%02u:%02u:%02u", hour(), minute(), second()); //use %x to get hex
    lcd.print(time_out);

    int totalSeconds = (hour() * 3600) + (minute() * 60) + second();
    lcd.print(totalSeconds);
    if (totalSeconds > 7806) {
      digitalWrite(2, HIGH);
    }
  }
}

void blink() {
  passed = true;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.