That is very true Robin,
I didn't think of that. I'll add that to the list of modifications I need to make to my code along with hardware serial. The code below is what I'm now using. I've replaced the rev pot with a SPDT rocker. So revs are either at idle or flat out.
This code was taken from the link below and modified to suit (Thanks Robin2 - this was put together by you. Your hard work is much appreciated).
http://forum.arduino.cc/index.php?topic=288234.0
Controller - Transmit
Few points to mention:
I found I couldn't transmit the full analogue 1023 bits (4 digit value) using this code below, so thats why I've mapped the input from 0 - 998. This works, so I wasn't to fussed about resolving it.
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7);
SoftwareSerial mySerial(10, 11); //RX, TX
const int an2 = A14;
const int an3 = A15;
const int dRhigh = 36;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(dRhigh, INPUT_PULLUP);
}
void loop() {
int in2(analogRead(an2));
int intTL = map(in2, 0, 1023, 0, 998);
int in3(analogRead(an3));
int intTR = map(in3, 0, 1023, 0, 998);
int Rhigh(digitalRead(dRhigh));
mySerial.print("<");
mySerial.print(intTL);
mySerial.print(", ");
mySerial.print(intTR);
mySerial.print(", ");
mySerial.print(Rhigh);
mySerial.println(">");
delay(25);
}
Machine - Receive
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //RX, TX
const byte buffSize = 50;
char inputBuffer[buffSize];
const char startMarker = '<';
const char endMarker = '>';
byte bytesRecvd = 0;
boolean readInProgress = false;
boolean newDataFromPC = false;
char messageFromPC[buffSize] = {0};
int intlt = 0;
int intrt = 0;
int Rhigh = 1;
int LTF = 0;
int LTR = 0;
int RTF = 0;
int RTR = 0;
int LTCali = 0;
int RTCali = 0;
unsigned long curMillis;
unsigned long prevReplyToPCmillis = 0;
unsigned long replyToPCinterval = 1000;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
analogWrite(5, 0);
analogWrite(4, 0);
analogWrite(3, 0);
analogWrite(2, 0);
pinMode(14, OUTPUT);
digitalWrite(14, LOW);
}
void loop() {
curMillis = millis();
// Recieve data from controller
recData();
// Output to machine
output();
// Serial output for debugging
serialOutput();
}
void output() {
// Joystick mid count
LTCali = 491;
RTCali = 520;
int LTFthres = LTCali - 2;
int LTRthres = LTCali + 2;
int RTFthres = RTCali - 2;
int RTRthres = RTCali + 2;
// Left side Control (499 middle)
if (intlt <= LTFthres) {
LTF = map(intlt, LTFthres, 0, 0, 255);
analogWrite(5, LTF); }
else { analogWrite(5, 0); }
if (intlt >= LTRthres) {
LTR = map(intlt, LTRthres, 998, 0, 255);
analogWrite(4, LTR); }
else { analogWrite(4, 0); }
// Right side Control (499 middle)
if (intrt <= RTFthres) {
RTF = map(intrt, RTFthres, 0, 0, 255);
analogWrite(3, RTF); }
else { analogWrite(3, 0); }
if (intrt >= RTRthres) {
RTR = map(intrt, RTRthres, 998, 0, 255);
analogWrite(2, RTR); }
else { analogWrite(2, 0); }
// Aux functions
if (Rhigh == 0) { digitalWrite(14, HIGH); } else {digitalWrite(14, LOW); }
}
void recData() {
// receive data from PC and save it into inputBuffer
if(mySerial.available() > 0) {
char x = mySerial.read();
// the order of these IF clauses is significant
if (x == endMarker) {
readInProgress = false;
newDataFromPC = true;
inputBuffer[bytesRecvd] = 0;
parseData();
}
if(readInProgress) {
inputBuffer[bytesRecvd] = x;
bytesRecvd ++;
if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1;
}
}
if (x == startMarker) {
bytesRecvd = 0;
readInProgress = true;
}
}
}
void parseData() {
// split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(inputBuffer,",");
intlt = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
intrt = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
Rhigh = atoi(strtokIndx);
}
void serialOutput() {
if (newDataFromPC) {
newDataFromPC = false;
Serial.print(" Tleft: ");
Serial.print(intlt);
Serial.print(" Tright: ");
Serial.print(intrt);
Serial.print(" Hrevs: ");
Serial.print(Rhigh);
Serial.println(">");
}
}
It might not be the 'right' way of doing it, but it works so far. Over the next few weeks I will develop the code further to include using the millis() instead of delay(), hardware serial, an emergency stop, wireless failure shutdown and a few more services.
My main priority at the minute is sending back oil pressure (digital), water temp (analogue) and fuel level (analogue) from the machine arduino to the controller arduino without causing interruption of machine control.
I understand how to implement the millis() function now, but how would I go about sending data back to the controller during this time?
ps. Tom, I will post pictures when the wheels are on and the machine is outside. Currently its on axle stands lol.
Thanks