Running sketches on Arduino MEGA

I bought an Arduino MEGA 2560 R3 because I wanted the extra pins. I tried uploading code to the board, but they never run. I have seen discussion on this that some changes must be made and some technical jargon, but no real help on what actually to do to make sketches work on the MEGA board. Anyone have a simple explanation?

Hello
What happens? What did you do?

I simply uploaded a sketch that ran on a UNO board. It uploads to the MEGA, but nothing happens.

Hi @AstroJ. Please post your full sketch.

  1. Auto Format your code by following these instructions:
    • If using the Arduino IDE: select Tools > Auto Format from the menus.
    • If using Arduino Cloud Editor: press Ctrl+B
  2. Click on the window that contains your sketch code.
  3. Press Ctrl+A. This will select all the text.
  4. Press Ctrl+C. This will copy the selected text to the clipboard.
  5. In a forum reply here, click on the reply field.
  6. Click the </> icon on the post composer toolbar. This will add the forum's code block markup (```) to your reply to make sure the error messages are correctly formatted.
    Code block
  7. Press Ctrl+V. This will paste the compilation output into the code block.
  8. Move the cursor outside of the code block markup before you add any additional text to your reply.
  9. Repeat the above process if your sketch has multiple tabs.
  10. Click the Reply button to post the output.

When your code requires a library that's not included with the Arduino IDE please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manager (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.

const int ninePin = 9;    // Red LED to PWM pin 9
const int elevinPin = 11;  // Yellow LED to PWM pin 11
// Cathode pins on both LEDs are connected to GND

void setup()
{
  pinMode(9, OUTPUT); //LED output
  pinMode(11, OUTPUT); //LED output
  // sets up LEDs for digital write LOW (off)
}

void loop()
{

  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 3)

  {
    analogWrite(ninePin, fadeValue);
    analogWrite(elevinPin, (100 - fadeValue));

    delay(15);

  }

  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 3)
  {
    analogWrite(ninePin, fadeValue);
    analogWrite(elevinPin, (100 - fadeValue));

    delay(15);
  }
  {
    digitalWrite(9, LOW); //turns off LED
    digitalWrite(11, LOW); //turns off LED
    delay(50);
  }

}

That was a good explanation. The sketch still did not run. Do I need to use different pins than D9 and D11?

OK. It works. Stupid me. One wiring mistake. Thank you! Also, I did not know about Auto Format. Appreciate the help!

You're welcome. I'm glad to hear it's working now. Enjoy!
Per