My Program Goes Back to Setup Again and Again

Hi,

I am a newbie to Arduino programming. I am using Arduino Mega 2560.

I have connected a fingerprint sensor (pins 10 and 11) and wave shield as described in https://paulhartigan.net/2012/01/01/wave-shield-and-arduino-mega-2650/ (I have changed my configuration as needed for 2560).

After completing the wiring, program for finger print sensor runs as expected. Similarly, program for wave shield to play a WAV file also runs as expected.

However, when I combine both my programs, my program goes back to setup again and again.

Any reason, this might be happening? Is my Mega board just rebooting each time for some reason? If so, what can I do to prevent that.

Attached is my code. If I uncomment code, the program goes back to setup again and again.

  • if (fpReturnValue == 104) {*
  • playByName(“4.WAV”, 2000);*
  • }*

If I keep this code commented, program works fine. However, I need to play different files based on return value of the function getFingerprintID.

Any pointers?

Thanks.

doorbell5.ino (5.2 KB)

You use pin 10 and 11 for SoftwareSerial, but that is often used for a chip-select. I’m not sure that the shield does no longer use it. The Mega board has enough pins, can you use other pins ?
See the reference which pins can be used : Arduino - SoftwareSerial
The pins A9 to A15 seem safe to use.

Resetting can occur with low power. Try a better USB cable. Do you power it with USB only from a laptop ? Do you have a 7.5V or 9V power supply to power it via the power barrel jack ?

Resetting might happen with ram overflow. But I don’t see large buffers.

An Arduino could reset with a bad ground or a missing ground wire. If for example the Arduino is connected to the ground of the computer and something else is connected with a different ground, that can reset it.

Perhaps the Mega board is a very cheap clone and resets for no reason.

   while (!Serial);  // For Yun/Leo/Micro/Zero/...

But you don't have one of those boards, so why the f**k is this here?

Why are you leaving 3 hardware serial ports unused while using SoftwareSerial? Makes NO sense to me, so I will not be reading any more of the code.

PaulS:

   while (!Serial);  // For Yun/Leo/Micro/Zero/...

But you don't have one of those boards, so why the f**k is this here?

Why are you leaving 3 hardware serial ports unused while using SoftwareSerial? Makes NO sense to me, so I will not be reading any more of the code.

It is safe to leave in as the other boards have a Serial object that is non-null.

Thanks all for reply. I am a middle school student and this is my first project on Arduino, so very much using the code that i get from examples on internet. I will try with pins A9 to A15 instead of 2 and 3. I also do not know difference between hardware serial and software serial. I will try to read more from internet. I would appreciate any pointers.

In the mean time, I have a question related to power, because I agree from comments above that Arduino may not be getting enough power from serial port alone. Can I power my Arduino Mega using external power supply and also connect a serial port? If that is the case, I will use both to ensure my Arduino Mega has enough power to it.

Thanks.

Inside the microcontroller are timers, a I2C interface, UARTs (serial interface) and a lot more.
The Arduino Mega has 4 serial ports, but one of them is used to upload a sketch and for the serial monitor.
That means there are still 3 that you can use.

When a microcontroller has not enough serial ports, it is possible to create a serial port on two pins in software. Often a timer is used. It has large consequences for the rest of the code, because it is not easy to create a serial port in software. Therefor you should avoid SoftwareSerial. Stay away from it, if possible. Since you have a Mega board, you don't need the SoftwareSerial.

Everything is online.
The Serial ports (for the Mega) : Serial - Arduino Reference

MarkT:
It is safe to leave in as the other boards have a Serial object that is non-null.

Yes, but leaving it in a program WITH a comment that explicitly says it isn't needed shows that no matter the experience level the programmer isn't really trying very hard. Anyone can be a script-kiddie. Writing a program requires at little effort. At least enough effort to read the thing you've written.

BlueBear365:
I also do not know difference between hardware serial and software serial.

Uno has only 1 on-chip serial port and needs to drive 2 pins to simulate another serial line using software.
SoftwareSerial requires adding a library to the sketch, it takes room and robs CPU cycles.

Mega2560 has 4 serial ports:

Serial
Serial1
Serial2
Serial3

Need more? Use SoftwareSerial. Need 2? Use Serial and Serial1.