Arduino: Custom library produces strange behavior

miskohut

I am fairly new to arduino as well as C++. I want to make a plant watering system. I have found this tutorial to measure soil moisture. It's in Czech, but you can find a piece of code to handle the moist sensor. This program runs without any problems. I have decidede to enclose this algorithm to a class so it's more readable to me. I followed arduinos tutorial to create custom library.

MoistSensor.h

#ifndef MoistSensor_h
#define MoistSensor_h

#include "Arduino.h"

class MoistSensor {
  public:
    MoistSensor(int analogPin, int digitalPin, int vccPin);
    void measure();
    int getMoisture();
    void setup();
  private:
    int _analogPin;
    int _digitalPin;
    int _vccPin;
    int _moisture;
    void convertMoisture(int analogValue);
};
#endif

MoistSensor.cpp

#include "MoistSensor.h"
#include "Arduino.h"

MoistSensor::MoistSensor(int analogPin, int digitalPin, int vccPin) {
  _analogPin = analogPin;
  _digitalPin = digitalPin;
  _vccPin = vccPin;

  Serial.println("constructor ok: " + analogPin);
  Serial.println("constructor ok: " + digitalPin);
  Serial.println("constructor ok: " + vccPin);
};

int MoistSensor::getMoisture() {
  return _moisture;
};

void MoistSensor::measure() {
  digitalWrite(_vccPin, HIGH);
  delay(100);
  Serial.println("Value: " + analogRead(_analogPin));
  digitalWrite(_vccPin, LOW);
};

void MoistSensor::convertMoisture(int analogValue) {
  if (analogValue <= 230) {
    _moisture = 100;
  }
  else if (analogValue >= 650) {
    _moisture = 0;
  }
  else {
    _moisture = 100 - ((analogValue - 230 ) / 4.2);
  }
};

void MoistSensor::setup() {
  pinMode(_analogPin, INPUT);
  pinMode(_digitalPin, INPUT);
  pinMode(_vccPin, OUTPUT);
  digitalWrite(_vccPin, LOW);
};

Arduino source code:

#include <MoistSensor.h>

#define analogPin A0
#define digitalPin 3
#define vccPin 4

unsigned long time = 0;

MoistSensor sensor(analogPin, digitalPin, vccPin);

void setup() {
  Serial.begin(9600);
  sensor.setup();
}

void loop() {
  if (millis() - time > 3000) {
    
    sensor.measure();
    
    time = millis();
  }
}

The output produced by my program looks like this: enter image description here

I run in on Arduino NANO on processor ATmega328P (Old bootloader).

I have no idea what could be wrong.

john

So the problem is here

Serial.println("constructor ok: " + analogPin);

(and all the other places that you have similar code).

You obviously think that this is going to convert analogPin to a string and append it to "constructor ok: ". But that's not what happens.

Instead what you are getting is pointer arithmetic. The string literal "constructor ok: " is being converted to a pointer, and the value of analogPin is being added to that pointer. At best this results in you printing just part of the original string, at worst it results in complete garbage, or a crash.

Try this instead

Serial.print("constructor ok: ");
Serial.println(analogPin); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related