Changing Variables of a Class

Hello,

I'd like to use a Class to Transprot Data between ans ESP 8266 and nd Arduino Mega). The Transfer works as intended, but i wrote a Method to clear all Values of the Class instance, but it seems to have no effect. The Code for the Class is the following:

// TransferData.h

#ifndef TransferData_h
#define TransferData_h

#include "Arduino.h"

class TransferData {
  public:
    void clear();

    byte processByte;
    byte mode;
    byte brightness;
    byte hours;
    byte minutes;
    byte seconds;
    byte color;
    byte saturation;
};

#endif
// TransferData.cpp

#include "Arduino.h"
#include "TransferData.h"


void TransferData::clear(){
  processByte = NULL;
  mode = NULL;
  brightness = NULL;
  hours = NULL;
  minutes = NULL;
  seconds = NULL;
  color = NULL;
  saturation = NULL;
}

I never worte a lot of Code in C and thus am not experienced with this language. Do i need to use pointers here. If so, how would I use them.
Help would be greatly appreciated.
KInd regards!

i wrote a Method to clear all Values of the Class instance, but it seems to have no effect

How did you establish that it has no effect ?

Please post the full code of the class and your test sketch

Thanks for the Answer, the full Code is too long to post here, but i can give you the relevant Code Snippets.

Init of variable

TransferData recv_data;

Code which runs from void loop() on the Mega and receives the Values send over Serial from the ESP. If ii send the Class with processByte as 1 and Mode as 1 the first Time it shows exactly that output. If i then send proces Byte 2 and Brightness 5, it shows these as well, but also says that mode is still 1, which means that the clear() function does not work the way i intend

void printESPSerial() {
  //clearTransferStruct();

  recv_data.clear();
  
  if(myTransfer.available()) {
    Serial.println("Available Transfer");

    uint16_t recSize = 0;

    recSize = myTransfer.rxObj(recv_data, recSize);
    
    if (DEBUG_ESP_SERIAL) {
      Serial.print("\nProcessByte: "); Serial.print(recv_data.processByte);
      Serial.print("\nMode: "); Serial.print(recv_data.mode);
      Serial.print("\nBrightness: "); Serial.print(recv_data.brightness);
      Serial.print("\nHours: "); Serial.print(recv_data.hours);
      Serial.print("\nMinutes: "); Serial.print(recv_data.minutes);
      Serial.print("\nSeconds: "); Serial.print(recv_data.seconds);
      Serial.print("\nColor: "); Serial.print(recv_data.color);
      Serial.print("\nSaturation: "); Serial.print(recv_data.saturation);
    }
  }
}

If i need to clarify further, just tell me.

Kind Regards.

IronInt:
Thanks for the Answer, the full Code is too long to post here, but i can give you the relevant Code Snippets.

Then make a short example :wink: For example, we have NO idea wat myTransfer.available() does.

http://snippets-r-us.com/

PS Don't be surprised to find the bug yourself while making a MCVE :wink:

Ok so, I cant actually post the FULL Code in here except for in a file/ a directory, because of size limitations. To clarify myTransfer is declared as the following in the mega_sketch
Library used here is SerialTransfer by PowerBroker2: GitHub - PowerBroker2/SerialTransfer: Arduino library to transfer dynamic, packetized data fast and reliably via Serial, I2C, or SPI

#include <SerialTransfer.h>

// SerialTransfer
SerialTransfer myTransfer;

It is initialized in void setup() as follows

Serial3.begin(115200);
myTransfer.begin(Serial3);

I have attached the Full Code as Files in the Post, but i think its very difficult to read all of it because i didnt comment on it a lot. If needed I can attach all Cpp and Header Files, but i think that goes a bit too far.
If not tell me and ill post it.

esp_sketch.ino (3.89 KB)

mega_sketch.ino (19.4 KB)

TransferData.h (296 Bytes)

TransferData.cpp (247 Bytes)

IronInt:
Ok so, I cant actually post the FULL Code in here except for in a file/ a directory, because of size limitations.

As already requested in Reply #3, create and post an MRE. This is the smallest, complete code that demonstrates the problem you're seeing and only that problem. Leave out all code that is unrelated to the problem as it's simply clutter.

@IronInt, You forgot the M in MCVE....

If you have a problem, ALWAYS try to reduce the scope.

Ill try, give me a second

Using the following Code snippet my Code seems to work as intended, so i probably need to investigate how Serialtransfer stores Data inside my TransferData Object

#include "TransferData.h"

TransferData recv_data;


void setup() {
  Serial.begin(9600);
  recv_data.processByte = 1;
  recv_data.mode = 1;
}

void loop() {
  
  Serial.print("\nProcessByte: "); Serial.print(recv_data.processByte);
  Serial.print("\nMode: "); Serial.print(recv_data.mode);
  Serial.print("\nBrightness: "); Serial.print(recv_data.brightness);
  Serial.print("\nHours: "); Serial.print(recv_data.hours);
  Serial.print("\nMinutes: "); Serial.print(recv_data.minutes);
  Serial.print("\nSeconds: "); Serial.print(recv_data.seconds);
  Serial.print("\nColor: "); Serial.print(recv_data.color);
  Serial.print("\nSaturation: "); Serial.print(recv_data.saturation);

  recv_data.clear();

  delay(5000);
}

Sorry for bothering you.

septillion:
PS Don't be surprised to find the bug yourself while making a MCVE :wink:

:smiley:

But from what I can see of the code: for C there is NO defined "clear" state. A variable exists (and thus has a value) or it does not. You can threat a specific value as clear but that's up to you. For example, you could see 0 as cleared. Or call it 'NULL'. But 'NULL' is nothing special, it just sets the value (to 0 actually). It just indicates in code that a value of 'NULL' should be treated as "no valid data". But the actually doing so is fully up to you. Also, because 'NULL' is just defined as 0, actual valid data can never be 0 then, because you can't see any difference. So you need to make the data field wide enough to accept all possible data values + a "not valid" marker. Or, have a separate (bool) which you set (on writing valid data) or reset (on a clear) to indicate valid data or not.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.