[SOLVED]Simple Syntax Questions

Hey Everyone,

I am working on a some hardware that keeps track of moisture levels in 4 different plants.

I spent the last two days learning about c++, and this is my first attempt at writing anything. I am running into a small problem. I know the problem I am having is a name scape issue, but I am not sure how to correctly reference the proper name scape.

class moistureSensor{
  public:             // Access specifier
          String input;
    const int airValue            = 620;
    const int waterValue          = 310;
          int moistureValue       = 0;
          double moisturePercent  = 0;

    double getSoilMoisture(){
      moistureValue = analogRead(input);
      moisturePercent = map(moistureValue, airValue, waterValue, 0, 100);
      if(moisturePercent >= 100){
        moisturePercent = 100;
      }
      if(moisturePercent <=0){
        moisturePercent = 0;
      }
      return moisturePercent;
    };
};

void setup() {
  Serial.begin(9600);
  moistureSensor ms1;
    ms1.input = "A0";
  moistureSensor ms2;
    ms2.input = "A1";
  moistureSensor ms3;
    ms3.input = "A2";
  moistureSensor ms4;
    ms4.input = "A3";
}

void loop() {
  ms1.getSoilMoisture();
  Serial.println(ms1.input);
  Serial.println(ms1.getSoilMoisturePercent());

  ms2.getSoilMoisture();
  Serial.println(ms2.input);
  Serial.println(ms2.getSoilMoisturePercent());

  ms3.getSoilMoisture();
  Serial.println(ms3.input);
  Serial.println(ms3.getSoilMoisturePercent());

  ms4.getSoilMoisture();
  Serial.println(ms4.input);
  Serial.println(ms4.getSoilMoisturePercent());

  delay(250);
}

I am getting an error because I am trying to reference ms1, ms2, ms3, ms4 in loop(), while they were referenced in setup()

Also, I suspect calling

analogRead(input);

might give me problems, because I don't know if "string" in the proper variable type.

Can anyone help?

Thanks

analogRead returns an int not a String.

Your declarations of ms1, ms2, ms3, ms4 should be global, i.e. at the top of the program.

Steve

It's "scope" not "scape". Just in case you have to Google something...

Create the instances of moistureSensor outside of any function to give them global scope

The pin numbers for the input values should integers not Strings. Assign the pin numbers (A0, A1, A2, A3) to the previously created objects in setup(). The A* pin numbers are #defined by the Arduino environment and are automatically given integer pin number values that match the board being used

got it working! thanks guys!

I should mention, I thought it was a Name Space issue, (which I believe is stuff like std::) but I now know it was a plain old scope issue!

and now that I set the input variable to int opposed to string, it works perfect.

class moistureSensor{
  public:             // Access specifier
          int input;
    const int airValue            = 0;
    const int waterValue          = 100;
          int moistureValue       = 0;
          double moisturePercent  = 0;

    double getSoilMoisture(){
      moistureValue = analogRead(input);
      moisturePercent = map(moistureValue, airValue, waterValue, 0, 100);
      if(moisturePercent >= 100){
        moisturePercent = 100;
      }
      if(moisturePercent <=0){
        moisturePercent = 0;
      }
      return moisturePercent;
    };
};

moistureSensor ms1;
moistureSensor ms2;
moistureSensor ms3;
moistureSensor ms4;

void setup() {
  Serial.begin(9600);
  ms1.input = A0;
  ms2.input = A1;
  ms3.input = A2;
  ms4.input = A3;
}

void loop() {
  ms1.getSoilMoisture();
  Serial.println("1: ");
  Serial.println(ms1.getSoilMoisture());
  Serial.println(ms1.moistureValue);

  ms2.getSoilMoisture();
  Serial.println("2: ");
  Serial.println(ms2.getSoilMoisture());

  ms3.getSoilMoisture();
  Serial.println("3: ");
  Serial.println(ms3.getSoilMoisture());

  ms4.getSoilMoisture();
  Serial.println("4: ");
  Serial.println(ms4.getSoilMoisture());

  delay(5000);
}

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