0

How can I make my SensorValue value update automatically? I am trying to make the SensorValue variable update itself if the condition of SensorValue < Maxdryness or SensorValue >= Maxdryness is false at any time. I want to be able to switch my moisture sensor between dry and wet soil and make it turn my motor on or off as needed automatically.

  if(incomingData.indexOf("Auto on")>=0) // if received command is to turn system automatic
 { 
  auto_flag = 1;
  message1 = "System is now automatic";
  send_message(message1); // Send a sms back to confirm that the motor is turned auto

  if (readSoil() >= Maxdryness)
     {
  Serial.println("Soil is good.\r"); 
  digitalWrite(wetSoil, HIGH);
  digitalWrite (drySoil, LOW);
  digitalWrite(motorPin1,LOW);
  digitalWrite(motorPin2,LOW);
  delay(1000);
    }

  else
   {
  Serial.println("Low Soil Moisture detected.\r");
  digitalWrite(wetSoil, LOW);
  digitalWrite (drySoil, HIGH);
  digitalWrite(motorPin1,HIGH);
  digitalWrite(motorPin2,LOW);
    }
   
  delay(1000);
  Serial.print("Soil Moisture = "); 
  Serial.println(readSoil());
  delay(1000);//take a reading every second 
   
  auto_flag = 0;
 }

My project involves sending SMS to turn the system on or off manually, Motor on, Motor off but I wanted an option to make the system run by itself without having to manually turn the motor on or off myself. Hence Auto on. This auto can be turned off by Auto off. I don't know how to make the system run while on Auto on to turn the motor off by switching my soil moisture probe from the dry soil into the wet soil and vice versa from wet to dry and turning the motor on. If the condition of SensorValue is >= Maxdryness I want it to stop and only turn on when the value is < the Maxdryness.

2
  • 1
    Not sure I understand what you mean but it kinda seem like you want you want the whole code to run in some kind of recursive way? if yes, how are you planning to end it? meaning what condition will end the continuous/auto update you're talking about? if no: you might want to be more precise with your question. Commented Dec 8, 2020 at 23:32
  • Arduino is not C, it's C++ :) Commented Dec 9, 2020 at 0:44

1 Answer 1

3

If SensorValue is a sensor on a single pin, read that pin each loop or periodically!
Documentation for: analog pin and digital pin

It seems more likely from your code that it's an analog pin. It also doesn't appear from the docs (I haven't read them in a while) that you need to declare analog-readable pins in your setup() block (while you do for a digital pin), so it should be as simple to get the result as assigning the return from calling analogRead() to SensorValue

void loop() {
    ...
    SensorValue = analogRead(PIN_NUMBER);
    ...
}

You may want to do some massaging to linearize logarithmic outputs force the value into some range (0-100 rather than 0-1023), etc. but this would depend on unshown code and on the value the sensor pin represents.

If the sensor is something more advanced, such as living on an SPI bus, you'll need to do some extra work, but the setup will be similar. Whomever produced the component should provide some documentation. Arduino also provides an example for some generic Barometric Pressure Sensor if you need another reference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.