Getting stuck in a loop, makes core unresponsive...why?

Hi all, I seem to be having some trouble calling functions inside void loop{} instead of defining all my functionality inline. I was thinking these functions would really streamline my main code, and so far they have, but for some reason whenever I flash this firmware (or some variant thereof), I seem to get stuck in some loop which renders the Spark unresponsive to the cloud, meaning I can’t re-flash firmware. I have a feeling I’m going to get very sick of resetting the WiFi on this thing so I’d like to know what I’m doing wrong!

Previously, I had seen behavior like this with my clockcontrol.ino program, which I just copy-pasted in as a function, void clockcontrol(). So, I know that code on its own is fine, but in the clockcontrol.ino program, I didn’t create a function - all the code was just inline.

Note that the code below has some strange variables like “panicstatus”; don’t worry about that, it’s for another function that I chose not to include because it didn’t seem important to the discussion (removing it from void loop{} didn’t change the behavior).

#include "SparkTime/SparkTime.h"

UDP UDPClient;
SparkTime rtc;
int status = D0;
int correction = D1;
unsigned long unix;
int panicstatus = 0;
char tweet;
TCPClient client;
byte server[] = { 1, 1, 1, 1 }; // server

void setup()
{
    pinMode(correction, OUTPUT);
    digitalWrite(correction, LOW);
    pinMode(status, OUTPUT);
    rtc.begin(&UDPClient, "north-america.pool.ntp.org");
    rtc.setTimeZone(-7); // gmt offset
}

void loop()
{
    unix = updateunix();  // update unix time from NTP
    if(panicstatus==0)
    {
        if(rtc.second(unix)%2)  // flash D0 every other second
        {
            digitalWrite(status, HIGH);
        }
        else
        {
            digitalWrite(status, LOW);
        }
    }
    clockcontrol();  // function defined below
    if(rtc.minute(unix)%5)  // part of an implementation I'm working on with Twitter
    {
        sendgetrequest();  // GET from the server
        tweet = latesttweet();  // eventually this will read in text from the server to an array or string
    }
}

int updateunix()  // update unix time from NTP
{
    int u = rtc.now();
    return u;
}

void clockcontrol()  // analog clock time setting function
{
    unix = rtc.now();
    if(((rtc.second(unix)>=57 && rtc.minute(unix)==57) || (rtc.second(unix)<=3 && rtc.minute(unix)==58)) && (rtc.hour(unix)!=6 && rtc.hour(unix)!=18))
    {
        digitalWrite(correction, HIGH);
        unix = rtc.now();
    }
    if(((rtc.second(unix)>=57 && rtc.minute(unix)==57) || (rtc.second(unix)<=7 && rtc.minute(unix)==58)) && (rtc.hour(unix)==6 || rtc.hour(unix)==18))
    {
        digitalWrite(correction, HIGH);
        unix = rtc.now();
    }
    else digitalWrite(correction, LOW);
    unix = rtc.now();
}

void sendgetrequest()  // connect to server
{
    if (client.connect(server, 8081))
    {
        Serial.println("connected");
        client.println("GET /tweets.txt");
        client.println("Host: 1.1.1.1");
        client.println("Content-Length: 0");
        client.println();
    }
    else
    {
        panicstatus = 1;
    }
}

char latesttweet()  // read in latest text from server (work in progress)
{
    if (client.available())
    {
        char t;
        t = client.read();
        panicstatus = 0;
        return t;
    }
    if (!client.connected())
    {
        panicstatus = 2;
        client.stop();
        for(;;);
    }
}

Thanks,
Dan

Hi all – I think I may have answered my own question! For the benefit of others, I wanted to share my dumb mistake…

Each function needs to have a way to get out of it – this is the “return”. A function that has a type other than void always has a return of something in particular – even “main” in traditional C programming is an int function, and returns 0. What I forgot is that a void function can also have a return, but all you do is have something like this:

void somefunction
{
    stuff
    return;
}

Basically, the “return” gives the function a “safe exit” place (note that “exit” is an entirely different C operation!).

I’m still reworking some of my code, but it’s definitely working better now that I added some returns!

-Dan

1 Like

Glad you found the issue! :smiley: