Replacd dalay() with timer

This commit is contained in:
Cory
2024-12-12 01:14:47 -05:00
parent a61cd98d87
commit b5cda6e963
3 changed files with 57 additions and 8 deletions
+8 -8
View File
@@ -14,7 +14,7 @@ By: mxcory
#include "structs.h"
#include "configuration.h"
#include "timer.h"
//Initiate transceiver object
RF24 radio(CE_PIN, CSN_PIN);
@@ -51,15 +51,16 @@ void setup() {
radio.stopListening(); //Put radio in TX mode
#include "configuration_input.h" //Input configuration
timerSetup();
}
void loop() {
unsigned long MSG_TIMER_START = micros(); //Begin message timer
bool report = radio.write(&payload, sizeof(payload)); //Transmit message
unsigned long MSG_TIMER_END = micros(); //End message timer
if (timer(BROADCAST_DELAY, 0)) {
unsigned long MSG_TIMER_START = micros(); //Begin message timer
bool report = radio.write(&payload, sizeof(payload)); //Transmit message
unsigned long MSG_TIMER_END = micros(); //End message timer
#include "configuration_input.h"
#ifdef DEBUG_ME
@@ -87,9 +88,8 @@ void loop() {
} else {
Serial.println( "Transmission failed or timed out" );
}
}
#endif
setMessage(); //Prepare value that will be sent
delay(250);
}
+3
View File
@@ -21,6 +21,9 @@ char TID = 'BB'; //RX device ID
//Number of channels
#define NUM_CHANNELS 10
//How long to delay broadcasting in milliseconds
#define BROADCAST_DELAY 250
//Byte array for inputs
int TX_CHANNELS[ NUM_CHANNELS+1 ][ 10 ] = {0};
+46
View File
@@ -0,0 +1,46 @@
//Timing
#define countDataAmount 1 //Number of timers
unsigned long countData[countDataAmount] = {0}; //Holds count information. (Adjust for numeber of timers needed.)
/* Timers
0 - Broacast rate
*/
//Used by timer function
unsigned long millisCount(int _mode, int _id) { //_mode: 0-Start 1-Stop | _id Identity number (allow more by editing the length of countData
unsigned long _count;
if (_mode == 0) {
countData[_id]= millis();
return 0;
}
if (_mode == 1) {
_count = millis() - countData[_id];
return _count;
}
}
//Will set all variables in timer table to 0.
void timerSetup() {
for (int i=0;i<countDataAmount;i++) {
millisCount(0,i);
}
}
//Use this to check if set time has passed.
//Will return true or false
bool timer(unsigned long _interval,int _id, bool _reset = true) { //_interval in millis, _id in countData
if (millisCount(1,_id) >= _interval) {
if (_reset) { //If true, reset timer after tripping
millisCount(0,_id);
}
return true;
} else {
return false;
}
}
void timerReset(byte _id) { //Reset timer start time
countData[_id]= millis();
}