Digital read pin 50

Hello, I am trying to figure out how to digital read on pin 50 of my mega2560.

I have a digital moisture sensor on pin 50 and I want to read the state high or low and print it to serial.

digitalRead()

Serial.println()

here is what I have but its not working.

int waterSensor1 = 50;



void setup() {
  // put your setup code here, to run once:
    Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  
    digitalRead(waterSensor1);
    Serial.println(waterSensor1);
    delay(10000);
}

everone:     digitalRead(waterSensor1);

This code reads the pin, but then does nothing with the reading.

everone:     Serial.println(waterSensor1);

This code prints the value of the waterSensor1 variable, which you have set to 50 here:

everone: int waterSensor1 = 50;

You don't currently understand how digitalRead works. Please take some time to study the digitalRead() documentation: https://www.arduino.cc/reference/en/language/functions/digital-io/digitalread/ and this tutorial: https://www.arduino.cc/en/Tutorial/DigitalReadSerial

When you read the value with digitalRead, it might be a good idea to store it somewhere so you can use that stored value to output it to the serial. Have a look at variables.

Hello now im having issue with the readings. I only get a 0 reading and am not getting a change.

int sensorValue = digitalRead(50);


void setup() {
  // put your setup code here, to run once:
    
    Serial.begin(9600);

     pinMode(50, INPUT);

  

   
}

void loop() {
  // put your main code here, to run repeatedly:
  
    digitalRead(sensorValue);
    Serial.println("Water sensor");
    Serial.println(sensorValue);
    delay(10000);
}

It's much the same problem as before. This code:

everone:     digitalRead(sensorValue);

Reads the pin, but you don't do anything with the reading so it's just discarded. Please go back to the documentation and tutorial I recommended in my last reply and really take some time to study it this time.

pert: It's much the same problem as before. This code:Reads the pin, but you don't do anything with the reading so it's just discarded. Please go back to the documentation and tutorial I recommended in my last reply and really take some time to study it this time.

I am obviously having difficulty with your suggestion. can you point me in the right direction? I keep looking at the direction you previously pointed me to and don't see where I am going wrong.!

There are thousands of tutorials that will teach you how to use digitalRead(), including several built right into the Arduino IDE under File>Examples, but since you are struggling so mightily, try this:

    int x = digitalRead(50);
    Serial.print("Water sensor: ");
    Serial.println(x);