Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: variable update::: general advice needed

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    4

    variable update::: general advice needed

    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

  2. #2
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,134
    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() {
    ....

  3. #3
    Junior Member
    Join Date
    Nov 2019
    Posts
    4
    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

  4. #4
    Junior Member
    Join Date
    Nov 2019
    Posts
    4
    okay sorry what about this::::

    analogread = x
    x=preeviousx
    print previousx

    how do you look at previousx?

    TY

  5. #5
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,134
    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

  6. #6
    Junior Member
    Join Date
    Nov 2019
    Posts
    4
    Quote Originally Posted by tonton81 View Post
    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

  7. #7
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,134
    Code:
    if ( Serial.available() ) {
      uint8_t receivedByte = Serial.read();
      if ( receivedByte != lastByte ) {
        //Do Something...
      }
      lastByte = receivedByte; // store the last byte as previous byte

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •