Time based application

This sketch is one of two boards (the other is an Arduino Uno) that opens a can of wet cat food.
The goal of this sketch is to feed the cat each day at a specific time, as well as have a push button available in case an additional feeding is wanted.
The push button is working fine, but the time-based part of the program is not working.

Can you tell what I may have done wrong?

#include "Particle.h" // Include Particle library for time functions

// Pin definitions
int Push1 = D0;
int Push2 = D1;
int Clamper1 = D2;
int Clamper2 = D3;
int TurnCan = D4;
int StartArduinoProgram = D5;
int StartPhotonProgram = D6;

bool hasRunToday = false; // Flag to ensure the program runs only once 

void setup() {
    pinMode(Push1, OUTPUT);
    pinMode(Push2, OUTPUT);
    pinMode(Clamper1, OUTPUT);
    pinMode(Clamper2, OUTPUT);
    pinMode(TurnCan, OUTPUT);
    pinMode(StartArduinoProgram, OUTPUT);
    pinMode(StartPhotonProgram, INPUT_PULLUP);

    digitalWrite(Push1, LOW);  
    digitalWrite(Push2, LOW);  
    digitalWrite(Clamper1, HIGH);
    digitalWrite(Clamper2, HIGH);
    digitalWrite(TurnCan, HIGH);
    digitalWrite(StartArduinoProgram, HIGH);
    
    // Publish a message confirming successful setup
    Particle.publish("SetupComplete", "Particle Photon is set up and ready!", PRIVATE);
}

void loop() {
    // Publish current time for debugging
    String currentTime = String(Time.hour()) + ":" + String(Time.minute());
    Particle.publish("CurrentTime", currentTime, PRIVATE); 

    // Reset the daily flag at midnight
    if (Time.hour() == 0 && Time.minute() == 0) {
        hasRunToday = false;
    }

    // Check if it's time to start the program (6:35 AM daily)
    if (TimeToStart() && !hasRunToday) {
        hasRunToday = true; // Mark as run for today
        startProgram();
    }

    // Check if the pushbutton is pressed to manually start the program
    if (digitalRead(StartPhotonProgram) == LOW) { // Button pressed
        delay(50); // Debounce delay
        if (digitalRead(StartPhotonProgram) == LOW) { // Confirm button is still pressed
            Particle.publish("ButtonPressed", "Manual start requested!", PRIVATE);
            startProgram();
        }
    }
}

// Function to check if it's time to start the program (6:35 AM daily)
bool TimeToStart() {
    return (Time.hour() == 6 && Time.minute() == 35);
}

void startProgram() {
    // Send a start signal to the Arduino 
    digitalWrite(StartArduinoProgram, LOW);
    delay(500);
    digitalWrite(StartArduinoProgram, HIGH);
    
    // Run the Photon program
    runProgram();
}

// Function to execute the Photon program
void runProgram() {
    delay(9000);    
    digitalWrite(Push1, HIGH); // Pusher forward
    digitalWrite(Push2, HIGH);
    delay(1000 * 21);
    digitalWrite(Push1, LOW); // Pusher backward
    digitalWrite(Push2, LOW);
    delay(1000 * 4);
    digitalWrite(Clamper1, LOW); // Clamper1 Down
    digitalWrite(Clamper2, LOW);
    delay(1000 * 3.5); 
    digitalWrite(TurnCan, LOW); // Turn can start
    delay(1000 * 21); 
    digitalWrite(TurnCan, HIGH); // Turn can stop
    digitalWrite(Clamper1, HIGH); // Release Clamper
    digitalWrite(Clamper2, HIGH);
    delay(9000);
    
    digitalWrite(StartArduinoProgram, HIGH);
}

I would add logging calls and monitor the USB serial debug log to see what it's doing.

Though one thing: Time calls on Particle at at UTC, so keep in mind that the hour is not your local time.

If you want to use local time and especially if you want daylight saving support, the LocalTimeRK library can help.

Hi Brent, this call above is going to get your device in trouble in the long run. It will consume many of your data ops for the month publishing at every second.

2 Likes

can you elaborate on how it does not work?