Only publishes 4x and Time based event not functional

Hello, I’ve been attempting to make an alarm clock that automatically turns off after a light in the room is switched on. In order to test if monitering of the light switch was functional, I made a couple Spark.publish() functions so it would publish if the light was on or off, and if the timing function has worked. However, once I flash the code over to the Spark Core, the only thing that occurs on the dashboard is 4 instances of the light being off. I have also allowed the Core to run through a set time, which ideally, once it hit this time an event would be published saying “the time has worked”. A screenshot of the dashboard is shown below. Is there any issues in my code that may be causing these issues? I am very new with this as a warning :slight_smile:

The actual code for it starts here:

int buzzerin = D0;
int lightin = A0;
int lightout = A1;
int on;
int off;
int threshhold;         //light threshhold
int loudness = 50;   //loudness of buzzer

int hour=     12;     //set time
int min =     30;

bool happenedtoday = false;

void setup() {
    
    Spark.syncTime();  
    Time.zone(-5);
    
    pinMode(buzzerin,OUTPUT);
    pinMode(lightin,OUTPUT);
    pinMode(lightout,INPUT);

    
    analogWrite(buzzerin,loudness);
    delay(500);
    analogWrite(buzzerin,0);
    delay(1000);
    
    on = analogRead(lightout);              //light on value
    delay(1500);
    
    
    analogWrite(buzzerin,loudness);
    delay(500);
    analogWrite(buzzerin,0);
    delay(1000);
    
    off = analogRead(lightout);             //light off value
    delay(1500);
    
    //calculating threshhold
    
    threshhold = ((on+off)/2);
    
}

void loop() {
    
    //Lettuce put some timing together

    if (Time.hour() == 12  && Time.minute() == 50) {
        Spark.publish("the time has worked");
    }
        

    else if (analogRead(lightout)>threshhold) {
        Spark.publish("the light is off");
    }
    
    else if (analogRead(lightout)<threshhold) {
        Spark.publish("the light is on");
    }

Thank you so much

@jtarlo, the way your loop() works, you will be doing a Spark.publish() for the entire minute between 12:50 and 12:51. The same will apply for you lighting conditions. Since there is no delay in your loop(), you will immediately exceed the 1 publish per second limit, and the 4 per second burst limit. You need to change your test conditions to be more specific or to only fire once per change and you need to add some delay in your loop to prevent publishes more often then 1 per second. :smile:

1 Like

Great I got it. Is there anything that you see that is an issue with the time aspect of the program?

@jtarlo, there is not need for Spark.syncTime() in setup() since that is done automatically on bootup. You may want to wait for a valid time to sync from the Cloud in setup(). I recall being good practice on the Core. Here is a topic that discussed how:

Try to avoid using delay() in loop() and use millis() based delay instead which is non-blocking (let me know if you need an example). For time based “alarms”, there is a timerAlarms library available. :smile:

I inserted the function that was the solution to the Time Sync into my program, however it is still not recognizing this function: – if (Time.hour() == 12 && Time.minute() == 50) { – as the current time reaches the set time in the function. I rerouted it to an led to make sure its not another publish error but no luck.

@jtarlo, you are testing for hours and minutes so the time will “appear” the same for one minute! You need to rethink how you test for the time so it doesn’t keep “firing”. The same applies to your other tests. One possibility is to also include seconds in your test and only sample the time every second.

Updated code… it should only go through the process and fire every second. I added in some stuff to check if it was another issue that was preventing it to occur but still nothing. As you can see I am very new… I still have no idea why it isn’t quite working since other codes I have seen use the same type of check of the time

void loop() {

if (millis()%1000 == 0) {
    if (Time.hour() == 9  && Time.minute() == 25 && Time.second() == 00 && Time.isAM() == false) {
        Spark.publish("the time has worked");
        digitalWrite(buzzerin,HIGH);
        delay(1000);
        digitalWrite(buzzerin,LOW);
    }
}