Chcem si zostrojiť do auta flex fuel anylzér ktorý na základe hotnoty etanolu upravuje riadiacu jednotku auta našiel som kód ktorý by mi vyhovoval, až na to že hodnotu etanolu zobrazuje cez PLX iMFD display ktorý je pomerne drahý, tak to chcem prerobyť na Oled 1306.
Upravil som preto kód na nasledujúci, ale hodnotu etanolu mi zobrazuje stále 0. Arduinu som vpodstate začiatočník preto sa obraciam na vás o radu.
Kód: Vybrat vše
//Flex Fuel sensor code
//Input frequency on pin 8 - pulled up to 5V through 1.8K resistor
//V_out pin 3 - 0-5V output through 1.8K resistor with 100uF capacitor to ground
//PLX iMFD serial packet on Tx pin 0 through 3.3V TTL voltage divider (3.3K/6.8K)
//TRAC double click code
//Input switch on pin 6 - pulled up to 5V through 3.3K resistor + 3.3uF capacitor.
//Output on pin 10 through NPN transistor to take VSC connection to ground
#include <Arduino.h>
#include <FreqMeasure.h>
#include <EEPROM.h>
#include "U8glib.h"
U8GLIB_SSD1306_128X64 mujOled(U8G_I2C_OPT_NONE);
//FlexFuel sensor
double sum = 0;
int count = 0;
int ethanol_int, V_out_int;
float freq, ethanol, V_out, V_out_avg, E_scalar;
float E0 = 55.4; //calibration data for pure gasoline
float E85 = 130.8; //calibration data for E85
//EEPROM store
int EE_V_addr = 0;
float V_out_default = 147;
long Elast, Enow, Edelta;
//PLX data
//long Plast, Pnow, Pdelta;
// byte PLXpacket[7] {0x80, 0x00, 0x11, 0x00, 0x00, 0x00, 0x40};
//PLX iMFD packet, sent every 100ms:
//0x80 Start byte
//0x00 0x11 Sensor device fuel level
//0x00 Sensor instance
//0x00 0x00 Sensor value (integer, unscaled)
//0x40 Stop byte
//TRAC switch double click
bool TRAC_buttonMonitor = HIGH;
int debounce = 20;
int doubleClick = 200;
int longClick = 3000;
int TRAC_initializeDelay = 5000;
int TRAC_buttonState = 4; //initialize to 0 for normal operation, 4 for on by default after delay
long TRAC_buttonCounter = 0;
//char str[1];
//void drawTest(void) {
//mujOled.setFont(u8g_font_unifont);
//mujOled.drawStr( 0, 20, "Flex Fuel");
//}
void setup()
{
pinMode (3, OUTPUT);
pinMode (10, OUTPUT);
digitalWrite (10, LOW);
pinMode (6, INPUT);
EEPROM.get(EE_V_addr, V_out_avg); //load last V_out_avg
if (isnan(V_out_avg))
{
V_out_avg = V_out_default;
}
V_out_int = (int)V_out_avg; //convert to integer for analogWrite
analogWrite(3, V_out_int); //output V_out as PWM voltage on pin 3
Elast = millis();
E_scalar = (E85 - E0) / 85;
FreqMeasure.begin();
Serial.begin(9600);
//Plast = millis();
//Serial.println("Flex Fuel");
}
void loop()
{
//Read FlexFuel sensor frequency
if (FreqMeasure.available())
{
// average several readings together
sum = sum + FreqMeasure.read();
count = count + 1;
if (count > 24)
{
freq = FreqMeasure.countToFrequency(sum / count);
sum = 0;
count = 0;
//Convert frequency to E%
ethanol = (freq - E0) / E_scalar; //scale frequency to E% interpolating E0 and E85 values
if (ethanol > 100)
{
ethanol = 100;
}
if (ethanol < 0)
{
ethanol = 0;
}
ethanol_int = (int)ethanol;
//FlexFuel voltage output
V_out = 255 - (2.55 * ethanol);
V_out_avg = V_out_avg + (0.1 * (V_out - V_out_avg)); //V_out_avg as moving average
V_out_int = (int)V_out_avg; //convert to integer for analogWrite
analogWrite(3, V_out_int); //output V_out as PWM voltage on pin 3
}
}
//Store EEPROM data every 5 minutes
Enow = millis();
Edelta = Enow - Elast;
if (Edelta >= 300000)
{
Elast = Enow;
EEPROM.put(EE_V_addr, V_out_avg ); //store V_out_avg to EEPROM
}
//PLX data packet
//Pnow = millis(); //send PLX packet on Tx pin every 100ms
// Pdelta = Pnow - Plast;
//if (Pdelta >= 100)
//{
//if (ethanol_int > 63) { //PLX iMFD only uses 6 LSB bits, 2 MSB must be 00 in each byte
// Plast = Pnow;
// PLXpacket[4] = 0x01;
// int ethanol_int_LSB = ethanol_int - 64;
// PLXpacket[5] = ethanol_int_LSB;
//Serial.write(PLXpacket, 7);
// }
// else {
// Plast = Pnow;
// PLXpacket[4] = 0x00;
// PLXpacket[5] = ethanol_int; //set data byte in PLX packet to E%
// Serial.write(PLXpacket, 7);
// }
//}
mujOled.firstPage();
do {
mujOled.setFont(u8g_font_helvB08);
mujOled.setPrintPos( 0, 15);
mujOled.print("Eth:");
mujOled.setPrintPos( 40, 15);
mujOled.print(ethanol_int);
mujOled.setPrintPos( 80, 15);
mujOled.print("%");
} while( mujOled.nextPage() );
//TRAC switch doubleclick
TRAC_buttonMonitor = digitalRead(6);
switch (TRAC_buttonState)
{
case 0: //inactive
if (TRAC_buttonMonitor == LOW) // button pressed
{
digitalWrite (10, HIGH); //Transistor on
TRAC_buttonState = 1; //increment state
}
break;
case 1: //check for release
if (TRAC_buttonMonitor == HIGH) //if button released
{
TRAC_buttonCounter = millis(); //start counter
TRAC_buttonState = 2; //increment state
}
break;
case 2: //wait for doubleClick
if (TRAC_buttonMonitor == LOW && (millis() - TRAC_buttonCounter) > debounce && (millis() - TRAC_buttonCounter) < doubleClick) //second click registered
{
TRAC_buttonState = 3; //increment state
}
else if ((millis() - TRAC_buttonCounter) > doubleClick)
{
digitalWrite (10, LOW); //Transistor off
TRAC_buttonState = 0; //reset state
}
break;
case 3: //doubleClick made
if ((millis() - TRAC_buttonCounter) > longClick)
{
digitalWrite (10, LOW); //Transistor off
TRAC_buttonState = 0; //reset state
}
break;
case 4: //initialize after delay by default
if (millis() > TRAC_initializeDelay)
{
digitalWrite (10, HIGH); //Transistor on
TRAC_buttonCounter = millis(); //start counter
TRAC_buttonState = 3; //set state to doubleClick
}
break;
}
}