LED Troubleshoot

Hello, before you say anything, this is not a project PRIMARILY on a LED. I just need project guidance. How do I get the LED on the Arduino Mega 2560 to stop blinking every time I run this program:

#include <DFPlayerMini_Fast.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
DFPlayerMini_Fast myMP3;

int buttonPin = 2; // Input for HC-S501

void setup() {

  Serial.begin(9601);

  mySerial.begin(9600);

  myMP3.volume(15);

  pinMode(buttonPin, INPUT_PULLUP);

}

void loop() {

  if (digitalRead(buttonPin) == LOW) {
    myMP3.play(1);
  }

}

I just don't know how to get the LED to stop blinking whenever I try to run the program. Any suggestions?

Thanks,
UptownKitten

I suspect you mean the LEDs connected to pin 10 and 11. They are hardwired to those pins and you are using the pins for your software serial. You can try to use other pins or use a soldering iron and remove them.

Have a look at the schematic. Which you can download from the following link under Documentation.

You do know that the Mega2560 has 4 hardware serial ports? You don't need software serial here, it just loads the core down and ties up an interrupt.

And there is no 9601 baudrate.

Klaus_K:
I suspect you mean the LEDs connected to pin 10 and 11. They are hardwired to those pins and you are using the pins for your software serial.

The pins 10 and 11 the LEDs are wired to are physical pins 10 and 11 on the ATmega16U2. That's different from the Arduino pins 10 and 11 on the ATmega2560 UptownKitten is using for software serial. There are no LEDs connected to those pins. It would be more correct to say the RX and TX LEDs are connected to Arduino pins 0 and 1 because they blink when there is serial communication via those pins.

UptownKitten:
How do I get the LED on the Arduino Mega 2560 to stop blinking every time I run this program:

Which LED? There are four LEDs on the Mega. They have markings next to each on the silkscreen to identify them: ON, L, TX, RX.

I am talking about the L LED. I don't know how to use the code without using the SoftwareSerial library.

I found the problem, the L LED was blinking because it wasn't a constant variable. But, I still can't figure out why the LED isn't turning on after I put my hand in front of the sensor. Any suggestions?

#include <DFPlayerMini_Fast.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
DFPlayerMini_Fast myMP3;

const byte pirPin = 7; // Input for HC-S501

const byte ledPin = 13;  // LED on Pin 13 of Arduino

const byte delayTime = 20;

void setup() {

  pinMode(ledPin, OUTPUT);

  Serial.begin(9600);

  mySerial.begin(9600);

  myMP3.volume(15);

  pinMode(pirPin, INPUT);

}


void takeAction() {

  byte pirValue = digitalRead(pirPin);

  if (pirValue == HIGH) {
    digitalWrite(ledPin, HIGH);
    myMP3.play(2);
  }
  else {
    digitalWrite(ledPin, LOW);
  }

}

void loop() {

  takeAction();
  delay(delayTime);
  
}

Oh, yeah, and I used this code as well and it worked just fine, but I want the code to be my way. Any suggestions on how to make the code more simple?

#include <DFPlayerMini_Fast.h>
#include "SoftwareSerial.h"


SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFPlayerMini_Fast myMP3;
void printDetail(uint8_t type, int value);

#define SENSORPIN 7
#define PAUSETIME 2000

void setup() {
  mySoftwareSerial.begin(9600);
  Serial.begin(9600);
  pinMode(SENSORPIN, INPUT);

  Serial.println();
  Serial.println(F("Initializing DFPlayer..."));

  //Use softwareSerial to communicate with MP3
  if (!myMP3.begin(mySoftwareSerial)) {
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true);
  }
  Serial.println(F("DFPlayer Mini online."));

  //Set volume value (From 0 to 30)
  myMP3.volume(30);
}

void sensorActivated() {
  int pirSensor = digitalRead(SENSORPIN);
  if(pirSensor == HIGH)
  {
    Serial.println("Sensor Activated");
    Serial.println("DFPlayer Working...");
    myMP3.play(2);
  }
  return;
}

void loop() {
  sensorActivated();
  delay(PAUSETIME);
}

I found the problem, the L LED was blinking because it wasn't a constant variable.

Just a correction, it was more than likely blinking because you did not set the pin to output. If you remove pinMode(ledPin, OUTPUT);, does it blink again?

UptownKitten:
I still can't figure out why the LED isn't turning on after I put my hand in front of the sensor. Any suggestions?

Add Serial.println() calls to strategic points in your code, then run it with Serial Monitor open so you can see what's happening in your program.

Yes, the LED blinks without the pinMode(ledPin, OUTPUT);, but now it is blinking again for some reason. even with the pinMode(ledPin, OUTPUT);. I also placed some Serial.println(""); statements and found out that the code will not even go inside the function or I can't see that it is inside the function. Very strange, this was half way working earlier, but not now. Hmm, maybe I bumped into a wire. I don't know. Updated code:

#include <DFPlayerMini_Fast.h>

DFPlayerMini_Fast myMP3;

const int pirPin = 7; // Input for HC-S501

const int ledPin = 13;  // LED on Pin 13 of Arduino

const byte delayTime = 20;

void setup() {

  pinMode(ledPin, OUTPUT);

  Serial.begin(9600);

  Serial1.begin(9600);

  myMP3.volume(30);

  pinMode(pirPin, INPUT);

}


void takeAction() {

  Serial.println("This is inside a function");

  int pirValue = digitalRead(pirPin);

  if (pirValue == HIGH) {
    digitalWrite(ledPin, HIGH);
    Serial.println("Gotcha!");
    myMP3.play(2);
  }
  else {
    digitalWrite(ledPin, LOW);                                  
  }

}

void loop() {

  takeAction();
  delay(delayTime);

}

UptownKitten:
I am talking about the L LED. I don't know how to use the code without using the SoftwareSerial library.

Arduino Uno has 1 serial port. Arduino Serial is the software object (code+data where the data is the port variables and the code is for all serial) that runs the first port. Serial.begin(), Serial.read(), etc, all refer to that one port. Arduino Mega has 4 serial ports named Serial, Serial1, Serial2, and Serial3. You use Serial1 through Serial3 the same way you use Serial.

Serial.begin( 115200 ); // initialize the 1st port
Serial1.begin( 115200 ); // initialize the 2nd port ~~~ use software serial when your Mega needs 5 serial ports!

......

Serial1.println( F( "On an Uno, you have to use software serial to print this." ));

and BTW, the F( "text" ) is a macro that makes the compiler keep the text in flash instead of copying it to RAM on startup. It stays in flash and prints from flash and doesn't use up precious RAM.

Add a Serial.println() to setup() after Serial.begin(9600) to see if your board is resetting over and over again.