Hello,
I’ve been having some difficulties with a class I made and I’m stumped… My variables don’t seem to be storing their respective data. here is my class:
#ifndef WaterSensor_h
#define WaterSensor_h
#include "Arduino.h"
class WaterSensor {
public:
WaterSensor();
String helloworld();
void set(int pin, bool muxBool,int muxPin);//if either muxPin or pin doesn't apply enter arbitrary number, will not effect readings
double getVWC();
double getMilliVolts();
void changeMilliVoltDenominator(double arg);
void changeReferenceMilliVolt(double arg);
void setPin();
double sensorReadingFunction();
void setSystemNumber(int x);
void changeMuxArduinoPin(int pin);
int getSystemNumber();
void changeMuxSelectorPins(int select0, int select1, int select2, int select3);
protected:
private:
double vwc;
double _milliVolts;
double populateMilliVolts();
double populateVWC();
double milliVoltDenominator;
double referenceMilliVolt;
int _pin;
void readMux();
double _sensorReading;
bool _muxBool;
int _muxPin;
int muxArduinoPin;
int systemNumber;
int S0;
int S1;
int S2;
int S3;
};
#endif
/*
WaterSensor.cpp - Library for communicating with analog Decagon soil Sensor.
Created by Matt Hart, April 2, 2017.
Released as Private.
*/
#include "Arduino.h"
#include "WaterSensor.h"
WaterSensor::WaterSensor(){
double milliVoltDenominator=4095.0;
double referenceMilliVolt=1.65;
double vwc;
double _milliVolts;
double _sensorReading;
int muxArduinoPin=A6;
int S0=2;
int S1=3;
int S2=4;
int S3=5;
}
String WaterSensor::helloworld(){
return "hello world";
}
void WaterSensor::set(int pin, bool muxBool,int muxPin){
_muxBool= muxBool;
_muxPin=muxPin;
pinMode(pin, INPUT_PULLUP);
int _pin=pin;
if (_muxBool==true && muxPin<5) {
systemNumber=2;
}
else if (_muxBool==true && muxPin>=5&&muxPin<10){
systemNumber=3;
}
else if (_muxBool==true&&muxPin>=10&&muxPin<15){
systemNumber=4;
}
else {
systemNumber=1;
}
}
void WaterSensor::readMux(){
int controlPin[] = {S0, S1, S2, S3};
int muxChannel[16][4]={
{0,0,0,0}, //channel 0
{1,0,0,0}, //channel 1
{0,1,0,0}, //channel 2
{1,1,0,0}, //channel 3
{0,0,1,0}, //channel 4
{1,0,1,0}, //channel 5
{0,1,1,0}, //channel 6
{1,1,1,0}, //channel 7
{0,0,0,1}, //channel 8
{1,0,0,1}, //channel 9
{0,1,0,1}, //channel 10
{1,1,0,1}, //channel 11
{0,0,1,1}, //channel 12
{1,0,1,1}, //channel 13
{0,1,1,1}, //channel 14
{1,1,1,1} //channel 15
};
//loop through the 4 sig
for(int i = 0; i < 4; i ++){
digitalWrite(controlPin[i], muxChannel[_muxPin][i]);
}
//read the value at the SIG pin
double _sensorReading = analogRead(muxArduinoPin);
}
void WaterSensor::changeMuxSelectorPins(int select0, int select1, int select2, int select3){
S0=select0;
S1=select1;
S2=select2;
S3=select3;
}
void WaterSensor::setSystemNumber(int x){
systemNumber=x;
}
int WaterSensor::getSystemNumber(){
return systemNumber;
}
void WaterSensor::changeMuxArduinoPin(int pin){
muxArduinoPin=pin;
}
double WaterSensor::sensorReadingFunction(){
if (_muxBool==true){
readMux();
}
else{
_sensorReading= analogRead(_pin);
}
return _sensorReading;
}
double WaterSensor::populateMilliVolts(){
double _milliVolts=_sensorReading * 1000.0 * (1.65 / 4095.0);
return _milliVolts;
}
double WaterSensor::populateVWC(){
double vwc=0.000992 * _milliVolts - 0.45;
return vwc;
}
double WaterSensor::getVWC(){
sensorReadingFunction();
populateMilliVolts();
populateVWC();
return vwc;
}
double WaterSensor::getMilliVolts(){
return _milliVolts;
}
void WaterSensor::changeMilliVoltDenominator(double arg){
milliVoltDenominator=arg;
}
void WaterSensor::changeReferenceMilliVolt(double arg){
referenceMilliVolt=arg;
}
Here is my test arduino sketch
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <WaterSensor.h>
WaterSensor sensor1;
WaterSensor sensor2;
// outputs
//multiplexer pins
const int S0=1;
const int S1=2;
const int S2=3;
const int S3=4;
void setup() {
// put your setup code here, to run once:
sensor1.set(A0, false, 0);
sensor2.set(A1, false, 0);
Wire.begin();
Serial.begin(9600);
delay(3000);
analogReadResolution(12);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
analogReference(AR_INTERNAL1V65);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println((double) sensor2.populateMilliVolts());
delay(10);
Serial.println((double)sensor2.getMilliVolts());
Serial.println((double)sensor2.sensorReadingFunction());
Serial.println((String)sensor2.helloworld());
Serial.println((double)sensor2.populateVWC());
Serial.println((double)sensor2.getVWC());
delay(1000);
}
and here is a little snippet of my serial monitor
hello world
-0.45
0.00
1650.00
0.00
3303.00
hello world
-0.45
0.00
1399.38
0.00
3631.00
hello world
-0.45
0.00
1190.66
Basically it seems like my milliVolts variable and my vwc variable are not storing any values. And I realize there are a few weird things in the class as I’ve been trying to figure it out and have messed with a few things…
Thanks for any help