variable update::: general advice needed

Status
Not open for further replies.

aperson

Member
Hello,
I was hoping someone could tell me a way to retain information in a variable temporarily until it could be evaluated.
something like this :::

serial available
read byte
byte = previous byte

if byte != previous byte


How can you keep track of a change?

Thanks
 
A class static/auto or global variable is needed where you can copy the last byte to and check later on

Simply can put it before the void setup() line:

Code:
uint8_t lastByte = 0;

void setup() {
....
 
I read about that but I don't get it.

Lets say you say Byte = lastByte, how do you every make lastByte stop updating and look at the previous Byte?

Thanks
 
okay sorry what about this::::

analogread = x
x=preeviousx
print previousx

how do you look at previousx?

TY
 
After you compare it:

myByte == lastByte, or however you wish to compare, then set it to the new value:
lastByte = myByte;

Then your code repeats it
 
After you compare it:

myByte == lastByte, or however you wish to compare, then set it to the new value:
lastByte = myByte;

Then your code repeats it

I haven't tried that but I did figure out something that works:::

read = a
read = b
read = c
read = d
read = e
if a != b || c || d || e

something to that effect you can see when it changes and compare.

I'll try what you mentioned.. thanks
 
Code:
if ( Serial.available() ) {
  uint8_t receivedByte = Serial.read();
  if ( receivedByte != lastByte ) {
    //Do Something...
  }
  lastByte = receivedByte; // store the last byte as previous byte
 
Status
Not open for further replies.
Back
Top