Remote tractor Shutdown Ver 2.0

180000 doesn't fit into an int. You'll need to make source unsigned long. I notice you have sneaked an int source into your version that wasn't there in the snippet I gave you too.

Oops, sorry. I just copied the last line and didn't even notice that. I've been up all night working on this.

I'm sorry, but I have no clue what that means

Replace this

 int source = 0; // variable for length of delay at idle during "shutdown"


unsigned long source = 0; // variable for length of delay at idle during "shutdown"

That did it! It compiles error free now. Thank you so much, wildbill.

Do you, or anyone else, see ANYTHING at all that could be a problem here? I'll start working on a schematic to upload, too. Reason I ask is that the hardware is 1200km away from me so I cannot test it.

Your shutdown function has two bugs in it - actually the same one twice. You loop waiting for the limit switches to change, but you have no code in the loop to read your variables ls1 and ls2.

Since they will never change, the loops are endless. You need a digitalRead in each loop.

ls1=digitalRead(LS1); // Start monitoring Limit Switch 1
ls2=digitalRead(LS2); // Start monitoring Limit Switch 2

Isn't that what those lines do? They read the limit switches, then I tell the motor to go forward, which it does until it hits the limit switch, sending it high. No?

Or do I need to enter the same lines in the shutdown loop as well?


ls1=digitalRead(LS1); Means read the limit switch once and store the value. If you want to know what the switch is doing later, you must read it again.

Hmmmm. That may cause for required changes elsewhere, too. I was under the belief that that section of code would continue to loop, reading the sensors over and over until they changed. So how do I set it up so that RFTrig, MSOil (after the 2 min timeout), and MSTemp are constantly being monitored for a high signal? And after that, once the DC motor has began moving, the limit switches scanned until they read high values?

Can you post the latest version?

Also, I suggest that you breadboard this up using LEDs to represent your motors and push buttons for your limit switches so that you can test it on the bench.

/* Irrigation tractor shutdown system
 *  By: Craig Hatzel
 *  May 20, 2021
 *  The purpose for this code is to shut off a tractor that is being used to run an irrigation system via its PTO drive. At startup the Mega2560 will immediately send ouput pin A14 (Igntn) high - closing the NO 
 *  IGNITION relay, connected in parallel with the ignition switch; send output pin A15 (LEDStat) high for 1 min - activating the "Ready to Start" indicator LED; begin a 120,000 millisecond timer (2 min), at 
 *  the end of which it will initialize the monitoring (digitalread) of input pin A5 (MSOil) - delaying the emergency shutdown use of the oil pressure sensor for until there is actually oil pressure to monitor; 
 *  and it will also send ouput pin A13 high - reversing the linear actuator until the throttle triggers limit switch 1 (LS1, on input pin A1). The tractor then can be started  
 *  When the implement has completed its watering cycling the arm will trigger the pump motor to shut down. This is between 800-900 feet away from the tractor.
 *  at the same time a long range RF transmitter will be triggered, it's receiver mounted at the tractor. The receiver will use this signal to send a high signal to input A0 (RFTrig). That will call the 
 *  "Shutdown" loop to initiate and will include a time variable. If Shutdown is called from either pins A4 or A5, the included time variable will be 0; if it is called from pin A0, it will be 180,000 ms (3 min). 
 *  Shutdown: The output on pin A12 (DCFwd) will go high, activating the DC motor controller forward trigger until a high signal is received from tripping the limit switch LS1 on input pin A1. the program will 
 *  then use the delay command to pause all operations for the amount specified in the attached variable. Dependant upon the source of the trigger, it will either pause for 0 milliseconds, or 180,000 milliseconds
 *  after which time it will once again activate output pin 12 (DCFwd) and hold it high until a high signal is received on input pin A2 (LS2), caused by contacting the limit switch (LS2) located at the throttle's
 *  off position. And finally, the Mega will release the high level that was being held on output pin 14, Igntn, turning off the tractor's ignition.
 *  In case of a malfunction or system lockup, or if the tractor was not started by the time the indicator LED was tuned off, a low level reset can be initiated via pressing the reset button connected to input 
 *  pin A3 (Rst) which will close the loop between GND and RESET on the board until a low level reset sequence is initiated.

#include "millisDelay.h" 
millisDelay ledDelay; // delay for led
millisDelay msoilDelay; // delay for msoil read
 const int RFTrig = 0; // RF input from pump on pin A0 
 const int LS1 = 1; // Limit switch at idle position on pin A1
 const int LS2 = 2; // Limit switch at off position on pin A2
 const int Rst = 3; // Reset the Mega 2560, re-initializing the setup routine
 const int MSTmp = 4; // Murphy switch high temperature on pin A4
 const int MSOil = 5; // Murphy switch oil pressure (low) on pin A5
 const int DCFwd = 12; // Ouput to activate DC motor controller forward on pin A12
 const int DCBwd = 13; // Output to activate DC motor controller reverse on pin A13
 const int Igntn = 14; // Output to activate tractor power kill relay on pin A14
 const int LEDStat = 15; // Output to activate LED (1min) to indicate ignoring Murphy oil (2min) start procedure
 int rftrig = 0; // variable for A0 read value
 int ls1 = 0; // variable for A1 read value
 int ls2 = 0; // variable for A2 read value
 int rst = 0; // variable for A3 read value
 int mstmp = 0; // variable for A4 read value
 int msoil = 0; //variable for A5 read value
 unsigned long source = 0; // variable for length of delay at idle during "shutdown"
void setup() {
  // put your setup code here, to run once:
pinMode(RFTrig, INPUT); // set pin A0 as input
pinMode(LS1, INPUT); //set pin A1 as input
pinMode(LS2, INPUT); // set pin A2 as input
pinMode(Rst, INPUT); // set pin A3 as input
pinMode(MSTmp, INPUT); // set pin A4 as input
pinMode(MSOil, INPUT); // set pin A5 as input
pinMode(DCFwd, OUTPUT); // set pin A12 as output
pinMode(DCBwd, OUTPUT); // set pin A13 as output
pinMode(Igntn, OUTPUT); // set pin A14 as output
pinMode(LEDStat, OUTPUT); // set pin A15 as output
digitalWrite(Igntn, HIGH);  
digitalWrite(LEDStat, HIGH);
ledDelay.start(60000);  // start a 60sec delay for the "ok to start LED"
do {         
  digitalWrite(DCBwd, HIGH); // activate the dc motor in reverse to return idle arm to idle/start position
  } while (ls1 = LOW); // for as long as limit switch 1 is low/off and stop when limit switch 1 is hit
msoilDelay.start(120000); // start a 120sec delay

void loop() {
  // put your main code here, to run repeatedly:
 // check if the LED timer has timed out
  if (ledDelay.justFinished()) {
    digitalWrite(LEDStat, LOW); // turn led off
 // check if MSOil read delay has timed out
  if (msoilDelay.justFinished()) {
    msoil=digitalRead(MSOil); // Start reading data from the oil pressure input
rftrig=digitalRead(RFTrig); // start monitoring the RFTrig input
ls1=digitalRead(LS1);  // Start monitoring Limit Switch 1
ls2=digitalRead(LS2); // Start monitoring Limit Switch 2
rst=digitalRead(Rst); // Start monitoring the reset switch
mstmp=digitalRead(MSTmp); // Start monitoring the tractor operating temperature (input signal is sent on high temp)
if (rftrig = HIGH) // if the source of the "shutdown" call was rftrig,
  source = 180000; // then variable source will be 3 min
  if (mstmp = HIGH) // if the source of the "shutdown" call was mstmp,
  source = 0; // then variable source will be 0

  if (msoil = HIGH) // if the source of the "shutdown" call was msoil,
  source = 0; // then variable source will be 0
if(rftrig == HIGH || mstmp == HIGH || msoil == HIGH)

void shutdown()  {
  // create function "shutdown"
do {
  digitalWrite(DCFwd, HIGH); // activate the DC motor forward (reducing RPM)
  } while (ls1 = LOW); // until limit switch 1 (idle) is triggered
do {
  digitalWrite (DCFwd, HIGH); // activate the motor again, moving from idle to off
  } while (ls2 = LOW); // until limit switch 2 (off) is triggered
digitalWrite(Igntn, LOW); // shuts off the key


Yeah, that is actually a very good idea. I probably should do that. I'll have the relay modules, LM7805 , and Mega 2560, of course. All I should need is a bread board, jumpers, buttons and LEDs, right?

That should do it.

All the checks you're doing in the loop function will be executed repeatedly and are therefore ok I think.

The problem is all of your do while loops. They all test a variable that will never change. add a digitalRead that reads the variable that is tested in the loop as the last thing before the closing brace.

Also, you are confusing = and == Check every if and every while and make sure you're using ==