This is a general enquiry about what people feel is a "Good" way to do something like the following....
I want to set aside some RAM to hold some bytes.
I want to be able to access those bytes within subroutines, with a minumum use of global variables.
I could do this in other languages! I just don't know "the right" way to do it in Arduino programming, or even if my approach is sensible.
Before I go into detail, a word about context...
The Sparkfun/ Innovations RFID reader transmits a burst of 14 bytes each time it sees a RFID tag.
Getting them displayed in the basic serial monitor is no big deal...
At the heart of that...
if (mySerialPort.available() > 0) {
// read the incoming byte from the serial buffer
incomingByte = mySerialPort.read();
// show what was received
Serial.print(incomingByte, DEC);
Serial.print(' ');
if (incomingByte==3) Serial.println();
(incomingByte will == 3 at the end of the bytes from a given tag)
I'd like to "catch" those bytes, store them someplace, and have easy access to them from other parts of the program.
I could, of course, establish a global array, access the array elements as needed.
It seemed a little "neater" to do something like the following, if possible....
At the top of the program, do something like....
byte myBuffer[6];
... to set aside 7 bytes of RAM where data could be stored.
Now, of course(?) you could fill the first byte with something like...
myBuffer[0]=27;
... in some, easy, circumstances.
I'd like to use that block of memory, though, less "directly"... primarily within subroutines.
Take for instance a subroutine to add 1 to each byte in the buffer... and we'll stipulate that no datum is 255, or if it is, rollover doesn't matter.
You could, of course, just use....
void AddOne
{
byte counter=0;
do {
myBuffer[counter]=myBuffer[counter]+1;
++counter;
} while (counter<8);
This "suffers" from the flaw that forces you to make changes within the subroutine if the buffer you want to process changes.
I was hoping there's a way to use... pointers?
Is there a way to, in old fashioned terms, "peek" and "poke" memory addresses? And to know where the buffer myBuffer is in memory, following the declaration given above? Why? Just for a start... suppose there were TWO such buffers, and you wanted to be able to add one to each element, with two calls of the same subroutine, just a different parameter...
Here's a rough, not- in- actual- Arduino- code idea of the subroutine that I hope is possible, with an example of it being called....
//Program with two buffers
byte myBufferA[6];
byte myBufferB[6];
... other stuff....
... somewhere in loop(), something that
... happens from time to time...
AddOneFancy(PmyBufferA);
AddOneFancy(PmyBufferB);
... rest of loop()
}//end of loop()
AddOneFancy(pointer PtToBuff)
{
byte counter=0;
do {
byte in memory location at address of PtToBuff + counter
=
that byte, plus 1;
++counter;
} while (counter<8);
}
I hope the idea comes though? The gap in my knowledge is with how I say something's a pointer, or maybe simple what data type to use in the header of AddOneFancy (in place of the word "pointer" there), and how to do the "peek" and the "poke" of and too the contents of that memory address...
Sorry if that didn't make sense. Thank you if you've struggled through... and have an answer for me!
WHY??? I think if I can crack this, I can write better general purpose routines for a bunch of things I want to do....