I am having issues while running my program. My Arduino Mega 2560 resets randomly. At times, it will reset in the setup portion of the code, at other times it will reset during the loop function. It happens randomly, not at consistent parts of my code. Also, it seems as though if I power down the device and leave it off for a week or so, the issue happens more frequently than if it has been running for a few minutes/hours.
The machine I am working on has 8 12v solenoids controlled in pairs with 4 mechanical relays. There are also 3 12v lights on a light stack controlled individually by the same type of relays. Each of the 7 relays are powered from an external power supply (5v 60a + 12v 30a) with their signal and ground going to the Arduino. There are also 4 servo motors which are again powered directly from that same 5v power supply with their common ground and signal going to the Arduino. Additionally, there are 2 inductive sensors implemented in the same way being powered separately from the Arduino. I am no expert at any of this by any means but I have spent quite a bit of time trying to figure this issue out on my own before posting about it here and have learned some things along the way to help improve my code and setup. I will paste my code below and answer any follow up questions necessary. Could someone please help me figure out what might be causing this random reset? Is it my code? Is there something I can do with my Arduino to prevent this?
#include <Servo.h>
Servo left;
Servo right;
Servo shoot;
Servo chop;
int RetractRLY = 3; //Relay3
int PushRLY = 4; //Relay4
int LiftRLY = 5; //Relay1
int DropRLY = 6; //Relay2
int REDRLY = 8;
int YLWRLY = 9;
int GRNRLY = 10;
int pauseButn = 11;
int startButn = 12;
int PushInitPosSens = 55; // PushInitPosSens
int MidPosSens = 56;//Sens3
int ServoPower = 60;
bool ServoPowerOn = 0;
void setup() {
shoot.attach(54);
chop.attach(57);
right.attach(58);
left.attach(59);
int StuckTime;
bool PusherINITpos = digitalRead(PushInitPosSens);
Serial.begin(115200);
pinMode(LiftRLY, OUTPUT);
pinMode(DropRLY, OUTPUT);
pinMode(RetractRLY, OUTPUT);
pinMode(PushRLY, OUTPUT);
pinMode(PushInitPosSens, INPUT_PULLUP);
pinMode(ServoPower, OUTPUT);
pinMode(MidPosSens, INPUT_PULLUP);
pinMode(REDRLY, OUTPUT);
pinMode(YLWRLY, OUTPUT);
pinMode(GRNRLY, OUTPUT);
pinMode(pauseButn, INPUT_PULLUP);
pinMode(startButn, INPUT_PULLUP);
chop.write(57);
Wait(150);
shoot.write(120);
Wait(150);
Grab();
Wait(150);
digitalWrite(ServoPower, HIGH);
Wait(150);
Lift();
Wait(150);
UNGrab();
Wait(200);
while (PusherINITpos == 1) {
digitalWrite(RetractRLY, HIGH);
PusherINITpos = digitalRead(PushInitPosSens);
StuckTime = millis();
if (StuckTime > 6000) {
goto MoveOn;
}
}
MoveOn:
digitalWrite(RetractRLY, LOW);
Push1();
Wait(300);
Push2();
Wait(300);
MidPos();
Wait(300);
}
void loop() {
bool StartState = digitalRead(startButn);
bool StopState = digitalRead(pauseButn);
unsigned long startTime;
unsigned long endTime;
float DeltaTime;
float CycleTime;
while (StartState == 1) {
startTime = millis();
//Serial.println(startTime);
GREEN();
StartPos();
Wait(100);
Chop();
Wait(400);
Push1();
Wait(100);
MidPos();
Wait(150);
Grab();
Wait(300);
Drop();
Wait(200);
SHOOT();
Wait(250);
Lift();
Wait(75);
UNGrab();
Wait(150);
Push2();
Wait(100);
endTime = millis();
//Serial.println(endTime);
DeltaTime = (endTime - startTime);
//Serial.println(DeltaTime);
CycleTime = DeltaTime / 1000;
Serial.print("Cycle time: ");
Serial.print(CycleTime);
Serial.print(" Sec");
Serial.println("");
StopState = digitalRead(pauseButn);
if (StopState == 1) {
StartState = 0;
MidPos();
LightsOut();
}
else {
StartState = 1;
}
}
}
void Drop() {
(digitalWrite(DropRLY, HIGH));
Wait(400);
(digitalWrite(DropRLY, LOW));
Wait(150);
}
void Push2() {
digitalWrite(RetractRLY, LOW);
digitalWrite(PushRLY, HIGH);
Wait(1000);
digitalWrite(PushRLY, LOW);
Wait(30);
}
void Push1() {
bool PusherMIDpos = digitalRead(MidPosSens);
while (PusherMIDpos == 1) {
digitalWrite(PushRLY, HIGH);
PusherMIDpos = digitalRead(MidPosSens);
}
digitalWrite(PushRLY, LOW);
}
void Chop() {
chop.write(57);
Wait(50);
chop.write(130);
Wait(700);
chop.write(57);
}
void Grab() {
const int leftgrab = 110.5;//amount of grab for left finger
const int rightgrab = 69;//amount of grab for right finger
left.write(leftgrab);
Wait(150);
right.write(rightgrab);
}
void UNGrab() {
left.write(130);
right.write(50);
}
void Lift() {
digitalWrite(LiftRLY, HIGH);
Wait(500);
digitalWrite(LiftRLY, LOW);
}
void YELLOW() {
digitalWrite(REDRLY, LOW);
digitalWrite(YLWRLY, HIGH);
digitalWrite(GRNRLY, LOW);
}
void RED() {
digitalWrite(REDRLY, HIGH);
digitalWrite(YLWRLY, LOW);
digitalWrite(GRNRLY, LOW);
}
void GREEN() {
digitalWrite(REDRLY, LOW);
digitalWrite(YLWRLY, LOW);
digitalWrite(GRNRLY, HIGH);
}
void LightsOut() {
digitalWrite(REDRLY, LOW);
digitalWrite(YLWRLY, LOW);
digitalWrite(GRNRLY, LOW);
}
void SHOOT() {
shoot.write(91);
Wait(300);
shoot.write(120);
}
void MidPos() {
bool PusherINITpos = digitalRead(PushInitPosSens);
while (PusherINITpos == 1) {
digitalWrite(RetractRLY, HIGH);
PusherINITpos = digitalRead(PushInitPosSens);
}
digitalWrite(RetractRLY, LOW);
}
void StartPos() {
digitalWrite(RetractRLY, HIGH);
Lift();
Wait(100);
digitalWrite(RetractRLY, LOW);
}
void Wait(int duration)
{
unsigned long CurrentTime;
unsigned long PastTime;
CurrentTime = millis();
PastTime = millis();
while (CurrentTime - PastTime <= duration) {
CurrentTime = millis();
//Serial.println(CurrentTime);
}
PastTime = CurrentTime;
}