how to create a buffer? #76
Answered
by
rlogiacco
TheRealDestroyer
asked this question in
Q&A
-
im new to this library and was wondering how to convert this into a buffer. int Code = 0; // the ir code recived
int refCode = 1; // same as prevCode- just kept for longer
int prevCode = 2; // the previous ir code recived
if (irrecv.decode(&results)) {
refCode = prevCode; // Sets refCode to the same value as prevCode so i keep refCode to peer 3 codes into the past
prevCode = Code; // Sets prevCode to the same value as Code so i keep prevCode to peer 2 codes into the past
Code = results.value; // Sets Code to the same value as results.value so i keep Code to peer at last recived code
Serial.println(Code);
Serial.println(prevCode);
Serial.println(refCode);
irrecv.resume();
} |
Beta Was this translation helpful? Give feedback.
Answered by
rlogiacco
Feb 14, 2023
Replies: 1 comment
-
You don't really need a circular buffer in your case because you apparently are receiving only two ir codes, anyway, assuming you do not want to store more than 100 codes and you are willing to blindly discard the oldest ones in case you receive more than 100: #include <CircularBuffer.h>
#include <IRremote.h>
CircularBuffer<unsigned long, 100> buffer;
void main() {
// omitted
}
void loop() {
decode_results results;
if (irrecv.decode(&results)) {
buffer.push(results.value); // adds the received value to the tail of buffer
irrecv.resume();
}
// here your buffer can have any number of entries and you can decide what to do and when to process them
// as an example, to spit out on the console when you have filled up the buffer (100 values)
if (buffer.isFull()) {
Serial.println("Received 100 signals:");
while (!buffer.isEmpty()) {
Serial.println(buffer.shift()); // extracts one value from the head of the buffer and prints it (the value is now lost)
}
Serial.println("START AGAIN");
}
} If you look at the README and the examples you can find all you need |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
rlogiacco
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You don't really need a circular buffer in your case because you apparently are receiving only two ir codes, anyway, assuming you do not want to store more than 100 codes and you are willing to blindly discard the oldest ones in case you receive more than 100: