Serial Monitor Test Issues

Brand new to the Arduino and coding world. Having an issue with my Mega2560 and trying to do some basics. Code below.

const int DIN_PIN = 7;

void setup(){
    pinMode( DIN_PIN, INPUT );
    Serial.begin(115200);
}

void loop(){
    int value;
    
    value = digitalRead( DIN_PIN );
    Serial.println( value );

    delay (1000);
}

All I want to do is get a simple return on the serial monitor if I have the pin either jumped to 5v or GND. When I upload the code, it only displays the state of the pin when it uploads, so it does not seem to be executing the loop. I get a 1 if my jumper is on pin 7 and 5V, and get a 0 if the jumper is not on the 5V.

Did you mean to call analogRead?

I do not think so. I found this is an example code on a different website. I will link at the end here. I am mostly just trying to figure out serial monitor reads, so that I can troubleshoot a basic button circuit that I could not get to work earlier either.

The Basics of Arduino: Reading Switch States (deviceplus.com)

By "not on the 5V" I assume you mean the jumper is only connected to pin 7. In which case this should help...

https://www.google.com/search?q=floating+pin+site:forum.arduino.cc

And this...

Ok after some more trials with even simpler code, such as just getting the built in LED to blink on and off for one second, which also was not working, I restart my computer. Seems to have entirely fixed all my problems. I can now see the serial monitor when using 9600 baud instead of on 115200, I got the built in LED to blink, and this little test is also working correctly. Thanks for the help, but I should always remember just to restart tech first when it acts up.

I don't think that you mentioned your version of the IDE and your operating system. That might be useful to know for this type of problems.

Glad that you found a solution.

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