Need help combining two sketches in Arduino

Hello, I’m having trouble running two sketches in my Arduino Mega simultaneously. The first is my RFID Door Access Control and the 2nd is my cayenne sketch. Both programs work standalone, but when i try to combine the two, cayenne isn’t working. New Text Document.txt (20.6 KB)

that is my RFID sketch, can someone please help me insert Cayenne sketch correctly

Hi @duanedtp, welcome to the Cayenne Community.

If the issue is ‘login timeout’ in the Arduino Serial Monitor when trying to maintain connection to Cayenne, it’s likely because of the use of delay() statements in loop(). The Arduino code we use to maintain connection with the cloud server doesn’t play nicely with anything more than a very modest delay.

You can use the SimpleTimer library to run items at repeating intervals or with delays.

#include <SimpleTimer.h>
SimpleTimer timer;

Ideally you should shoot for a sketch that only has

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

in it, and keep all other code outside of the loop() function.

can you please insert it into code for me, thank you. because i’m getting this error:

Arduino: 1.8.2 (Windows 10), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

C:\Users\TRADEM~1\AppData\Local\Temp\ccrWklQN.ltrans0.ltrans.o: In function `main’:

ccrWklQN.ltrans0.o:(.text.startup+0x97e): undefined reference to `SimpleTimer::setInterval(long, void (*)())’

ccrWklQN.ltrans0.o:(.text.startup+0xffc): undefined reference to `SimpleTimer::run()’

C:\Users\TRADEM~1\AppData\Local\Temp\ccrWklQN.ltrans4.ltrans.o: In function `global constructors keyed to 65535_0_EEPROM.ino.cpp.o.4451’:

ccrWklQN.ltrans4.o:(.text.startup+0x190): undefined reference to `SimpleTimer::SimpleTimer()’

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

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

Hi and thank you for the quick response. But I’m still a bit lost since i’m a noob and the delay functions seem to be used for different purpose in my RFID sketch. Basically when i scan an rfid tag or card to gain access to a room, the delay for set for how long the door should be opened for 3 seconds, 5 seconds, etc. If you would be so kind to open my sketch that i have attached and insert the neccessary “timer” codes and repost it.New Text Document.txt (20.6 KB)

I’m getting this in error logs

int setTimeout (long d, timer_callback f);

Could you share the updated code that generated that error. Was it on compilation? And were there any other errors displayed around it?

#include <EEPROM.h>
#include <SPI.h>
#include <MFRC522.h>
#include <SimpleTimer.h>
#include <CayenneEthernet.h>
#include <Ethernet.h>

//Create a single global SimpleTime object
SimpleTimer timer;

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

#define COMMON_ANODE

#ifdef COMMON_ANODE
#define LED_ON LOW
#define LED_OFF HIGH
#else
#define LED_ON HIGH
#define LED_OFF LOW
#endif

#define redLed 7
#define greenLed 6
#define blueLed 5

#define relay 4
#define wipeB 3

boolean match = false;
boolean programMode = false;
boolean replaceMaster = false;

int successRead;

byte storedCard[4];
byte readCard[4];
byte masterCard[4];

#define SS_PIN 53
#define RST_PIN 5
MFRC522 mfrc522(SS_PIN, RST_PIN);

///////////////////////////////////////// Setup ///////////////////////////////////
void setup() {
  //Arduino Pin Configuration
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(wipeB, INPUT_PULLUP);
  pinMode(relay, OUTPUT);
  //Be careful how relay circuit behave on while resetting or power-cycling your Arduino
  digitalWrite(relay, HIGH);		// Make sure door is locked
  digitalWrite(redLed, LED_OFF);	// Make sure led is off
  digitalWrite(greenLed, LED_OFF);	// Make sure led is off
  digitalWrite(blueLed, LED_OFF);	// Make sure led is off

  //Protocol Configuration
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  Cayenne.begin(token);
    
  mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);

  Serial.println(F("Access Control v3.4"));   // For debugging purposes
  ShowReaderDetails();	// Show details of PCD - MFRC522 Card Reader details

  if (EEPROM.read(1) != 143) {
    Serial.println(F("No Master Card Defined"));
    Serial.println(F("Scan A PICC to Define as Master Card"));
    do {
      successRead = getID();            // sets successRead to 1 when we get read from reader otherwise 0
      digitalWrite(blueLed, LED_ON);    // Visualize Master Card need to be defined
      timer.setTimeout(200, LED_ON);
      digitalWrite(blueLed, LED_OFF);
      timer.setTimeout(200, LED_OFF);
    }
    while (!successRead);                  // Program will not go further while you not get a successful read
    for ( int j = 0; j < 4; j++ ) {        // Loop 4 times
      EEPROM.write( 2 + j, readCard[j] );  // Write scanned PICC's UID to EEPROM, start from address 3
    }
    EEPROM.write(1, 143);                  // Write to EEPROM we defined Master Card.
    Serial.println(F("Master Card Defined"));
  }
  Serial.println(F("-------------------"));
  Serial.println(F("Master Card's UID"));
  for ( int i = 0; i < 4; i++ ) {          // Read Master Card's UID from EEPROM
    masterCard[i] = EEPROM.read(2 + i);    // Write it to masterCard
    Serial.print(masterCard[i], HEX);
  }
  Serial.println("");
  Serial.println(F("-------------------"));
  Serial.println(F("Everything Ready"));
  Serial.println(F("Waiting PICCs to be scanned"));
  cycleLeds();    // Everything ready lets give user some feedback by cycling leds
}


///////////////////////////////////////// Main Loop ///////////////////////////////////
void loop () {
  do {
    successRead = getID(); 	// sets successRead to 1 when we get read from reader otherwise 0
    if (digitalRead(wipeB) == LOW) {
      digitalWrite(blueLed, LED_OFF);  // Make sure led is off
      digitalWrite(greenLed, LED_ON);  // Make sure led is off
      digitalWrite(redLed, LED_OFF); // Make sure led is off
      Serial.println(F("Wipe Button Pressed"));
      Serial.println(F("Master Card will be Erased! in 10 seconds"));
//      timer.setTimeout(10000);
      if (digitalRead(wipeB) == LOW) {
        EEPROM.write(1, 0);                  // Reset Magic Number.
        Serial.println(F("Restart device to re-program Master Card"));
        while (1);
      }
    }
    if (programMode) {
      cycleLeds();              // Program Mode cycles through RGB waiting to read a new card
    }
    else {
      normalModeOn(); 		// Normal mode, blue Power LED is on, all others are off
    }
  }
  while (!successRead); 	//the program will not go further while you not get a successful read
  if (programMode) {
    if ( isMaster(readCard) ) { //If master card scanned again exit program mode
      Serial.println(F("Master Card Scanned"));
      Serial.println(F("Exiting Program Mode"));
      Serial.println(F("-----------------------------"));
      programMode = false;
      return;
    }
    else {
      if ( findID(readCard) ) { // If scanned card is known delete it
        Serial.println(F("I know this PICC, removing..."));
        deleteID(readCard);
        Serial.println("-----------------------------");
        Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
      }
      else {                    // If scanned card is not known add it
        Serial.println(F("I do not know this PICC, adding..."));
        writeID(readCard);
        Serial.println(F("-----------------------------"));
        Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
      }
    }
  }
  else {
    if ( isMaster(readCard)) {  	// If scanned card's ID matches Master Card's ID enter program mode
      programMode = true;
      Serial.println(F("Hello Master - Entered Program Mode"));
      int count = EEPROM.read(0); 	// Read the first Byte of EEPROM that
      Serial.print(F("I have "));    	// stores the number of ID's in EEPROM
      Serial.print(count);
      Serial.print(F(" record(s) on EEPROM"));
      Serial.println("");
      Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
      Serial.println(F("Scan Master Card again to Exit Program Mode"));
      Serial.println(F("-----------------------------"));
    }
    else {
      if ( findID(readCard) ) {	// If not, see if the card is in the EEPROM
        Serial.println(F("ACCESS GRANTED"));
        granted(300);        	// Open the door lock for 300 ms
      }
      else {			// If not, show that the ID was not valid
        Serial.println(F("ACCESS DENIED"));
        denied();
        }
    }
      Cayenne.run();
      timer.run();
      }
}

/////////////////////////////////////////  Access Granted    ///////////////////////////////////
void granted ( int setInterval) {
  digitalWrite(greenLed, LED_ON); 	// Turn off blue LED
  digitalWrite(redLed, LED_ON); 	// Turn off red LED
  digitalWrite(blueLed, LED_OFF); 	// Turn on green LED
  digitalWrite(relay, HIGH); 		// Unlock door!
//  timer.setTimeout(3000, relay, HIGH); 					// Hold door lock open for given seconds
  digitalWrite(relay, LOW); 		// Relock door
//  timer.setTimeout(3000, relay, LOW); 						// Hold green LED on for a second
}

///////////////////////////////////////// Access Denied  ///////////////////////////////////
void denied() {
  digitalWrite(redLed, LED_ON); 	// Make sure green LED is off
  digitalWrite(blueLed, LED_ON); 	// Make sure blue LED is off
  digitalWrite(greenLed, LED_OFF); 	// Turn on red LED
}


///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
int getID() {
  // Getting ready for Reading PICCs
  if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {   //Since a PICC placed get Serial and continue
    return 0;
  }
  // There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
  // I think we should assume every PICC as they have 4 byte UID
  // Until we support 7 byte PICCs
  Serial.println(F("Scanned PICC's UID:"));
  for (int i = 0; i < 4; i++) {  //
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
  }
  Serial.println("");
  mfrc522.PICC_HaltA(); // Stop reading
  return 1;
}

void ShowReaderDetails() {
  // Get the MFRC522 software version
  byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
  Serial.print(F("MFRC522 Software Version: 0x"));
  Serial.print(v, HEX);
  if (v == 0x91)
    Serial.print(F(" = v1.0"));
  else if (v == 0x92)
    Serial.print(F(" = v2.0"));
  else
    Serial.print(F(" (unknown),probably a chinese clone?"));
  Serial.println("");
  // When 0x00 or 0xFF is returned, communication probably failed
  if ((v == 0x00) || (v == 0xFF)) {
    Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
    Serial.println(F("SYSTEM HALTED: Check connections."));
    while (true); // do not go further
  }
}

///////////////////////////////////////// Cycle Leds (Program Mode) ///////////////////////////////////
void cycleLeds() {
  digitalWrite(redLed, LED_OFF); 	// Make sure red LED is off
  digitalWrite(greenLed, LED_ON); 	// Make sure green LED is on
  digitalWrite(blueLed, LED_OFF); 	// Make sure blue LED is off
  delay(200);
  digitalWrite(redLed, LED_OFF); 	// Make sure red LED is off
  digitalWrite(greenLed, LED_OFF); 	// Make sure green LED is off
  digitalWrite(blueLed, LED_ON); 	// Make sure blue LED is on
  delay(200);
  digitalWrite(redLed, LED_ON); 	// Make sure red LED is on
  digitalWrite(greenLed, LED_OFF); 	// Make sure green LED is off
  digitalWrite(blueLed, LED_OFF); 	// Make sure blue LED is off
  delay(200);
}

//////////////////////////////////////// Normal Mode Led  ///////////////////////////////////
void normalModeOn () {
  digitalWrite(greenLed, LED_OFF); 	// Blue LED ON and ready to read card
  digitalWrite(redLed, LED_ON); 	// Make sure Red LED is off
  digitalWrite(blueLed, LED_ON); 	// Make sure Green LED is off
  digitalWrite(relay, HIGH); 		// Make sure Door is Locked
}

//////////////////////////////////////// Read an ID from EEPROM //////////////////////////////
void readID( int number ) {
  int start = (number * 4 ) + 2; 		// Figure out starting position
  for ( int i = 0; i < 4; i++ ) { 		// Loop 4 times to get the 4 Bytes
    storedCard[i] = EEPROM.read(start + i); 	// Assign values read from EEPROM to array
  }
}

///////////////////////////////////////// Add ID to EEPROM   ///////////////////////////////////
void writeID( byte a[] ) {
  if ( !findID( a ) ) { 		// Before we write to the EEPROM, check to see if we have seen this card before!
    int num = EEPROM.read(0); 		// Get the numer of used spaces, position 0 stores the number of ID cards
    int start = ( num * 4 ) + 6; 	// Figure out where the next slot starts
    num++; 								// Increment the counter by one
    EEPROM.write( 0, num ); 		// Write the new count to the counter
    for ( int j = 0; j < 4; j++ ) { 	// Loop 4 times
      EEPROM.write( start + j, a[j] ); 	// Write the array values to EEPROM in the right position
    }
    successWrite();
    Serial.println(F("Succesfully added ID record to EEPROM"));
  }
  else {
    failedWrite();
    Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
  }
}

///////////////////////////////////////// Remove ID from EEPROM   ///////////////////////////////////
void deleteID( byte a[] ) {
  if ( !findID( a ) ) { 		// Before we delete from the EEPROM, check to see if we have this card!
    failedWrite(); 			// If not
    Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
  }
  else {
    int num = EEPROM.read(0); 	// Get the numer of used spaces, position 0 stores the number of ID cards
    int slot; 			// Figure out the slot number of the card
    int start;			// = ( num * 4 ) + 6; // Figure out where the next slot starts
    int looping; 		// The number of times the loop repeats
    int j;
    int count = EEPROM.read(0); // Read the first Byte of EEPROM that stores number of cards
    slot = findIDSLOT( a ); 	// Figure out the slot number of the card to delete
    start = (slot * 4) + 2;
    looping = ((num - slot) * 4);
    num--; 			// Decrement the counter by one
    EEPROM.write( 0, num ); 	// Write the new count to the counter
    for ( j = 0; j < looping; j++ ) { 				// Loop the card shift times
      EEPROM.write( start + j, EEPROM.read(start + 4 + j)); 	// Shift the array values to 4 places earlier in the EEPROM
    }
    for ( int k = 0; k < 4; k++ ) { 				// Shifting loop
      EEPROM.write( start + j + k, 0);
    }
    successDelete();
    Serial.println(F("Succesfully removed ID record from EEPROM"));
  }
}

///////////////////////////////////////// Check Bytes   ///////////////////////////////////
boolean checkTwo ( byte a[], byte b[] ) {
  if ( a[0] != 0 ) 			// Make sure there is something in the array first
    match = true; 			// Assume they match at first
  for ( int k = 0; k < 4; k++ ) { 	// Loop 4 times
    if ( a[k] != b[k] ) 		// IF a != b then set match = false, one fails, all fail
      match = false;
  }
  if ( match ) { 			// Check to see if if match is still true
    return true; 			// Return true
  }
  else  {
    return false; 			// Return false
  }
}

///////////////////////////////////////// Find Slot   ///////////////////////////////////
int findIDSLOT( byte find[] ) {
  int count = EEPROM.read(0); 			// Read the first Byte of EEPROM that
  for ( int i = 1; i <= count; i++ ) { 		// Loop once for each EEPROM entry
    readID(i); 								// Read an ID from EEPROM, it is stored in storedCard[4]
    if ( checkTwo( find, storedCard ) ) { 	// Check to see if the storedCard read from EEPROM
      // is the same as the find[] ID card passed
      return i; 				// The slot number of the card
      break; 					// Stop looking we found it
    }
  }
}

///////////////////////////////////////// Find ID From EEPROM   ///////////////////////////////////
boolean findID( byte find[] ) {
  int count = EEPROM.read(0);			// Read the first Byte of EEPROM that
  for ( int i = 1; i <= count; i++ ) {  	// Loop once for each EEPROM entry
    readID(i); 					// Read an ID from EEPROM, it is stored in storedCard[4]
    if ( checkTwo( find, storedCard ) ) {  	// Check to see if the storedCard read from EEPROM
      return true;
      break; 	// Stop looking we found it
    }
    else {  	// If not, return false
    }
  }
  return false;
}

///////////////////////////////////////// Write Success to EEPROM   ///////////////////////////////////
// Flashes the green LED 3 times to indicate a successful write to EEPROM
void successWrite() {
  digitalWrite(blueLed, LED_OFF); 	// Make sure blue LED is off
  digitalWrite(redLed, LED_OFF); 	// Make sure red LED is off
  digitalWrite(greenLed, LED_OFF); 	// Make sure green LED is on
  delay(200);
  digitalWrite(greenLed, LED_ON); 	// Make sure green LED is on
  delay(200);
  digitalWrite(greenLed, LED_OFF); 	// Make sure green LED is off
  delay(200);
  digitalWrite(greenLed, LED_ON); 	// Make sure green LED is on
  delay(200);
  digitalWrite(greenLed, LED_OFF); 	// Make sure green LED is off
  delay(200);
  digitalWrite(greenLed, LED_ON); 	// Make sure green LED is on
  delay(200);
}

///////////////////////////////////////// Write Failed to EEPROM   ///////////////////////////////////
// Flashes the red LED 3 times to indicate a failed write to EEPROM
void failedWrite() {
  digitalWrite(blueLed, LED_OFF); 	// Make sure blue LED is off
  digitalWrite(redLed, LED_OFF); 	// Make sure red LED is off
  digitalWrite(greenLed, LED_OFF); 	// Make sure green LED is off
  delay(200);
  digitalWrite(redLed, LED_ON); 	// Make sure red LED is on
  delay(200);
  digitalWrite(redLed, LED_OFF); 	// Make sure red LED is off
  delay(200);
  digitalWrite(redLed, LED_ON); 	// Make sure red LED is on
  delay(200);
  digitalWrite(redLed, LED_OFF); 	// Make sure red LED is off
  delay(200);
  digitalWrite(redLed, LED_ON); 	// Make sure red LED is on
  delay(200);
}

///////////////////////////////////////// Success Remove UID From EEPROM  ///////////////////////////////////
// Flashes the blue LED 3 times to indicate a success delete to EEPROM
void successDelete() {
  digitalWrite(blueLed, LED_OFF); 	// Make sure blue LED is off
  digitalWrite(redLed, LED_OFF); 	// Make sure red LED is off
  digitalWrite(greenLed, LED_OFF); 	// Make sure green LED is off
  delay(200);
  digitalWrite(blueLed, LED_ON); 	// Make sure blue LED is on
  delay(200);
  digitalWrite(blueLed, LED_OFF); 	// Make sure blue LED is off
  delay(200);
  digitalWrite(blueLed, LED_ON); 	// Make sure blue LED is on
  delay(200);
  digitalWrite(blueLed, LED_OFF); 	// Make sure blue LED is off
  delay(200);
  digitalWrite(blueLed, LED_ON); 	// Make sure blue LED is on
  delay(200);
}

////////////////////// Check readCard IF is masterCard   ///////////////////////////////////
// Check to see if the ID passed is the master programing card
boolean isMaster( byte test[] ) {
  if ( checkTwo( test, masterCard ) )
    return true;
  else
    return false;
}`

are you there? @rsiegel

Hi, I’m in the office today, I’ll have a look at this soon. I did edit out the Cayenne token so no one else tries to connect a device to your dashboard.

Ok, I think I see what’s creating that error now, thanks for your patience.

The timer.setTimeout() function expects 2 arguments, a time value in milliseconds, and a function to be called after that time has elapsed.

So the compiler is complaining that in timer.setTimeout(200, LED_ON); that LED_ON is not a function.

this is giving me too much of a headache. Basically i wanted to incorporate cayenne into my project on the arduino so I can unlock the door from my phone if needed. But i think i will use my RasPi instead to handle Cayenne. Thnx anyways.