Need help combining two sketches in Arduino

I should have mentioned that you need to add the SimpleTimer library to your Arduino IDE via Tools > Include Library > Manage Libraries, then searching for SimpleTimer.

Here is a contrived example.

Lets say I want to repeat two lines of text, one each every 5 seconds, while staying connected to Cayenne. This would be the bad way to do it, with delay() statements, since they will interfere with the ‘heartbeat’ that we use to stay connected to the cloud server:

//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneEthernet.h>

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = “yourtokenhere”;

void setup()
{
	Serial.begin(9600);
	Cayenne.begin(token);
}

void loop()
{
  Cayenne.run();
  Serial.println("This should print first,  then wait 5 seconds");
  // Wait 5 seconds
  delay(5000);
  Serial.println("This should print second,  then wait 5 seconds");
  // Wait 5 seconds
  delay(5000);
}

Instead, I’ll use a SimpleTimer object called timer to create the same behavior, then use two different functions of timer to accomplish this:

setInterval(w, x) where w is the amount of time to wait in between repeated runs of function x
setTimeout(y, z) where y is the amount of time to wait before running function z only a single time.

This keeps the main loop() function free of anything except Cayenne.run(); and timer.run();

//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space

#include <SimpleTimer.h>
#include <CayenneEthernet.h>

//Create a single global SimpleTimer object named timer
SimpleTimer timer;

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "yourtokenhere";

void setup()
{
	Serial.begin(9600);
	Cayenne.begin(token);
  
  //after 5 seconds have passed,  call OnceOnlyTask() a single time
  timer.setTimeout(5000, OnceOnlyTask);
  
  //after 10 seconds have passed,  call OnceOnlyTask2() a single time
  timer.setTimeout(10000, OnceOnlyTask2);
  
  //These space the two intervals out so we only get once print every 5 seconds,  alternating.
}

void loop()
{
  Cayenne.run();
   timer.run();
}

// Call function PrintFirstLine() every 10 seconds
void OnceOnlyTask() {
  timer.setInterval(10000, PrintFirstLine);  
}  

// Call function PrintSecondLine() every 10 seconds
void OnceOnlyTask2() {
  timer.setInterval(10000, PrintSecondLine);  
}  


// function to be called repeatedly
void PrintFirstLine() {
  Serial.println("This should print first,  then wait 5 seconds");    
}

// function to be called repeatedly
void PrintSecondLine() {
  Serial.println("This should print second,  then wait 5 seconds");    
}

I chose this example mostly to show how SimpleTimer can be used to replace both delay() statements, as well as anything that needs to be looped at a specific interval.

2 Likes