I am having a rare but troublesome problem with my Arduino mega. I don't know why it happens, perhaps you can help.
Scenario: The mega is controlling a 90v DC motor. The speed is controlled by sending a PWM signal filtered into a variable DC voltage to the speed control. Everything works great on the bench and for weeks in the field. BUT! every once in awhile, the mega will come up in some strange lockup where it is sending a speed signal to the drive and my robot drives it's self off the end of the track, past the limit switch (takes it about five minutes). And mind you, I have plenty of redundant code to prevent just that from happening. There is no log of the failure so I am guessing the mega is not executing my code. The mega has a nice little power-on reset resister/capacitor combo on it. Maybe I need to increase the size of the capacitor to slow the start and wait for more stable power. Hell, I don't know! Power is 9vdc "wall transformer" via A/C line.
Here is my "startup" code for the PWM:
void setup() { pinMode (Enc_A, INPUT); pinMode (Enc_B, INPUT); pinMode (Enc_A_N, INPUT); pinMode (Enc_B_N, INPUT); pinMode (Lim_pin, INPUT); // limit switch pinMode (PWM_pin, OUTPUT); // output for motor PWM pinMode (Dir_pin, OUTPUT); // output direction relay for motor PWM pinMode (WLvalve_pin, OUTPUT); // left valve relay pinMode (WRvalve_pin, OUTPUT); // right valve relay pinMode (MLvalve_pin, OUTPUT); // left aux valve relay pinMode (MRvalve_pin, OUTPUT); // right aux valve relay digitalWrite (Lim_pin, HIGH); // turn on pullup resistors digitalWrite (Enc_A, HIGH); digitalWrite (Enc_B, HIGH); digitalWrite (Enc_A_N, HIGH); digitalWrite (Enc_B_N, HIGH); digitalWrite (WLvalve_pin, LOW); // left valve relay digitalWrite (WRvalve_pin, LOW); // right valve relay digitalWrite (MLvalve_pin, LOW); // left aux valve relay digitalWrite (MRvalve_pin, LOW); // right aux valve relay
TCCR2A = B10000001; // set Atmega PWM params TCCR2B = B00000001; // set Atmega PWM - bout 31250 hz OCR2A = 0; // init pulse width to min (max = 255) 0 = off Timer3.initialize(500000); // initialize timer3, and set a 1/2 second period Timer3.attachInterrupt(Move_Check); // attaches Move_Check() as a stall warning attachInterrupt(0, doEncoder, CHANGE); Wire.begin(); // for the real time clock Serial1.begin(57600); // used to communicate with the touch screen Serial.begin(57600); // for debug GetDate(); Cumulative_minutes = (Week_day * 1440) + (hour * 60) + minute; // set our Cumulative since Sunday midnight delay(1000); // one second startup delay - wait for screen Serial1.print("H"); // Send reset Serial.print("Reset\n"); // let log know we reset
}