Water Sensor and LED

Hello, I am a beginner in C++ and am trying out something, what I want the program to do is light up an LED when it is between a certain water level, but the LED is not lighting up no matter what.

here is the code

int adc_id = 0;
int HistoryValue = 0;
char printBuffer[128];

void setup()
{
  Serial.begin(9600);
pinMode(2, OUTPUT);

}

void loop()
{
    int value = analogRead(adc_id); // get adc value

    if(((HistoryValue>=value) && ((HistoryValue - value) > 10)) || ((HistoryValue<value) && ((value - HistoryValue) > 10)))
    {
      sprintf(printBuffer,"ADC%d level is %d\n",adc_id, value);
      Serial.print(printBuffer);
      HistoryValue = value;
    }
    if (printBuffer <= 200){
      
      digitalWrite(2,HIGH);
      }
      else(digitalWrite(2,LOW));
}

Can you try this first and see what values you are actually getting?

void setup() {

  Serial.begin(9600);
}


void loop() {

   int value;

   value  = analogRead(A0);      // get adc value. (Guessing at pin# here. 0 just seemed wrong.)
   Serial.print(value);
}

-jim lee

   if (printBuffer <= 200){

You are comparing the memory address where printBuffer is stored to 200, which is completely unrelated to the value read from the analog port.

How would I go about attaching/ Linking them?
thank you for the feedback.

Instead of:

 if (printBuffer <= 200){

Did you mean?

 if (value <= 200){

Replace:

    if (printBuffer <= 200){
      
      digitalWrite(2,HIGH);
      }
      else(digitalWrite(2,LOW));

with this:

   if (value <= 200)
   {
       digitalWrite(2,HIGH);
   }
   else
   {
       digitalWrite(2,LOW);
   }

See what happens.

Thank You, it worked and i also see what I did wrong, silly mistake. Thank you very much

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