Why my arduino code needs to re-upload or reset the arduino to function the code again

When I successfully uploaded my code, and test the functionality, it perfectly works, but when I tried once again, the code isnt functioning, and it needs to be re-uploaded or restart the arduino to function again, how to fix this?

Please show your sketch.

Of many possibilities, this one in your code, a condition might be set to stop the sketch from leaving the condition.

1 Like

What Code?
What Arduino?
What is connected to the Arduino?
How is the Arduino powered?

1 Like

That sounds like a code bug to me but you can upload your schematic to be sure.

Here is my code

```#include <Stepper.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include "DFRobotDFPlayerMini.h"
#include "VoiceRecognitionV3.h"

SoftwareSerial mySoftwareSerial(8, 9); // RX, TX for DFPlayer
VR myVR(2, 3); // RX, TX for Voice Recognition

uint8_t buf[128]; // Increased buffer size for Voice Recognition
const int stepsPerRevolution = 32;
const int buzzerPin = 11;
const int irSensorPin = A0;
const int servoPin = 12;

Servo servo;
int servoAngle = 0;

Stepper myStepper1(stepsPerRevolution, 4, 6, 5, 7);
DFRobotDFPlayerMini myDFPlayer;

#define NeozepRecord (0)

void setup() {
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  myVR.begin(9600);

  // Initialize DFPlayer Mini
  if (!myDFPlayer.begin(mySoftwareSerial, true, false)) {
    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."));
  myDFPlayer.volume(30); // Set volume

  // Initialize Voice Recognition Module
  Serial.println("Elechouse Voice Recognition V3 Module\\Control LED sample");
  if (myVR.clear() == 0) {
    Serial.println("Recognizer cleared.");
  } else {
    Serial.println("Voice Recognition Module not found.");
    Serial.println("Please check the connection and restart Arduino.");
    while (1);
  }
  if (myVR.load((uint8_t)NeozepRecord) >= 0) {
    Serial.println("NeozepRecord loaded");
  }

  // Setup pins
  pinMode(irSensorPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(servoPin, OUTPUT);
  servo.attach(servoPin);
  myStepper1.setSpeed(900);
  myStepper1.step(0);
}

void loop() {
  int ret = myVR.recognize(buf, 75); // Adjust recognition threshold
  Serial.print("Voice Recognition Result: ");
  Serial.println(ret);

  if (ret > 0) {
    switch (buf[1]) {
      case NeozepRecord:
        // Actions for recognized voice command
        myDFPlayer.play(3); // Play the first MP3 file
        myStepper1.setSpeed(900);
        myStepper1.step(1024); // Rotate Stepper
        delay(500);
        myStepper1.step(-1024); // Rotate back

        int sensorStatus = digitalRead(irSensorPin);
        if (sensorStatus == LOW) {
          digitalWrite(buzzerPin, HIGH);
          delay(1000); // Buzzer sound
          digitalWrite(buzzerPin, LOW);
          servo.write(180); // Rotate Servo
          delay(200);
          servo.write(0); // Rotate back
          delay(100);
          myDFPlayer.play(4); // Play the fourth MP3 file after servo rotation
        }
        break;
    }
  }
}

Im using arduino mega 2560

What port?

Do you have a schematic?

Is this the same project you posted in this long thread:

If so, I have not yet had your reply to my post #16.

The VoiceRecognitionV3.h library install its own SoftwareSerial , so with these lines:

SoftwareSerial mySoftwareSerial(8, 9); // RX, TX for DFPlayer
VR myVR(2, 3); // RX, TX for Voice Recognition

start a two SoftwareSerial instances.
The multiple SoftwareSerial can't work at the same time

How to fix this?

As @Delta_G mentioned above, Mega has a three (or four) hardware Serials. Replace the one of your SoftwareSerial to hardware one.

Open the DFRobotDFPlayerMini library example GetStarted to see how to use a hardware Serial with DFRobot player.

It still need to be re uploaded, how to fix this?

please show your revised code

#include <Stepper.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include "VoiceRecognitionV3.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

VR myVR(2, 3);

SoftwareSerial mySoftwareSerial(4, 5); // RX, TX
// SoftwareSerial mySoftwareSerial(5, 4); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

uint8_t buf[128]; // Increased buffer size

const int stepsPerRevolution = 32;
const int buzzerPin = 7;
const int irSensorPin = A0;
const int servoPin = 6;

Servo servo;
int servoAngle = 0;

Stepper myStepper1(stepsPerRevolution, 8, 10, 9, 11);

#define NeozepRecord (0)

void setup() {
  Serial.begin(9600);
  Serial.println("Elechouse Voice Recognition V3 Module\r\nControl LED sample");

  mySoftwareSerial.begin(9600); // Initialize serial communication with DFPlayer Mini
  Serial.begin(115200); // Initialize serial communication with Arduino IDE

  myDFPlayer.begin(mySoftwareSerial); // Start communication with DFPlayer Mini
  delay(1000); // Wait for DFPlayer Mini to initialize

  myVR.begin(9600);

  if (myVR.clear() == 0) {
    Serial.println("Recognizer cleared.");
  } else {
    Serial.println("Voice Recognition Module not found.");
    Serial.println("Please check the connection and restart Arduino.");
    while (1);
  }

  if (myVR.load((uint8_t)NeozepRecord) >= 0) {
    Serial.println("NeozepRecord loaded");
  }

  pinMode(irSensorPin,INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(servoPin, OUTPUT);

  servo.attach(servoPin);
  myStepper1.setSpeed(900);
  myStepper1.step(0);
}

void loop() {
  int ret = myVR.recognize(buf, 75); // Adjust recognition threshold

  Serial.print("Voice Recognition Result: ");
  Serial.println(ret);

  if (ret > 0) {
    int sensorStatus; // Declare sensorStatus outside the switch statement
    switch (buf[1]) {
      case NeozepRecord:
        // Play the first audio file
        myDFPlayer.play(1);
        delay(1000); // Wait for the first audio to finish playing
        // Rotate Stepper 1 clockwise
        myStepper1.setSpeed(900);
        myStepper1.step(1024);
        delay(500);

        // Rotate the stepper motor back
        myStepper1.setSpeed(900);
        myStepper1.step(-1024);

        // Trigger buzzer using IR sensor
        int sensorStatus = digitalRead(irSensorPin);
        Serial.println("IR Sensor Status: " + String(sensorStatus)); // Debug print

        if (sensorStatus == LOW) {
          digitalWrite(buzzerPin, HIGH);
          delay(1000); // Increase the delay for a longer buzzer sound
          digitalWrite(buzzerPin, LOW);

          // Rotate the servo motor
          Serial.println("Rotating Servo to 90 degrees..."); // Debug print
          servo.write(180); // Rotate to 90 degrees (adjust as needed)
          delay(300);     // Adjust the delay based on your requirements

          Serial.println("Rotating Servo back to 0 degrees..."); // Debug print
          servo.write(0);  // Rotate back to 0 degrees

          // Play the second audio file
        myDFPlayer.play(2);
        delay(3000); // Wait for the second audio to finish playing
        }
        break;
    }
    } else {
    servo.write(0); // Rotate back
  }
}

From
https://docs.arduino.cc/learn/built-in-libraries/software-serial/

Not all pins on the Mega and Mega 2560 boards support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).

You have not used any of the additional hard ware Serial ports as recommended.

@thomor
I don't see any big changes in your code... the two SoftwareSerial are still in its place

#include <Stepper.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include "VoiceRecognitionV3.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

VR myVR(2, 3);

SoftwareSerial mySoftwareSerial(14, 15); // RX, TX
// SoftwareSerial mySoftwareSerial(14, 15); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

uint8_t buf[128]; // Increased buffer size

const int stepsPerRevolution = 32;
const int buzzerPin = 7;
const int irSensorPin = A0;
const int servoPin = 6;

Servo servo;
int servoAngle = 0;

Stepper myStepper1(stepsPerRevolution, 8, 10, 9, 11);

#define NeozepRecord (0)

void setup() {
  Serial.begin(9600);
  Serial.println("Elechouse Voice Recognition V3 Module\r\nControl LED sample");

  mySoftwareSerial.begin(9600); // Initialize serial communication with DFPlayer Mini
  Serial.begin(115200); // Initialize serial communication with Arduino IDE

  myDFPlayer.begin(mySoftwareSerial); // Start communication with DFPlayer Mini
  delay(1000); // Wait for DFPlayer Mini to initialize

  myVR.begin(9600);

  if (myVR.clear() == 0) {
    Serial.println("Recognizer cleared.");
  } else {
    Serial.println("Voice Recognition Module not found.");
    Serial.println("Please check the connection and restart Arduino.");
    while (1);
  }

  if (myVR.load((uint8_t)NeozepRecord) >= 0) {
    Serial.println("NeozepRecord loaded");
  }

  pinMode(irSensorPin,INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(servoPin, OUTPUT);

  servo.attach(servoPin);
  myStepper1.setSpeed(900);
  myStepper1.step(0);
}

void loop() {
  int ret = myVR.recognize(buf, 75); // Adjust recognition threshold

  Serial.print("Voice Recognition Result: ");
  Serial.println(ret);

  if (ret > 0) {
    int sensorStatus; // Declare sensorStatus outside the switch statement
    switch (buf[1]) {
      case NeozepRecord:
        // Play the first audio file
        myDFPlayer.play(1);
        delay(1000); // Wait for the first audio to finish playing
        // Rotate Stepper 1 clockwise
        myStepper1.setSpeed(900);
        myStepper1.step(1024);
        delay(500);

        // Rotate the stepper motor back
        myStepper1.setSpeed(900);
        myStepper1.step(-1024);

        // Trigger buzzer using IR sensor
        int sensorStatus = digitalRead(irSensorPin);
        Serial.println("IR Sensor Status: " + String(sensorStatus)); // Debug print

        if (sensorStatus == LOW) {
          digitalWrite(buzzerPin, HIGH);
          delay(1000); // Increase the delay for a longer buzzer sound
          digitalWrite(buzzerPin, LOW);

          // Rotate the servo motor
          Serial.println("Rotating Servo to 90 degrees..."); // Debug print
          servo.write(180); // Rotate to 90 degrees (adjust as needed)
          delay(300);     // Adjust the delay based on your requirements

          Serial.println("Rotating Servo back to 0 degrees..."); // Debug print
          servo.write(0);  // Rotate back to 0 degrees

          // Play the second audio file
        myDFPlayer.play(2);
        delay(3000); // Wait for the second audio to finish playing
        }
        break;
    }
    } else {
    servo.write(0); // Rotate back
  }
}

Here is my code @b707

Are you trolling us?

2 Likes