I have taken up a project to equip my RV with a hybrid vehicle control system that integrates hho and h2o injection in a standard gas V8. Much of the hardware is already installed including two Arduinos. The microcontrollers' end of things is to monitor and display data, but also to control the timing advance of the engine with a servo I have mounted in place of the vacuum advance on the engine distributor, among other things. But I have hit a snag in getting the two Arduinos to communicate.
I am not sure if it is a hardware or a software problem. But on the hardware end, I am using one USB Duemilanove and another that is serial 232. I have been programming each with one laptop on ports com6 and com1 respectively. Meanwhile, the controllers are connected via the 1 - 0 pins and 0 - 1.
The master unit (the USB) mainly runs an LCD and the basic logic, while the serial unit is mainly intended to provide stable servo control, which is a design that seems to be ideal for the RC servos I am using.
In testing, I have been sending an array of three integers from the master to the slave (1, 0, 1), but the slave somehow seems to keep getting 53, 51, 49. and sometimes 53, 51, 109, and sometimes something a little different.
I will leave it at that for now. Somebody probably knows the answer here and it is obvious to them. Any feedback would be appreciated.
If this is a software/syntax issue, the problem should most likely appear in the very first void of one of the two programs, SendData and ReceiveData.
Here is the Main Code:
#include <LiquidCrystal.h>
#define TimeToWarmEngine 4 // in minutes
#define TempToWarmEngine 160 // in Fahrenheit degrees
char Program[] = " Prog: M100.52a";
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int dataSyncRate = 2000; // a period in which to sync data trans
const byte LeftKnobPin = 0; // analog 0
const byte EngineThermistor = 2; // analog 2
const byte FourButtons = 4; // analog 4
const byte RightKnobPin = 5; // analog 5
//const byte InputToggleSwitch = 6;
//const byte AutoSwitch = 7;
const byte OilSendingUnit = 9;
const byte SelectButton = 10;
const byte NumRdgsCt = 48;
const byte NumRdgsLK = 16;
const byte integersToSend = 3;
const byte integersToReceive = 1;
int SendDataArray[integersToSend];
int ReceiveDataArray[integersToReceive];
int Creadings[NumRdgsCt], LKreadings[NumRdgsLK];
int RightKnob = 0, LeftKnob = 0;
int Cindex = 0, Ctotal = 0, Caverage = 0, Creading = 0;
int LKindex = 0, LKtotal = 0, LKaverage = 0, LKreading = 0;
int EgTemp = 0, Advance = 0, count = 0, newData1 = 0;
int BtnTest = 0, BtnRead = 0, BtnRead2 = 0, newData2 = 0;
boolean Start, SystemsGo, EngineOn, dataSent, Automatic;
boolean dataReceived = false, EngineCold = true;
float ExTemp, CoolTemp = 120, tFar;
byte degree[8] = {
B01000, B10100, B01000, B00000, B00000, B00000, B00000,};
void SendData() {
if ((millis() % dataSyncRate) < (dataSyncRate/4)) {
if (dataSent == false) {
if (Automatic == false)
SendDataArray[0] = 0;
else SendDataArray[0] = 1;
SendDataArray[1] = LKaverage;
if (EngineCold == true)
{SendDataArray[2] = 1;}
else SendDataArray[2] = 0;
for(int x=0; x<integersToSend; x++)
Serial.print(SendDataArray[x]);
// Serial.println("master out");
dataSent = true; } }
else dataSent = false; }
void ReceiveData() {
if ((millis() % dataSyncRate) > (3*(dataSyncRate/4))) {
if (dataReceived == false) {
for(int x=0; x<integersToReceive; x++) {
if (Serial.available() > 0) {
ReceiveDataArray[x] = Serial.read(); } }
Serial.flush();
Serial.print(ReceiveDataArray[0]);
// Serial.println("master in");
dataReceived = true; } }
else dataReceived = false; }
void ReadLeftKnob() {
LKreading = map(analogRead(LeftKnobPin), 0, 1023, 0, 32);
LKtotal = LKtotal - LKreadings[LKindex];
LKreadings[LKindex] = LKreading;
LKtotal = LKtotal + LKreadings[LKindex];
LKindex = LKindex + 1;
if (LKindex >= NumRdgsLK) {
LKaverage = LKtotal / NumRdgsLK;
LKindex = 0; } }
void ReadRightKnob() {
RightKnob = analogRead(RightKnobPin); }
void ReadButtons() {
BtnRead = analogRead(FourButtons);
if (BtnRead >= 5) {
delay(50); BtnRead2 = analogRead(FourButtons);
if (((BtnRead + 5) % BtnRead2) <= 10) {
if(BtnRead > 950) {
BtnTest = 2; };
if((BtnRead > 600) && (BtnRead < 950)) {
BtnTest = 4; };
if((BtnRead > 400) && (BtnRead < 600)) {
BtnTest = 3; };
if((BtnRead < 400) && (BtnRead > 10 )) {
BtnTest = 1; }; }; };
if(BtnTest == 2) {
// digitalWrite(AutoSwitch, LOW);
Automatic = true;
BtnTest = 0;
delay(300); };
if(BtnTest == 1) {
// digitalWrite(AutoSwitch, HIGH);
Automatic = false;
BtnTest = 0;
delay(300); };
if (BtnTest == 3) {
BtnTest = 0;
delay(300); }
if (BtnTest == 4) {
BtnTest = 0;
delay(300); }}
void setup() {
Serial.begin(9600);
pinMode(SelectButton, INPUT);
// pinMode(InputToggleSwitch, INPUT);
// digitalWrite(InputToggleSwitch, HIGH);
// pinMode(AutoSwitch, OUTPUT);
// digitalWrite(AutoSwitch, LOW);
// pinMode(OilSendingUnit, INPUT);
Start = false;
EngineOn = false;
SystemsGo = false;
Automatic = true;
lcd.begin(16,2);
lcd.createChar(0, degree);
lcd.clear();
lcd.print(" System Check");
lcd.setCursor(0, 1);
lcd.print(Program);
delay(2500); }
void WarmUp() {
while (Start == false) {
if ((!EngineOn) && (int(digitalRead(OilSendingUnit)) > 0)) {
delay(3000);
if ((!EngineOn) && (int(digitalRead(OilSendingUnit)) > 0)){
EngineOn = 1;
Start = true; }; }
if (!EngineOn && SystemsGo) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Ready ");
delay(200);
if (Start == false ) {
Start = digitalRead(SelectButton); } }
else {
lcd.setCursor(0, 1);
lcd.print(" Fault ");
while (SystemsGo == false ) {
// put tests here for SystemsGo
SystemsGo = true; } } };
if ((millis() << TimeToWarmEngine * 60000) &&
(Caverage << TempToWarmEngine))
EngineCold = true; }
void FigureStuff() {
if (Automatic == true) Advance = ReceiveDataArray[0];
else Advance = LKaverage;
}
void DisplayStuff() {
if (count >= 200) {
lcd.clear();
lcd.print('5');
lcd.print('1');
lcd.setCursor(3,0);
lcd.print("ct");
lcd.print(int(Caverage));
lcd.setCursor(7,1);
if (Automatic == true)
{lcd.print("Auto");}
else lcd.print("Man");
lcd.setCursor(9,0);
lcd.print("egt");
lcd.print(" 00");
lcd.setCursor(0,1);
lcd.print("adv");
lcd.print(Advance);
lcd.write(0);
lcd.setCursor(12,1);
lcd.print("inj");
lcd.print(0);
count = 0; };
++count; }
void loop() {
WarmUp();
SendData();
ReceiveData();
ReadLeftKnob();
ReadRightKnob();
ReadButtons();
FigureStuff();
DisplayStuff();
}