Code effizienter machen


Ich schreibe gerade an einem Code von einer Wetterstation. Im laufe der Zeit bin ich jedoch darauf gestoßen, dass man doch bitte C-strings anstatt die string klasse und pointer verwenden soll, etc. .
Hier seht ihr einen Ausschnitt, bei dem ich die Inputs engegen nehme. Ich würde den Code gerne so effizient wie möglich machen, also schönen Code schreiben bzw. möglichst viel Speicher und RAM sparen.

const int magnet_pin = 2;  //digital
const int voltage_pin = 2;  //analog
const unsigned long package_frequency = 10000; 

class Windrad {

  float _voltage;
  int _measure_times;
  unsigned long _prev_millisVoltage;

  int _revolutions;
  bool _cooldown = false;

  unsigned long _prev_millisPackage;

    bool package_available;
    String package;

    void checkInputs();

Windrad::Windrad() {
  pinMode(magnet_pin, INPUT);

void Windrad::checkInputs() {
  if (millis() - _prev_millisVoltage > 100) {
    _voltage += analogRead(voltage_pin);
    _prev_millisVoltage = millis();

  if (digitalRead(magnet_pin) and _cooldown == false) {
    _cooldown = true;
  else if (!digitalRead(magnet_pin) and _cooldown == true){
    _cooldown = false;

  if (millis() - _prev_millisPackage >= package_frequency) {
    package_available = true;
    package = String(_voltage/_measure_times) + "," + String(_revolutions);
    _voltage = 0;
    _measure_times = 0;
    _revolutions = 0;
    _prev_millisPackage = millis();

Windrad windrad;

void setup() {

void loop() {

  if (windrad.package_available) {
    windrad.package_available = false;


wenn noch unklarheiten sind würde ich den Code noch kurz erläutern. Verbesserungen erwünscht!


This is the English board. If you know that you are not supposed to use the "String" class, why are you doing it anyway?

Oh, sorry. I did not see it. So I heard that its not recommended using the "String" class on Arduino but its common to use it on regular c++ code but why??? So it would be very nice to use my code to draw my attention to my mistakes, thx

Most Arduinos have limited memory, with poor garbage collection. With those, frequent use of String objects can lead to memory problems and program crashes. There is rarely, if ever a genuine need for Strings.

However, don't be surprised if a forum member drops by to tell you how wonderful Strings really are.