Documentos de Académico
Documentos de Profesional
Documentos de Cultura
h>
#include <OneWire.h>
//temporizador calentadores
const int inputPin = 7; // pin digital para la señal de entrada
const int outputPin = 8; // pin digital para el relé
unsigned long startTime = 0; // tiempo de inicio del temporizador
bool timerActive = false; // indicador de si el temporizador está activo o no
bool lastInputState = LOW; // estado anterior de la señal de entrada
unsigned int digitalPins = 0;
/// Define variables to track scan times
unsigned long Time;
byte LastBroadcast;
// Define variables for reading IO
bool LBLr; // Left Blinker from Digital in on Arduino
bool RBLr; // Right Blinker from Digital in on Arduino
bool HBr; // High Beams from Digital in on Arduino
bool MALFr; // Check Engine Light from Digital in on Arduino
//Sensor temperatura
OneWire ourWire(2);
DallasTemperature sensor1(&ourWire);
// incoming data
byte incomingFrame[17] = { 0 };
unsigned int incomingFramePos = 0;
void setup()
{
#if defined (READWRITE_PINS)
// set digital pins as outputs
for (int i=1; i<14; i++)
{
pinMode(i, OUTPUT);
}
#endif
// init serial
Serial.begin(115200);
//Sensor de temperatura
sensor1.begin();
//Temorizador calentadores
pinMode(inputPin, INPUT);
pinMode(outputPin, OUTPUT);
//Sensor de RPM
pinMode(3, INPUT);
attachInterrupt(0, interrupcion0, RISING);
delay(100);
void loop()
{
// leer estado de la señal de entrada
int inputState = digitalRead(inputPin);
void ReadIOPins(){
LBLr = !digitalRead(LBLPin);
RBLr = !digitalRead(RBLPin);
HBr = !digitalRead(HBPin);
MALFr = !digitalRead(MALFPin);
}
void BuildCanFrames()
{
byte CanBuffer[8];
ReadDigitalStatuses();
ReadAnalogStatuses();
SendCANFramesToSerial();
ReadIncomingSerialData();
sensor1.requestTemperatures();
float temp= sensor1.getTempCByIndex(0);
Serial.print(rpm * 60);
{
rpm = 0;
}
if (kpa++ > 2500)
{
kpa = 10;
}
if (tps++ > 1000)
{
tps = 0;
}
{
clt = temp;
}
// simple counter for sending the text frame to avoid sending it too often.
if (textCounter++ > 4000)
{
textCounter = 0;
}
delay(5);
}
void ReadDigitalStatuses()
{
#if defined (READWRITE_PINS)
// read status of digital pins (1-13)
digitalPins = 0;
int bitposition = 0;
for (int i=1; i<14; i++)
{
if (digitalRead(i) == HIGH) digitalPins |= (1 << bitposition);
bitposition++;
}
#endif
}
void ReadAnalogStatuses()
{
#if defined (READWRITE_PINS)
// read analog pins (0-7)
for (int i=0; i<7; i++)
{
analogPins[i] = analogRead(i);
}
#endif
}
void SendCANFramesToSerial()
{
byte frameData[8];
// build 1st CAN frame, RPM, MAP, CLT, TPS (just example data)
memcpy(frameData, &rpm, 2);
memcpy(frameData + 2, &kpa, 2);
memcpy(frameData + 4, &clt, 2);
memcpy(frameData + 6, &tps, 2);
// build 2nd CAN frame, Arduino digital pins and 2 analog values
memcpy(frameData, &digitalPins, 2);
memcpy(frameData + 2, &analogPins[0], 2);
memcpy(frameData + 4, &analogPins[1], 2);
memcpy(frameData + 6, &analogPins[2], 2);
// text payload
Serial.write(text, strlen(text) + 1);
}
}
void ReadIncomingSerialData()
{
while (Serial.available() > 0)
{
// little bit of extra effort here, since especially Bluetooth connections
// may leave unsent/received data in internal buffer for a long time
// therefore, we cannot be sure that incoming byte stream really starts at
// where we expect it to start.
// check the first incoming bytes tag (0x44, 0x33, 0x22, 0x11)
if (incomingFrame[0] != 0x44)
{
// first incoming byte is not 0x44,
// the tag at the beginning of the frame does not match, this is an invalid
frame
// just zero the incomingFrame buffer and start expecting first byte again
memset(incomingFrame, 0, 17);
incomingFramePos = 0;
}
// zero the incomingFrame buffer and start expecting first byte again
memset(incomingFrame, 0, 17);
incomingFramePos = 0;
}
}
}
// next four bytes contain set value CAN frame id in little endian form
unsigned long canFrameId = 0;
memcpy(&canFrameId, frame + 4, 4);
if (frame[16] == checkByte)
{
// checksum match, this is a valid set value-frame:
// the frame payload data is in frame + 8 bytes
HandleIncomingSetValueFrame(canFrameId, frame + 8);
}
}
analogWrite(0, analogPins[0]);
analogWrite(1, analogPins[1]);
analogWrite(2, analogPins[2]);
#endif
}
else if (canFrameId == 3202)
{
memcpy(&analogPins[3], frameData + 0, 2);
memcpy(&analogPins[4], frameData + 2, 2);
memcpy(&analogPins[5], frameData + 4, 2);
memcpy(&analogPins[6], frameData + 6, 2);
// =======================================================
// Sample Program for sending digital singals to RealDash
// =======================================================
void setup() {
Serial.begin(115200); //for diagnostics
Serial1.begin(115200); // for TTL to Dash
}
// =======================================================
void loop() {
Time = millis();
void ReadIOPins(){
LBLr = !digitalRead(LBLPin);
RBLr = !digitalRead(RBLPin);
HBr = !digitalRead(HBPin);
MALFr = !digitalRead(MALFPin);
}
void BuildCanFrames()
{
byte CanBuffer[8];