Mega 2560 Beginner - HELP!

Hello! Looking for help with retirement project using a Mega 2560. I am have strange input results with pins 22 through 53. I can get pins 37 through 53 to work with a pinMode of "Input_Pullup" and sinking the input to ground. However, I am not getting the same results with 22 through 36?? I tried to figure this input issue out by testing each input with this simple code with doesn't work either?? I am not getting any return in the serial monitor - I checked baud and comm ports...?
Thanks, Rick

//My Code to test each input

int In=38;
void setup() {
pinMode(In, INPUT_PULLUP);
}
void loop() {
if (In==HIGH) {Serial.print("H");}
if (In==LOW) {Serial.print("L");}
delay(2000);
}

In seems to be your input pin with the number 38.

If you just print LOW or HIGH, it will show 0 or 1. They never match 38.

You have forgotten to do a digitalRead() to read the pin; next you can compare the results of that read with LOW or HIGH.

You're spending all your time in

which means the Arduino is basically frozen all the time. You also don't have a line such as

int reading = digitalRead(In);

which would change your code from

to

if (reading==HIGH) {Serial.print("H");}
else if (reading==LOW) {Serial.print("L");}

If this is a simple pushbutton, it reads HIGH by default because of

so you might only print on the state of the button being pressed. What it's doing by default is usually less interesting so you might try only testing for the pressed condition

if (reading==LOW) {
Serial.println("L"); // changed to Serial.println to print neatly on new rows
}

then you don't need delay at all.

Oh yeah, you also forgot to use Serial.
Serial.begin(115200); // my default baud rate

If you just connect a switch between a port pin and 5V or Gnd without using pinMode(pin,INPUT_PULLUP), then digitalRead(pin) will return either HIGH or LOW randomly, depending on noise on the pin.
If you use INPUT_PULLUP and connect a switch between the pin and Gnd then all the pins should work. digitalRead(pin) will return HIGH when the switch is open, and LOW when the switch is closed.
If you follow the above suggestion, do you still get unexpected results from some pins? If so, please post most recent code, and serial monitor output, both in code tabs. Please don't edit posts to correct a mistake someone else commented on after you posted. It makes nonsense of some of the comments when someone comes in and tries to follow the thread from post #1.

Hello Friends, thank you kindly for your feedback! I did simplify the test code to its simplest terms to test the pins and still was unable to get any return in the serial monitor until I defined the baud rate as suggested by Hollowed31(thank you) to Serial.begin(115200);. I changed it back to 9600 and the code seems to be responding as expected. Not sure why is need this, I thought it defaulted to 9600?? I ask for patience with my forum etiquette as I am learning....:slight_smile:

void setup() {
Serial.begin(9600);
pinMode(22, INPUT_PULLUP);
}

void loop() {
if (digitalRead(22)==LOW) {
Serial.println("Input_OK");
}
}
1 Like

No Problem.

I'd like to add that a great next step would be to look at and understand the IDE example sketch File > Examples > 02. Digital > StateChangeDetection. I'd recommend modifying the example sketch; however, to eliminate the need for an external pulldown resistor and simply use the internal pullups as we just discussed.

Not sure if it was suggested to change to 115200 and next back to 9600. @hallowed31 only gave an example.

The serial monitor in the IDE defaults to 9600 till you change it. 9600 is just an antique baud rate, 115200 is just about 10x faster and a more modern standard. There are reason to choose 9600 (or even slower) but for an Arduino connected to a PC next to it via USB that is not the case.

Serial.begin() doesn't just set the baud rate. It also initialises other aspects of the Serial support. Without Serial.begin() your Serial.print/println won't work.