Remote Engine Control for Dragline Pump

HI there, I’m new to the forum and totally thrilled with what everyone here is doing. I am currently waiting on my PCBv2 from china and a few other parts for putting together my first autosteer board. In the mean time I have been playing with the Arduino’s that I purchased. I just can’t stop coming up with ideas for their use. I have been doing a lot of learning as of late on how to program the Arduino, watching video after video.

What I want to do is have 4 sensors at the motor (completely mechanical engine with no electronics)
Engine rpm (not sure yet, proximity sensor???)
Engine oil pressure (5 vdc pressure sensor)
Engine temp (still looking for a 5vdc temp sensor)
Pump Pressure (5 vdc pressure sensor)
And 3 relays
Linear actuator in
Linear actuator out
Engine stop

The plan currently is to have an XBEE radio and relays at the Motor and XBEE radio connected to an Arduino with OLED display and buttons for the relays in the dragline tractor.

So far I have been playing with some sensors and a small motor connected via 2 relays for forward and reverse. The sensors work and display on the OLED display and the relays work for controlling the small motor.

My problem is when I just had the relays in the sketch the relays were instant when the buttons were pressed. Now that I have added the sensors to the sketch there is a tremendous lag between button press and contactor closing.

I thought that someone here might have some insight into what I am doing wrong.

Also if anyone has thoughts on a better way to accomplish my goals please share, I really have know I idea of what I am doing. Just reading and watching a lot and trying to string ideas together.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_WIDTH 128
#define OLED_HEIGHT 64

#define trigPin 9
#define echoPin 10

#define trigPin2 6
#define echoPin2 5

#define buttonApin 7
#define buttonBpin 8
#define relay1pin 3
#define relay2pin 4

#define OLED_RESET A4
Adafruit_SSD1306 display(OLED_RESET);

void setup()
{ Serial.begin (38400);
pinMode(relay1pin, OUTPUT);
digitalWrite(relay1pin, LOW);
pinMode(relay2pin, OUTPUT);
digitalWrite(relay2pin,LOW);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
display.clearDisplay();
}

void loop()
{
if (digitalRead(buttonApin) == LOW)
digitalWrite(relay1pin, HIGH);

else if (digitalRead(buttonApin) == HIGH)
digitalWrite(relay1pin, LOW);

if (digitalRead(buttonBpin) == LOW)
digitalWrite(relay2pin, HIGH);

else if (digitalRead(buttonBpin) == HIGH)
digitalWrite(relay2pin, LOW);
long duration,duration2, distance, distance2;

digitalWrite(trigPin, LOW); //PULSE |—|
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

digitalWrite(trigPin2, LOW); //PULSE |—|
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);

duration2 = pulseIn(echoPin2, HIGH);

distance = (duration/2) / 29.1;

display.setCursor(20,0); //oled display
display.setTextSize(1.7);
display.setTextColor(WHITE);
display.println(distance);
display.setCursor(45,0);
display.setTextSize(1.7);

display.println(“Centimetres”);

distance2 = (duration2/2) / 73.914;

display.setCursor(20,10); //oled display
display.setTextSize(1.7);
display.setTextColor(WHITE);
display.println(distance2);
display.setCursor(45,10);
display.setTextSize(1.7);

display.println(“inches”);

display.display();

delay(500);
display.clearDisplay();

Serial.println(distance);//debug

Serial.println(distance2);//debug
}

1 Like

Use interrupts (ISR) to capture your button presses, keep code within interupt very short.
Delay is a bad command, only use when debugging.

Thanks Apm,

Googled interrupts and below is what I have came up with, however now both buttons control both relays at the same time. The buttons are much faster now though.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_WIDTH 128
#define OLED_HEIGHT 64

#define trigPin 9
#define echoPin 10

#define trigPin2 6
#define echoPin2 7


#define relay1pin  5
#define relay2pin  4
#define OLED_RESET A4
Adafruit_SSD1306 display(OLED_RESET);
 
const byte interruptPin = 2;
const byte interrupt2Pin = 3;
volatile byte state = LOW;
volatile byte state2 = LOW;


void setup() 
{  pinMode(interruptPin, INPUT_PULLUP);  
  pinMode(interrupt2Pin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
  attachInterrupt(digitalPinToInterrupt(interrupt2Pin), blink, CHANGE);
 
  pinMode(relay1pin, OUTPUT);
  
  pinMode(relay2pin, OUTPUT);
  
 
   Serial.begin (9600);
 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
   display.clearDisplay();
}

void loop() 
{{
   digitalWrite(relay1pin, state);
    digitalWrite(relay2pin, state2);
}{  
  long duration,duration2, distance, distance2;
  
  digitalWrite(trigPin, LOW);  //PULSE ___|---|___
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

   digitalWrite(trigPin2, LOW);  //PULSE ___|---|___
  delayMicroseconds(2); 
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin2, LOW);
  
  duration2 = pulseIn(echoPin2, HIGH);
  
 
  distance = (duration/2) / 29.1;
  
  

  display.setCursor(20,0);  //oled display
  display.setTextSize(1.7);
  display.setTextColor(WHITE);
  display.println(distance);
  display.setCursor(45,0);
  display.setTextSize(1.7);
  
  
 
  display.println("Centimetres");


  distance2 = (duration2/2) / 73.914;


  display.setCursor(20,10);  //oled display
  display.setTextSize(1.7);
  display.setTextColor(WHITE);
  display.println(distance2);
  display.setCursor(45,10);
  display.setTextSize(1.7);
  

  display.println("inches");

  
  display.display();
  display.clearDisplay();
 
  Serial.println(distance);//debug
  
 Serial.println(distance2);//debug 
}}
void blink() {
   state = !state;
    state2 = !state2;
}
  1. use separate isr for each button, or some “if” statements linking each button to the relevant commands. Put a bit of debounce code in the isr, this way the state=!state command is only run once. otherwise you may get results that will seem a bit confusing as the button state changes multiple times per second.
  2. Try to write your own code rather than using copy/paste, really is best way to learn what your doing. use meaningful names.
  3. put comments next to your code; // this really will help you and others understand what you want each command to do.
  4. when posting code, use 3 backticks ``` on their own line above and below your code. Be sure there are no spaces on the lines with the backticks, or this will not work. You should be able to go back and edit the code in your posts above to try.