Go Down

Topic: cant get my lcd to blink lcd.print(" "); right (Read 155 times) previous topic - next topic

mbasile

Having trouble with my code not blinking the (0,0) row of my 16x2 lcd screen right. I want it to blink each lcd.print("") command for 5 seconds each, however it is only blinking the date for 5 seconds right now, and then the lcd.print("lena feeder"); only prints for 1 second, also the time seems to be affected by this since it only updates every 6 seconds now. can someone spot my problem? and help me get what i want.

Code: [Select]
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
#include <TimeAlarms.h>
#include <Time.h>

RTC_DS1307 RTC;
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
//int motorPin = 2;
int lastHour = 24;
int lastMinute = 60;

bool feedPet = false;
unsigned long feedTime = 0;
unsigned long lastPrintTime = 0;

struct FeedTime {
  int hour, minute;
};

FeedTime amFeed = {9, 30};  // i.e. 9:30am
FeedTime pmFeed = {17, 30};  // i.e. 5:30pm

//sets up motor 1
//int pinI1 = 8;
int pinI2 = 12;
int speedpin1 = 10;

//sets speed of motor
int speed = 255;

void setup ()
{
  //pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  lcd.begin(16, 2);
  Wire.begin();
  RTC.begin();
  //pinMode (pinI1, OUTPUT);
  pinMode (pinI2, OUTPUT);
  pinMode (speedpin1, OUTPUT);

  /*{
    lcd.println("RTC NOT Running!");
    RTC.adjust(DateTime((__DATE__), (__TIME__)));
  }*/
}

void loop ()
{
  DateTime now = RTC.now();
  FeedTime currentTime;
  currentTime.hour = now.hour();
  currentTime.minute = now.minute();
  if ((currentTime.minute != lastMinute) && (((currentTime.hour == amFeed.hour) && (currentTime.minute == amFeed.minute)) || ((currentTime.hour == pmFeed.hour) && (currentTime.minute == pmFeed.minute))))
  {
    feedTime = millis();
    feedPet = true;
  }
  lastMinute = currentTime.minute;
  if (feedPet)
  {
    turnFeeder();
  }
  if (millis() - lastPrintTime > 1000UL)
  {
    // display the date
    lcd.setCursor(0, 0);
    char nowDate[24] = "";
    sprintf(nowDate, "DATE: %02d/%02d/%d", now.month(), now.day(), now.year());
    lcd.print(nowDate);
   
    // set the blink rate
    delay(5000);
    lcd.clear();
    lcd.print("Lena Feeder v3.0");
   
    // display the time
    lcd.setCursor(0, 1);
    char nowTime[24] = "";
    sprintf(nowTime, "Time: %02d:%02d:%02d", now.hour(), now.minute(), now.second());
    lcd.print(nowTime);
    lastPrintTime = millis();
  }

  analogWrite(speedpin1, speed);
}

void turnFeeder(void)
{
  static bool pinState = true;
  if (millis() - feedTime < 15000UL) //15.0 second(s)
  {
    if (pinState)
    {
      //digitalWrite(motorPin, HIGH);
      digitalWrite(pinI2, HIGH); //motorshield code
      pinState = false;
    }
  }
  else
  {
    //digitalWrite(motorPin, LOW);
    digitalWrite(pinI2, LOW); //motorshieild code
    pinState = true;
    feedPet = false;
  }
}

UKHeliBob

Why are you using a mixture of millis() timing and delay() timing ?  Using delay(5000) halts the program for 5 seconds and interferes with the millis() 1 second timing.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

mbasile

how do i go about changing the code so that it will not stop the program for 5 seconds? should i just chamge the delay to millis?

UKHeliBob

Quote
should i just chamge the delay to millis?
No, that won't work.  Do you understand how the date is printed every second using millis() for timing ?  Have you seen and understood the BlinkWithoutDelay example in the IDE ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

mbasile

im using that to help me but i am relatively new when it comes to coding, especially this kind out stuff, what should i look for in the blink without delay ide that will help me?

Delta_G

im using that to help me but i am relatively new when it comes to coding, especially this kind out stuff, what should i look for in the blink without delay ide that will help me?
The strategy and not so much the code itself.  Think about having nothing stop your loop and making all your timing decisions based on the last time something happened and the time now. 
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

mbasile

ive been trying for a while now with nothing to show for. everything i do either blinks both lines of the lcd or interrupts and delays the time on the bottom lcd

UKHeliBob

Have a look at Several things at the same time
Within loop() you can have as many independant tests of elapsed time as you like each with different periods and/or start times if necessary.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

MorganS

Think about the loop running thousands of times per second. Each time, it looks at the "wall clock" time to see if it is overdue to perform any action.

You don't stop the loop and go away from looking at the clock for 5 seconds, not if you want the other actions to continue. Any new action you add to the loop needs to use the same method.
GoForSmoke: "What GShield? You never mentioned a shield."

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy