#include <OneWire.h>
//inintial settings ****************************************************************************************
//Номера аналоговых входов датчиков
byte volt = 0; //analog input number voltage
byte fuel = 1; //analog input number fuel sensor
byte temp = 2; //analog input number coolant temp sensor
byte cren = 3; //analog input number cren sensor
byte diff = 4; //analog input number diff sensor
byte an00 = 5; //analog input number analog00 sensor
byte an01 = 6; //analog input number analog01 sensor
byte an02 = 7; //analog input number analog02 sensor
byte an03 = 8; //analog input number analog03 sensor
//Номера цифровых входов зажигания и световых приборов
byte igni = 4; //digital input number ignition
byte trnL = 5; //digital input number left turn
byte trnR = 6; //digital input number right turn
byte ligh = 7; //digital input number parking lights
byte loli = 8; //digital input number low beam
byte hili = 9; //digital input number high beam
byte fogF = 10; //digital input number front fog lights
byte fogR = 11; //digital input number rear fog lights
byte revr = 12; //digital input number revers lamp
//Номера цифровых входов смены скина и сброса пробега
byte skin = 24; //digital input number skin change
byte trip = 26; //digital input number trip reset
//Номера цифровых входов КПП
byte gP = 28; //digital input number P
byte gN = 30; //digital input number N
byte gD = 32; //digital input number D
byte gL = 34; //digital input number L
byte g2 = 36; //digital input number 2
//Номера входов контрольных ламп
byte lm00 = 23; //digital input number warning lamp 0(-)
byte lm01 = 25; //digital input number warning lamp 1(-)
byte lm02 = 27; //digital input number warning lamp 2(-)
byte lm03 = 29; //digital input number warning lamp 3(-)
byte lm04 = 31; //digital input number warning lamp 4(-)
byte lm05 = 33; //digital input number warning lamp 5(-)
byte lm06 = 35; //digital input number warning lamp 6
byte lm07 = 37; //digital input number warning lamp 7
byte lm08 = 39; //digital input number warning lamp 8
byte lm09 = 41; //digital input number warning lamp 9
byte lm10 = 43; //digital input number warning lamp 10
byte lm11 = 45; //digital input number warning lamp 11
byte lm12 = 47; //digital input number warning lamp 12
byte lm13 = 49; //digital input number warning lamp 13
byte lm14 = 51; //digital input number warning lamp 14
byte lm15 = 53; //digital input number warning lamp 15
//Номера входов контрольных дверей
byte dfl = 38; //digital input number front left door lamp [24]
byte dfr = 40; //digital input number front right door lamp [25]
byte drl = 42; //digital input number rear left door lamp [26]
byte drr = 44; //digital input number rear right door lamp [27]
byte dtr = 46; //digital input number trunk lamp [28]
byte sp_sens = 6; //PPR speed sensor
byte ds_pin = 22; //digital input number DS18B20
byte refr = 20; //data period, ms
//******************************************************************************************************
unsigned long sp_mult = 0; //freq to speed
volatile unsigned long millis_t = 0;
//******************************************************************************************************
volatile unsigned long micros_sp = 0;
volatile byte sz = 0; //zero counter
volatile unsigned long sp = 0; //speed
volatile boolean st = false; //trigger
//******************************************************************************************************
volatile unsigned long micros_th = 0;
volatile byte tz = 0; //zero counter
volatile unsigned int th = 0; //rpm
volatile boolean tt = false; //trigger
//******************************************************************************************************
OneWire ds(ds_pin); //initialization OneWire
volatile unsigned int ds_tm = 0; //temp DS18B20
unsigned long millis_ds = 0;
const int ds_upd = 1000; //read period DS18B20, ms
//******************************************************************************************************
volatile unsigned int vo = 0; //voltage
volatile unsigned int fu = 0; //fuel
volatile unsigned int tm = 0; //coolant temp
byte GrB = 0;
String Gr = " "; //gear
String Out = ""; //data string
//******************************************************************************************************
void setup(){
Serial .begin(115200); //serial port RX0/TX0(USB terminal)
Serial2.begin(115200); //serial port RX2/TX2(ESP8266, HLK-RM04, DT-06)
attachInterrupt(0, spd, FALLING); //speed sensor interruption
attachInterrupt(1, tah, RISING); //tahometr interruption
sp_mult = 3600000/sp_sens; //freq to speed
pinMode (skin, OUTPUT); digitalWrite(skin, HIGH);
pinMode (trip, OUTPUT); digitalWrite(trip, HIGH);
tone(21,200);
}
void loop(){
DallasRd();
refresh();
}
void refresh(){ //read inputs
if((millis() - millis_t) >= refr){
millis_t = millis();
vo = analogRead(volt);
fu = analogRead(fuel);
tm = analogRead(temp);
switch ((digitalRead(gP) << 4)
+ (digitalRead(gN) << 3)
+ (digitalRead(gD) << 2)
+ (digitalRead(gL) << 1)
+ (digitalRead(g2))){
case 16: Gr = "P"; break;
case 8: Gr = "N"; break;
case 4: Gr = "D"; break;
case 2: Gr = "L"; break;
case 1: Gr = "2"; break;
}
//********************************************************************
Out += (sp / 100) % 10; //speed [0]
Out += (sp / 10) % 10;
Out += (sp) % 10;
Out += ",";
//********************************************************************
Out += (th / 10000) % 10; //rpm [1]
Out += (th / 1000) % 10;
Out += (th / 100) % 10;
Out += (th / 10) % 10;
Out += (th) % 10;
Out += ",";
//********************************************************************
Out += (vo / 1000) % 10; //voltage [2]
Out += (vo / 100) % 10;
Out += (vo / 10) % 10;
Out += (vo) % 10;
Out += ",";
//********************************************************************
Out += (fu / 1000) % 10; //fuel [3]
Out += (fu / 100) % 10;
Out += (fu / 10) % 10;
Out += (fu) % 10;
Out += ",";
//********************************************************************
Out += (tm / 1000) % 10; //coolant temp [4]
Out += (tm / 100) % 10;
Out += (tm / 10) % 10;
Out += (tm) % 10;
Out += ",";
//********************************************************************
Out += analogRead(cren); Out += ","; //analog sensor00 [5]
Out += analogRead(diff); Out += ","; //analog sensor01 [6]
Out += analogRead(an00); Out += ","; //analog sensor00 [7]
Out += analogRead(an01); Out += ","; //analog sensor01 [8]
Out += analogRead(an02); Out += ","; //analog sensor02 [9]
Out += analogRead(an03); Out += ","; //analog sensor03 [10]
Out += "0,0,0,0,"; //[11],[12],[13],[14]
//********************************************************************
if(ds_tm > 295){
Out += " ";
}else{
Out += (ds_tm / 100) % 10;
Out += (ds_tm / 10) % 10; //temp DS18B20 [15]
Out += (ds_tm) % 10;
}
Out += ",";
//********************************************************************
Out += Gr; //gear [16]
Out += ",";
//********************************************************************
Out += digitalRead(igni); //ignition [0]
Out += digitalRead(loli); // [1]
//********************************************************************
Out += digitalRead(revr); //revers lamp [2]
//********************************************************************
Out += ( digitalRead(trnL) //left turn [3]
+ digitalRead(trnR) * 2); //right turn
//********************************************************************
Out += ( digitalRead(ligh) //parking lights [4]
+ digitalRead(hili) * 2); //high beam
//********************************************************************
Out += ( digitalRead(fogF) //front fog lights[5]
+ digitalRead(fogR) * 2); //rear fog lights
//********************************************************************
Out += !digitalRead(skin); // [6]
Out += !digitalRead(trip); // [7]
Out += !digitalRead(lm00); //input warning lamp 0 [8]
Out += !digitalRead(lm01); //input warning lamp 1 [9]
Out += !digitalRead(lm02); //input warning lamp 2[10]
Out += !digitalRead(lm03); //input warning lamp 3[11]
Out += !digitalRead(lm04); //input warning lamp 4[12]
Out += !digitalRead(lm05); //input warning lamp 5[13]
Out += digitalRead(lm06); //input warning lamp 6[14]
Out += digitalRead(lm07); //input warning lamp 7[15]
Out += digitalRead(lm08); //input warning lamp 8[16]
Out += digitalRead(lm09); //input warning lamp 9[17]
Out += digitalRead(lm10); //input warning lamp 10[18]
Out += digitalRead(lm11); //input warning lamp 11[19]
Out += digitalRead(lm12); //input warning lamp 12[20]
Out += digitalRead(lm13); //input warning lamp 13[21]
Out += digitalRead(lm14); //input warning lamp 14[22]
Out += digitalRead(lm15); //input warning lamp 15[23]
Out += !digitalRead(dfl); //front left door lamp [24]
Out += !digitalRead(dfr); //front right door lamp [25]
Out += !digitalRead(drl); //rear left door lamp [26]
Out += !digitalRead(drr); //rear right door lamp [27]
Out += !digitalRead(dtr); //trunk lamp [28]
Out += ":\n";
Serial .print(Out);
Serial .flush();
Serial2.print(Out);
Serial2.flush();
if(tz != 0){tz--;}else{th = 0;};
if(sz != 0){sz--;}else{sp = 0;};
Out = String("");
}
}
//******************************************************************************************************
void spd(){
if(!st){micros_sp = micros();}
else{
sp = (sp_mult/(micros() - micros_sp));
}
st = !st;
sz = 30;
}
//******************************************************************************************************
void tah(){
if(!tt){micros_th = micros();}
else {th = (30000000/(micros() - micros_th));}
tt = !tt;
tz = 30;
}
//******************************************************************************************************
int DallasRd(){
byte data[2];
if ((millis() - millis_ds) > ds_upd){
ds.reset();
ds.write(0xCC);
ds.write(0xBE);
data[0] = ds.read();
data[1] = ds.read();
int16_t tm = (data[1] << 8) | data[0];
ds_tm = (tm / 16) + 200;
millis_ds = millis();
ds.reset();
ds.write(0xCC);
ds.write(0x44, 1);
}
}