Help with RTC and Matrix Keyboard

Hi there,

I am working on a pet feeder project and I use DS1307, LCD, Stepper motor and a 4x4 Matrix Keyboard. I use the keyboard to move from screen to screen on the LCD and by setting times that I want the stepper motor to work.

The problem is with the time sets. I tried only one set (and it doesn’t work) so that if it would work I just need to duplicate it.

This is the code I wrote till now:

  #include <LiquidCrystal.h>
  LiquidCrystal lcd(36, 34, 32, 30, 28, 26); 
  
  #include <Wire.h>
  #include "RTClib.h"

  RTC_DS1307 rtc;
  char timeset;
  char ar[4] [4];
  int i=0;
  char key;
  int screen=0 ;
  
  void message() // Activation Screen
  {
    lcd.begin(16, 2);
    lcd.setCursor(3, 0);
    lcd.print("Pet Feeder");
    lcd.setCursor(4, 1);
    lcd.print("Project");
    delay(3000);
    lcd.clear();
  }
  
  
 void screen_start() // Main Screen
 {
    DateTime now = rtc.now();
    if(now.hour() < 10) // If the hour is less than 10, put a zero.
    {
      lcd.print("0");
      lcd.setCursor(1, 0);
      lcd.print(now.hour(), DEC);
    }
    else
    {
      lcd.print(now.hour(), DEC);
    }
    lcd.setCursor(2, 0);
    lcd.print(":");
    lcd.setCursor(3, 0);
    if(now.minute() < 10)
    {
      lcd.print("0");
      lcd.setCursor(4, 0);
      lcd.print(now.minute(), DEC);
    }
    else
    {
      lcd.print(now.minute(), DEC);
    }
    lcd.setCursor(6, 0);
    if(now.day() < 10)
    {
      lcd.print("0");
      lcd.setCursor(7, 0);
      lcd.print(now.day(), DEC);
    }
    else
    {
      lcd.print(now.day(), DEC);
    }
    lcd.setCursor(8, 0);
    lcd.print("/");
    lcd.setCursor(9, 0);
    if(now.month() <10)
    {
      lcd.print("0");
      lcd.setCursor(10, 0);
      lcd.print(now.month(), DEC);
    }
    else
    {
      lcd.print(now.month(), DEC);
    }
    lcd.setCursor(11, 0);
    lcd.print("/");
    lcd.setCursor(12, 0);
    lcd.print(now.year() - 2000, DEC);
    lcd.setCursor(0, 1);
    lcd.print("Press any Button");
 }
  
  #include <Keypad.h>
  const byte COLS = 4; 
  const byte ROWS = 4;
  char keys[ROWS][COLS] = {
    {'1','2','3','A'},
    {'4','5','6','B'},
    {'7','8','9','C'},
    {'*','0','#','D'}
  };
  byte colPins[COLS] = {53, 51, 49, 47};
  byte rowPins[ROWS] = {45, 43, 41, 39};
  Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  
  #define IN1 4
  #define IN2 5
  #define IN3 6
  #define IN4 7

  void step_motor() // Stepper Motor Control
  {
    int steps = 0;
    while(steps<512)
    {
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, LOW);
      delay(10);
      steps++;
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, LOW);
      delay(10);
      steps++;
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, HIGH);
      digitalWrite(IN4, LOW);
      delay(10);
      steps++;
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
      delay(10);
      steps++;
    }
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
  }
  
  void array() // Insert Keys of Time Sets to Array
  {
     if(key)
     {
        switch(i);
         {
           lcd.setCursor(5+i, 1);
           if(i>=2)
           {
             lcd.setCursor(6+i,1);
           }
           lcd.print(key);
           ar [timeset-1] [i] = key;
           Serial.println(ar[timeset-1][i]);
           i++;
           if(i==4)
           {
              delay(500);
              lcd.clear();
              screen_start();
              screen = 0;
              i=0;
           }
         }
      }
  }
  
  void setup()
  {
    message();
    Serial1.begin(9600);
    Serial.begin(9600);
    #ifdef AVR
    Wire.begin();
    #else
    Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
    #endif
    rtc.begin();
    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);
    pinMode(IN3, OUTPUT);
    pinMode(IN4, OUTPUT);
  }
  
 void loop()
 {
  if(screen == 0)
  {
    screen_start();
  }

  key = keypad.getKey();
  if (key)
  {
    switch (screen)
    {
      case 0:  // Beginning Screen
        lcd.clear();
        screen = 1;  // Switch to Menu Screen
        lcd.print("Feed>A");
        lcd.setCursor(0, 1);
        lcd.print("Set Feed Times>B");
        break;

      case 1:  // Menu Screen Options
        if (key == 'A')
        {
          lcd.clear();
          lcd.setCursor(3, 0);
          lcd.print("Feeding...");
          lcd.setCursor(2, 1);
          lcd.print("Please Wait");
          step_motor();
          lcd.clear();
          screen_start();
          screen = 0;  // Go back to Beginning Screen
        }
        if (key == 'B')
        {
          screen = 2;  // Go to Set Feeding Times Screen
          lcd.clear();
          lcd.print("Choose Time Set:");
          lcd.setCursor(0, 1);
          lcd.print("1>  2>  3>  4>");
        }
        break;
      
      case 2:
        if(key == '1' || key == '2' || key == '3' || key == '4')
        {
           timeset = key;
           lcd.clear();
           lcd.setCursor(3, 0);
           lcd.print("Set Hour:");
           lcd.setCursor(5, 1);
           lcd.print("__:__");
           screen = 3; 
        }
        break;
      
      case 3:
        array();
        break;
    }
  }
   /*This is not working*/
        DateTime now = rtc.now();
        char hour1 = (ar[0][0]*10) + ar[0][1];
        char minute1 = (ar[0][2]*10) + ar[0][3];
        if(now.hour() == hour1 && now.minute() == minute1)
        {
          lcd.clear();
          lcd.setCursor(3, 0);
          lcd.print("Feeding...");
          lcd.setCursor(2, 1);
          lcd.print("Please Wait");
          step_motor();
          lcd.clear();
          screen_start();
          screen = 0;  // Go back to Beginning Screen
        }
}

So how can I solve this problem?

Thank you very much!

Try inserting some serial debug statements in the time set routine.

aarg: Try inserting some serial debug statements in the time set routine.

Sorry I am new with arduino. I don't understand what you mean. Can you explain please?

Thank you very much for your reply!

*Returning the topic to the first page.