Code only works when Serial Monitor opens/reset button is pressed

I'm having an Issue with running a sketch where the code only works properly when the Arduino is plugged into a laptop via USB and has the serial monitor open in the IDE. I'm experiencing this issue on an Arduino Mega 2560 Rev3.

I've research throughout this forum for other users having a similar issue, but haven't found a solution that works for me.

A short description of the use case and when the issue arises:

This system is an automation for a sewing machine. With the press of a button the sewing machine will be activated and a separate air system is activated to blow finished pieces off of the sewing table. A thread detector is used to stop the sewing machine if the thread becomes stationary.

The system it self contains (2) pneumatic solenoids run by relays, two buttons used for stopping and starting the program, and one piezo electric sensor that is used to detect motion.

The system works as intended when the serial monitor is open on the computer, however if I try to run the system off of USB power via the computer or an external power supply, the system automatically stops as if the thread detector reported a value that represents stationary thread.

I've attached my code below.

// Code for Juki 9010 Automatic Rip-Stop Folder
// Written by
// June 2021
// Version 3

//Initializing Pins
//digital pin for control of the compressed air system.
int right_Relay_Pin_AIR = 5;
// digital pin for control on the Juki I/O Start/Stop Pin.
int left_Relay_Pin_JUKI = 6;
// digital pin for the start button.
int start_Button = 8;
// digital pin for the stop button.
int stop_Button = 7;
// analog pin for the thread break detector
int thread_Detector = A2;

//Initializing Variables
int thread_Detector_Base = 0;


void setup(){ 
  //Serial.begin(9600);
  Serial.end();
  // Configures the relay control pins
  pinMode(right_Relay_Pin_AIR,OUTPUT);
  pinMode(left_Relay_Pin_JUKI,OUTPUT);

  // Configures the stop/start buttons.
  pinMode(start_Button,INPUT);
  digitalWrite(start_Button, HIGH);
  pinMode(stop_Button,INPUT);
  digitalWrite(stop_Button,HIGH);

  // Configures the thread sensor pins.
  analogReference(INTERNAL1V1); // References the analog readings to 1.1V
  pinMode(thread_Detector, INPUT);

  // Sets a value for the thread detector when it isn't moving.
  thread_Detector_Base = AverageThreadBreakSensor();
  //Serial.print("Base Value = ");
  //Serial.println(thread_Detector_Base);
}

// Function to average 10 measurements from the thread break sensor.
// Returns the averaged value.
int AverageThreadBreakSensor(){
  int i;
  int aVal = 0;
  for(i = 0; i < 10; i++){
    aVal = aVal + analogRead(thread_Detector);
  }
  aVal = aVal/10;
  return aVal;
  }

void loop(){
  delay(100); 
  if(digitalRead(start_Button) == LOW){ // Start Button Pressed.
    while(digitalRead(stop_Button) == HIGH){ // While Stop Button is not pressed.
      digitalWrite(right_Relay_Pin_AIR,HIGH); // Turns on pneumatics.
      digitalWrite(left_Relay_Pin_JUKI,HIGH); // Starts Juki Sewing.
      delay(300);
      if(AverageThreadBreakSensor() >= (thread_Detector_Base-5)){ // If the current thread break sensor value is within 5 units from the base value then loop breaks.
        //Serial.print("AverageThreadBreakSensor = ");
        //Serial.println(AverageThreadBreakSensor());
        break;
      }
    }
    digitalWrite(right_Relay_Pin_AIR,LOW); // Turns off the pneumatics.
    delay(50);
    digitalWrite(left_Relay_Pin_JUKI,LOW); // Turns off the Juki Sewing.
  }
}

Hi @rwalch
If the arduino is connected to USB and the serial monitor is closed, the system doesn't work?

RV mineirin

@ruilviana
That's correct. The sewing and air functions will turn on, but then will be shut off by the piezoelectric sensor, whether the sensor is detecting motion or not.

Something I forgot to add is that the system works as expected without the sensor and with the serial monitor closed. (i.e. the start button is pressed and the sewing and air functionality are both turned on and stay on until the stop button is pressed.)

Post a wiring diagram and image of your setup, please.

@Idahowalker I've attached a picture of the setup and some drawings of the current layout of my circuit.

When you try to run the project without the USB plugged in what voltage do you get for the Mega supply, as measured on the Mega?

If the wires are connected directly to the Mega does the project work properly?

BTW the way the issue is described it reads as a power issue.

Good looking project.

[quote]If I read your post correctly, it sounds like you are powering the Arduino by applying +5vdc from your custom PS module to the Arduino Vin pin?

If that is the case, you are not meeting specification for external power input reccomendation of 9-12vdc. The only difference between the Arduino external jack's positive terminal and the Vin pin is that there is a series connected diode for reverse voltage protection. Both points feed the input of the on-board +5vdc regulator chip which requires about 7.5v minimum input voltage to meet it's voltage regulation drop out specification.

A better solution could be to add an additional regulator, a 7809, to your external power supply module and feed it's +9vdc output into either the external power jack or the Vin pin on the Arduino.

Lefty
[/quote]
from the link of Vin pin issues

The reason why the project is working with the USB plugged in is its being properly powered by 5V.

I was thinking it was a power issue as well.

So originally I modified a USB cable and powered it via the onboard 5v power supply. From my understanding, powering it through the USB only requires 5v.

But you are correct, while trouble shooting I connected the 5v directly to the Vin pin and jumped the common wire from the Mega to my power supply (instead of using the common on the usb). This was an error on my part.

However, the issue still arises when the Arduino is powered by a laptop. The program only starts to work as expected when I open the serial monitor in the IDE...

Please, post a revised schematic of how you are properly powering the Mega.

Here is an updated schematic of the arduino being powered via modified usb cable connected to the 5v power supply.

I suppose I will try to feed 9-12v into the Vin pin and see if that solves the issue. The dependency on the serial monitor is confusing me.

Have you tried a basic blink the led sketch? Upload the sketch, no serial monitor and see if the led turns off and on. Just a basic thing. We can learn from such a basic thing.

First, doing the blink the led thing, leave all your wires connected to the mega just as you were to run it normal. If that does not work pull the mega out of the system and run blink the led as a stand alone.

Hi @rwalch
Why are you using " Serial.end(); " if you haven't used
" Serial.begin();"?
ref: Serial.end() - Documentação de Referência do Arduino

RV mineirin

@ruilviana Troubleshooting efforts...

I'll remove that portion.

@Idahowalker

Let me try that out, when I get a second.

@Idahowalker @ruilviana

The blink method seems to work just fine with the mega in the system. It worked as expected using both the on board power supply and and the laptop USB connection.

Now add in bits of your code, leaving the wiring in place and leave the blinking led in place as an indicator all is well.

Disconnect the relays outputs so an enable of the relay will not damage something.

Such as comment out all the code in loop() but blink and uncomment 'everything' in setup.

Oh so the mega and the shield are, at this point, not the issue.

Also, when you make code changes, like getting rid of serial.end, post your new updated code.

does that work? If it does add in another function and use serial prints to view results.

With over 300ms of delay your code will be stopped for 3/10th of every second. If it is critical for the unit to stop when the stop button is pressed 1/3 of a second without a response might be life/death? Serious question.

I'll try that code in a few minutes.

The 300ms delay is specifically in the code to allow for the thread to start moving before a "baseline" value is recorded for the motion sensor. It may not be necessary.

Perhaps using millis() might work in this case. By using millis() you could still have a delay but, also, still check on the safety button sate.

Let's say the safety button is done by an interrupt.

In the ISR set a Boolean to true. each time loop runs it can check for the safety button Boolean value sate and respond accordingly.

demonstration of several things at the same time.

Obviously connecting 5 V to "Vin" or the "barrel jack" is a mistake.

In fact, using "Vin" or the "barrel jack" is in general, a mistake.

But correctly powered, by regulated 5 V to the USB jack or the "5V" pin with no PC connected, it is very much not obvious why it malfunctions and clearly some sort of hardware problem so testing code is unlikely to get anywhere. :astonished: