E85 Gauge w/ output for under $100
#124
Evolving Member
Thread Starter
#126
Evolved Member
iTrader: (4)
Code
Code:
/******************************************************* This program will sample a 50-150hz signal depending on ethanol content, and output a 0-5V signal via PWM. The LCD will display ethanol content, hz input, mv output, fuel temp ********************************************************/ // include the library code: #include <Wire.h> #include <LCD.h> #include <LiquidCrystal_I2C.h> #define I2C_ADDR 0x27 // <<- Add your address here. #define Rs_pin 0 #define Rw_pin 1 #define En_pin 2 #define BACKLIGHT_PIN 3 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 LiquidCrystal_I2C lcd(0x27,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); int inpPin = 8; //define input pin to 8 int outPin = 11; //define PWM output, possible pins with LCD and 32khz freq. are 3 and 11 (UNO) //Define global variables volatile uint16_t revTick; //Ticks per revolution uint16_t pwm_output = 0; //integer for storing PWM value (0-255 value) int HZ; //unsigned 16bit integer for storing HZ input int ethanol = 0; //Store ethanol percentage here float expectedv; //store expected voltage here - range for typical GM sensors is usually 0.5-4.5v uint16_t voltage = 0; //Store display millivoltage here (0-5000) int duty; //Duty cycle (0.0-100.0) float period; //Store period time here (eg.0.0025 s) float temperature = 0; //Store fuel temperature here int fahr = 0; int cels = 0; static long highTime = 0; static long lowTime = 0; static long tempPulse; void setupTimer() // setup timer1 { TCCR1A = 0; // normal mode TCCR1B = 132; // (10000100) Falling edge trigger, Timer = CPU Clock/256, noise cancellation on TCCR1C = 0; // normal mode TIMSK1 = 33; // (00100001) Input capture and overflow interupts enabled TCNT1 = 0; // start from 0 } ISR(TIMER1_CAPT_vect) // PULSE DETECTED! (interrupt automatically triggered, not called by main program) { revTick = ICR1; // save duration of last revolution TCNT1 = 0; // restart timer for next revolution } ISR(TIMER1_OVF_vect) // counter overflow/timeout { revTick = 0; } // Ticks per second = 0 void setup() { Serial.begin(9600); pinMode(inpPin,INPUT); setPwmFrequency(outPin,1); //Modify frequency on PWM output setupTimer(); // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.setBacklightPin(3,POSITIVE); lcd.setBacklight(HIGH); // Initial screen formatting lcd.setCursor(1, 0); lcd.print("Ethanol: %"); lcd.setCursor(0, 1); lcd.print(" Hz F"); } void loop() { getfueltemp(inpPin); //read fuel temp from input duty cycle if (revTick > 0) // Avoid dividing by zero, sample in the HZ {HZ = 62200 / revTick;} // 3456000ticks per minute, 57600 per second else {HZ = 0;} //needs real sensor test to determine correct tickrate //calculate ethanol percentage if (HZ > 50) // Avoid dividing by zero {ethanol = (HZ-50);} else {ethanol = 0;} if (ethanol > 99) // Avoid overflow in PWM {ethanol = 99;} expectedv = ((((HZ-50.0)*0.01)*4)+0.5); //Screen calculations pwm_output = 1.11 * (255 * (expectedv/5.0)); //calculate output PWM for NEMU lcd.setCursor(12, 0); lcd.print(ethanol); lcd.setCursor(2, 1); lcd.print(HZ); lcd.setCursor(12, 1); lcd.print(fahr); //Use this for celsius //PWM output analogWrite(outPin, pwm_output); //write the PWM value to output pin delay(1000); //make screen more easily readable by not updating it too often Serial.println(ethanol); Serial.println(pwm_output); Serial.println(expectedv); Serial.println(HZ); delay(1000); } void getfueltemp(int inpPin){ //read fuel temp from input duty cycle highTime = 0; lowTime = 0; tempPulse = pulseIn(inpPin,HIGH); if(tempPulse>highTime){ highTime = tempPulse; } tempPulse = pulseIn(inpPin,LOW); if(tempPulse>lowTime){ lowTime = tempPulse; } duty = ((100*(highTime/(double (lowTime+highTime))))); //Calculate duty cycle (integer extra decimal) float T = (float(1.0/float(HZ))); //Calculate total period time float period = float(100-duty)*T; //Calculate the active period time (100-duty)*T float temp2 = float(10) * float(period); //Convert ms to whole number temperature = ((40.25 * temp2)-81.25); //Calculate temperature for display (1ms = -40, 5ms = 80) int cels = int(temperature); cels = cels*0.1; float fahrtemp = ((temperature*1.8)+32); fahr = fahrtemp*0.1; } void setPwmFrequency(int pin, int divisor) { //This code snippet raises the timers linked to the PWM outputs byte mode; //This way the PWM frequency can be raised or lowered. Prescaler of 1 sets PWM output to 32KHz (pin 3, 11) if(pin == 5 || pin == 6 || pin == 9 || pin == 10) { switch(divisor) { case 1: mode = 0x01; break; case 8: mode = 0x02; break; case 64: mode = 0x03; break; case 256: mode = 0x04; break; case 1024: mode = 0x05; break; default: return; } if(pin == 5 || pin == 6) { TCCR0B = TCCR0B & 0b11111000 | mode; } else { TCCR1B = TCCR1B & 0b11111000 | mode; } } else if(pin == 3 || pin == 11) { switch(divisor) { case 1: mode = 0x01; break; case 8: mode = 0x02; break; case 32: mode = 0x03; break; case 64: mode = 0x04; break; case 128: mode = 0x05; break; case 256: mode = 0x06; break; case 1024: mode = 0x7; break; default: return; } TCCR2B = TCCR2B & 0b11111000 | mode; } }
I have modified my own code for a lcd screen output. If you would like the code to have a 16x2 display, please let me know.
Does this get directly copied and pasted, or were supposed to fill in the blanks here lol, can you send me your code complete? I believe I bought a slightly different LCD but i think it should work the same. Im completely new to the arduino
#127
Evolving Member
Thread Starter
the only think that could/may need changed is the "#define I2C_ADDR 0x27 // <<- Add your address here." that is dependent on what the address of your I2C screen is
#128
EvoM Community Team Leader
I was researching my own diy gauge project when I came across these https://www.sparkfun.com/products/11442 has an ATmega328 built into it with the arduino bootloader and complete arduino IDE support. Im waiting for them to get the red ones in stock before I order and start tinkering but i figured i would share what i found.
#129
Evolving Member
Thread Starter
I was researching my own diy gauge project when I came across these https://www.sparkfun.com/products/11442 has an ATmega328 built into it with the arduino bootloader and complete arduino IDE support. Im waiting for them to get the red ones in stock before I order and start tinkering but i figured i would share what i found.
#130
Evolved Member
iTrader: (4)
Heres what im getting right now
Arduino: 1.8.3 (Windows 7), Board: "Arduino/Genuino Uno"
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:30:3: error: stray '\302' in program
<title>Arduino-Library/LCD.h at master · nherment/Arduino-Library · GitHub</title>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:30:3: error: stray '\267' in program
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:30:3: error: stray '\302' in program
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:30:3: error: stray '\267' in program
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:140:8: warning: missing terminating ' character
<!-- '"` --><!-- </textarea></xmp> --></option></form><form accept-charset="UTF-8" action="/nherment/Arduino-Library/search" class="js-site-search-form" data-scoped-search-url="/nherment/Arduino-Library/search" data-unscoped-search-url="/search" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /></div>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:140:3: error: missing terminating ' character
<!-- '"` --><!-- </textarea></xmp> --></option></form><form accept-charset="UTF-8" action="/nherment/Arduino-Library/search" class="js-site-search-form" data-scoped-search-url="/nherment/Arduino-Library/search" data-unscoped-search-url="/search" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /></div>
^
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:528:70: error: stray '#' in program
<td id="LC17" class="blob-code blob-code-inner js-file-line">#<span class="pl-k">ifndef</span> LCD_h</td>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:532:70: error: stray '#' in program
<td id="LC18" class="blob-code blob-code-inner js-file-line">#<span class="pl-k">define</span> <span class="pl-en">LCD_h</span></td>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:541:70: error: stray '#' in program
<td id="LC20" class="blob-code blob-code-inner js-file-line">#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">"</span>WProgram.h<span class="pl-pds">"</span></span></td>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:631:70: error: stray '#' in program
<td id="LC42" class="blob-code blob-code-inner js-file-line">#<span class="pl-k">endif</span></td>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:641:10: warning: missing terminating ' character
<!-- '"` --><!-- </textarea></xmp> --></option></form><form accept-charset="UTF-8" action="" class="js-jump-to-line-form" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /></div>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:641:5: error: missing terminating ' character
<!-- '"` --><!-- </textarea></xmp> --></option></form><form accept-charset="UTF-8" action="" class="js-jump-to-line-form" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /></div>
^
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:689:12: warning: missing terminating ' character
You can't perform that action at this time.
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:689:5: error: missing terminating ' character
You can't perform that action at this time.
^
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:7:1: error: expected unqualified-id before '<' token
<!DOCTYPE html>
^
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:406:187: error: 'modified' does not name a type
<a href="/nherment/Arduino-Library/commit/25af561ee8dd5361fb8a41edeb1ede620a5e50c1" class="message" data-pjax="true" title="Adding doc & modified license">Adding doc & modified license</a>
^
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:541:151: error: expected unqualified-id before '<' token
<td id="LC20" class="blob-code blob-code-inner js-file-line">#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">"</span>WProgram.h<span class="pl-pds">"</span></span></td>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:541:195: error: expected unqualified-id before '<' token
<td id="LC20" class="blob-code blob-code-inner js-file-line">#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">"</span>WProgram.h<span class="pl-pds">"</span></span></td>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:622:72: error: expected unqualified-id before '<' token
<td id="LC40" class="blob-code blob-code-inner js-file-line">};</td>
^
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:672:18: error: expected unqualified-id before numeric constant
<li>© 2017 <span title="0.10192s from github-fe-b03c132.cp1-iad.github.net">GitHub</span>, Inc.</li>
^
C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o: In function 'void setup()':
sketch_jun21a:69: error: 'lcd' was not declared in this scope
lcd.begin(16, 2);
^
sketch_jun21a:70: error: 'POSITIVE' was not declared in this scope
lcd.setBacklightPin(3,POSITIVE);
^
C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o: In function 'void loop()':
sketch_jun21a:101: error: 'lcd' was not declared in this scope
lcd.setCursor(12, 0);
^
exit status 1
'lcd' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Arduino: 1.8.3 (Windows 7), Board: "Arduino/Genuino Uno"
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:30:3: error: stray '\302' in program
<title>Arduino-Library/LCD.h at master · nherment/Arduino-Library · GitHub</title>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:30:3: error: stray '\267' in program
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:30:3: error: stray '\302' in program
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:30:3: error: stray '\267' in program
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:140:8: warning: missing terminating ' character
<!-- '"` --><!-- </textarea></xmp> --></option></form><form accept-charset="UTF-8" action="/nherment/Arduino-Library/search" class="js-site-search-form" data-scoped-search-url="/nherment/Arduino-Library/search" data-unscoped-search-url="/search" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /></div>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:140:3: error: missing terminating ' character
<!-- '"` --><!-- </textarea></xmp> --></option></form><form accept-charset="UTF-8" action="/nherment/Arduino-Library/search" class="js-site-search-form" data-scoped-search-url="/nherment/Arduino-Library/search" data-unscoped-search-url="/search" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /></div>
^
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:528:70: error: stray '#' in program
<td id="LC17" class="blob-code blob-code-inner js-file-line">#<span class="pl-k">ifndef</span> LCD_h</td>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:532:70: error: stray '#' in program
<td id="LC18" class="blob-code blob-code-inner js-file-line">#<span class="pl-k">define</span> <span class="pl-en">LCD_h</span></td>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:541:70: error: stray '#' in program
<td id="LC20" class="blob-code blob-code-inner js-file-line">#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">"</span>WProgram.h<span class="pl-pds">"</span></span></td>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:631:70: error: stray '#' in program
<td id="LC42" class="blob-code blob-code-inner js-file-line">#<span class="pl-k">endif</span></td>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:641:10: warning: missing terminating ' character
<!-- '"` --><!-- </textarea></xmp> --></option></form><form accept-charset="UTF-8" action="" class="js-jump-to-line-form" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /></div>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:641:5: error: missing terminating ' character
<!-- '"` --><!-- </textarea></xmp> --></option></form><form accept-charset="UTF-8" action="" class="js-jump-to-line-form" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /></div>
^
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:689:12: warning: missing terminating ' character
You can't perform that action at this time.
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:689:5: error: missing terminating ' character
You can't perform that action at this time.
^
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:7:1: error: expected unqualified-id before '<' token
<!DOCTYPE html>
^
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:406:187: error: 'modified' does not name a type
<a href="/nherment/Arduino-Library/commit/25af561ee8dd5361fb8a41edeb1ede620a5e50c1" class="message" data-pjax="true" title="Adding doc & modified license">Adding doc & modified license</a>
^
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:541:151: error: expected unqualified-id before '<' token
<td id="LC20" class="blob-code blob-code-inner js-file-line">#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">"</span>WProgram.h<span class="pl-pds">"</span></span></td>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:541:195: error: expected unqualified-id before '<' token
<td id="LC20" class="blob-code blob-code-inner js-file-line">#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">"</span>WProgram.h<span class="pl-pds">"</span></span></td>
^
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:622:72: error: expected unqualified-id before '<' token
<td id="LC40" class="blob-code blob-code-inner js-file-line">};</td>
^
In file included from C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o:9:0:
C:\Program Files (x86)\Arduino\libraries\LiquidCrystal_I2C/LCD.h:672:18: error: expected unqualified-id before numeric constant
<li>© 2017 <span title="0.10192s from github-fe-b03c132.cp1-iad.github.net">GitHub</span>, Inc.</li>
^
C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o: In function 'void setup()':
sketch_jun21a:69: error: 'lcd' was not declared in this scope
lcd.begin(16, 2);
^
sketch_jun21a:70: error: 'POSITIVE' was not declared in this scope
lcd.setBacklightPin(3,POSITIVE);
^
C:\Users\CJ Windows 7\Documents\Arduino\sketch_jun21a\sketch_jun21a.in o: In function 'void loop()':
sketch_jun21a:101: error: 'lcd' was not declared in this scope
lcd.setCursor(12, 0);
^
exit status 1
'lcd' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Code:
#include <LiquidCrystal_I2C.h> #include <LiquidCrystal_I2C.h> #include <LiquidCrystal.h> // include the library code: #include <Wire.h> #include <LCD.h> #include <LiquidCrystal_I2C.h> #define I2C_ADDR = 0x3f // <<- Add your address here. #define Rs_pin 0 #define Rw_pin 1 #define En_pin 2 #define BACKLIGHT_PIN 3 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 LiquidCrystal_I2C lcd(0x3f,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); int inpPin = 8; //define input pin to 8 int outPin = 11; //define PWM output, possible pins with LCD and 32khz freq. are 3 and 11 (UNO) //Define global variables volatile uint16_t revTick; //Ticks per revolution uint16_t pwm_output = 0; //integer for storing PWM value (0-255 value) int HZ; //unsigned 16bit integer for storing HZ input int ethanol = 0; //Store ethanol percentage here float expectedv; //store expected voltage here - range for typical GM sensors is usually 0.5-4.5v uint16_t voltage = 0; //Store display millivoltage here (0-5000) int duty; //Duty cycle (0.0-100.0) float period; //Store period time here (eg.0.0025 s) float temperature = 0; //Store fuel temperature here int fahr = 0; int cels = 0; static long highTime = 0; static long lowTime = 0; static long tempPulse; void setupTimer() // setup timer1 { TCCR1A = 0; // normal mode TCCR1B = 132; // (10000100) Falling edge trigger, Timer = CPU Clock/256, noise cancellation on TCCR1C = 0; // normal mode TIMSK1 = 33; // (00100001) Input capture and overflow interupts enabled TCNT1 = 0; // start from 0 } ISR(TIMER1_CAPT_vect) // PULSE DETECTED! (interrupt automatically triggered, not called by main program) { revTick = ICR1; // save duration of last revolution TCNT1 = 0; // restart timer for next revolution } ISR(TIMER1_OVF_vect) // counter overflow/timeout { revTick = 0; } // Ticks per second = 0 void setup() { Serial.begin(9600); pinMode(inpPin,INPUT); setPwmFrequency(outPin,1); //Modify frequency on PWM output setupTimer(); // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.setBacklightPin(3,POSITIVE); lcd.setBacklight(HIGH); // Initial screen formatting lcd.setCursor(1, 0); lcd.print("Ethanol: %"); lcd.setCursor(0, 1); lcd.print(" Hz F"); } void loop() { getfueltemp(inpPin); //read fuel temp from input duty cycle if (revTick > 0) // Avoid dividing by zero, sample in the HZ {HZ = 62200 / revTick;} // 3456000ticks per minute, 57600 per second else {HZ = 0;} //needs real sensor test to determine correct tickrate //calculate ethanol percentage if (HZ > 50) // Avoid dividing by zero {ethanol = (HZ-50);} else {ethanol = 0;} if (ethanol > 99) // Avoid overflow in PWM {ethanol = 99;} expectedv = ((((HZ-50.0)*0.01)*4)+0.5); //Screen calculations pwm_output = 1.11 * (255 * (expectedv/5.0)); //calculate output PWM for NEMU lcd.setCursor(12, 0); lcd.print(ethanol); lcd.setCursor(2, 1); lcd.print(HZ); lcd.setCursor(12, 1); lcd.print(fahr); //Use this for celsius //PWM output analogWrite(outPin, pwm_output); //write the PWM value to output pin delay(1000); //make screen more easily readable by not updating it too often Serial.println(ethanol); Serial.println(pwm_output); Serial.println(expectedv); Serial.println(HZ); delay(1000); } void getfueltemp(int inpPin){ //read fuel temp from input duty cycle highTime = 0; lowTime = 0; tempPulse = pulseIn(inpPin,HIGH); if(tempPulse>highTime){ highTime = tempPulse; } tempPulse = pulseIn(inpPin,LOW); if(tempPulse>lowTime){ lowTime = tempPulse; } duty = ((100*(highTime/(double (lowTime+highTime))))); //Calculate duty cycle (integer extra decimal) float T = (float(1.0/float(HZ))); //Calculate total period time float period = float(100-duty)*T; //Calculate the active period time (100-duty)*T float temp2 = float(10) * float(period); //Convert ms to whole number temperature = ((40.25 * temp2)-81.25); //Calculate temperature for display (1ms = -40, 5ms = 80) int cels = int(temperature); cels = cels*0.1; float fahrtemp = ((temperature*1.8)+32); fahr = fahrtemp*0.1; } void setPwmFrequency(int pin, int divisor) { //This code snippet raises the timers linked to the PWM outputs byte mode; //This way the PWM frequency can be raised or lowered. Prescaler of 1 sets PWM output to 32KHz (pin 3, 11) if(pin == 5 || pin == 6 || pin == 9 || pin == 10) { switch(divisor) { case 1: mode = 0x01; break; case 8: mode = 0x02; break; case 64: mode = 0x03; break; case 256: mode = 0x04; break; case 1024: mode = 0x05; break; default: return; } if(pin == 5 || pin == 6) { TCCR0B = TCCR0B & 0b11111000 | mode; } else { TCCR1B = TCCR1B & 0b11111000 | mode; } } else if(pin == 3 || pin == 11) { switch(divisor) { case 1: mode = 0x01; break; case 8: mode = 0x02; break; case 32: mode = 0x03; break; case 64: mode = 0x04; break; case 128: mode = 0x05; break; case 256: mode = 0x06; break; case 1024: mode = 0x7; break; default: return; } TCCR2B = TCCR2B & 0b11111000 | mode; } }
#131
Evolving Member
Thread Starter
I edited this for your I2C address, copy and paste this. and reinstalled the libarys i have on page one. delete the 2 folders u have, and move the new ones over.
[/QUOTE]
Code:
/******************************************************* This program will sample a 50-150hz signal depending on ethanol content, and output a 0-5V signal via PWM. The LCD will display ethanol content, hz input, mv output, fuel temp ********************************************************/ // include the library code: #include <Wire.h> #include <LCD.h> #include <LiquidCrystal_I2C.h> #define I2C_ADDR 0x3f // <<- Add your address here. #define Rs_pin 0 #define Rw_pin 1 #define En_pin 2 #define BACKLIGHT_PIN 3 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 LiquidCrystal_I2C lcd(0x27,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); int inpPin = 8; //define input pin to 8 int outPin = 11; //define PWM output, possible pins with LCD and 32khz freq. are 3 and 11 (UNO) //Define global variables volatile uint16_t revTick; //Ticks per revolution uint16_t pwm_output = 0; //integer for storing PWM value (0-255 value) int HZ; //unsigned 16bit integer for storing HZ input int ethanol = 0; //Store ethanol percentage here float expectedv; //store expected voltage here - range for typical GM sensors is usually 0.5-4.5v uint16_t voltage = 0; //Store display millivoltage here (0-5000) int duty; //Duty cycle (0.0-100.0) float period; //Store period time here (eg.0.0025 s) float temperature = 0; //Store fuel temperature here int fahr = 0; int cels = 0; static long highTime = 0; static long lowTime = 0; static long tempPulse; void setupTimer() // setup timer1 { TCCR1A = 0; // normal mode TCCR1B = 132; // (10000100) Falling edge trigger, Timer = CPU Clock/256, noise cancellation on TCCR1C = 0; // normal mode TIMSK1 = 33; // (00100001) Input capture and overflow interupts enabled TCNT1 = 0; // start from 0 } ISR(TIMER1_CAPT_vect) // PULSE DETECTED! (interrupt automatically triggered, not called by main program) { revTick = ICR1; // save duration of last revolution TCNT1 = 0; // restart timer for next revolution } ISR(TIMER1_OVF_vect) // counter overflow/timeout { revTick = 0; } // Ticks per second = 0 void setup() { Serial.begin(9600); pinMode(inpPin,INPUT); setPwmFrequency(outPin,1); //Modify frequency on PWM output setupTimer(); // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.setBacklightPin(3,POSITIVE); lcd.setBacklight(HIGH); // Initial screen formatting lcd.setCursor(1, 0); lcd.print("Ethanol: %"); lcd.setCursor(0, 1); lcd.print(" Hz F"); } void loop() { getfueltemp(inpPin); //read fuel temp from input duty cycle if (revTick > 0) // Avoid dividing by zero, sample in the HZ {HZ = 62200 / revTick;} // 3456000ticks per minute, 57600 per second else {HZ = 0;} //needs real sensor test to determine correct tickrate //calculate ethanol percentage if (HZ > 50) // Avoid dividing by zero {ethanol = (HZ-50);} else {ethanol = 0;} if (ethanol > 99) // Avoid overflow in PWM {ethanol = 99;} expectedv = ((((HZ-50.0)*0.01)*4)+0.5); //Screen calculations pwm_output = 1.11 * (255 * (expectedv/5.0)); //calculate output PWM for NEMU lcd.setCursor(12, 0); lcd.print(ethanol); lcd.setCursor(2, 1); lcd.print(HZ); lcd.setCursor(12, 1); lcd.print(fahr); //Use this for celsius //PWM output analogWrite(outPin, pwm_output); //write the PWM value to output pin delay(1000); //make screen more easily readable by not updating it too often Serial.println(ethanol); Serial.println(pwm_output); Serial.println(expectedv); Serial.println(HZ); delay(1000); } void getfueltemp(int inpPin){ //read fuel temp from input duty cycle highTime = 0; lowTime = 0; tempPulse = pulseIn(inpPin,HIGH); if(tempPulse>highTime){ highTime = tempPulse; } tempPulse = pulseIn(inpPin,LOW); if(tempPulse>lowTime){ lowTime = tempPulse; } duty = ((100*(highTime/(double (lowTime+highTime))))); //Calculate duty cycle (integer extra decimal) float T = (float(1.0/float(HZ))); //Calculate total period time float period = float(100-duty)*T; //Calculate the active period time (100-duty)*T float temp2 = float(10) * float(period); //Convert ms to whole number temperature = ((40.25 * temp2)-81.25); //Calculate temperature for display (1ms = -40, 5ms = 80) int cels = int(temperature); cels = cels*0.1; float fahrtemp = ((temperature*1.8)+32); fahr = fahrtemp*0.1; } void setPwmFrequency(int pin, int divisor) { //This code snippet raises the timers linked to the PWM outputs byte mode; //This way the PWM frequency can be raised or lowered. Prescaler of 1 sets PWM output to 32KHz (pin 3, 11) if(pin == 5 || pin == 6 || pin == 9 || pin == 10) { switch(divisor) { case 1: mode = 0x01; break; case 8: mode = 0x02; break; case 64: mode = 0x03; break; case 256: mode = 0x04; break; case 1024: mode = 0x05; break; default: return; } if(pin == 5 || pin == 6) { TCCR0B = TCCR0B & 0b11111000 | mode; } else { TCCR1B = TCCR1B & 0b11111000 | mode; } } else if(pin == 3 || pin == 11) { switch(divisor) { case 1: mode = 0x01; break; case 8: mode = 0x02; break; case 32: mode = 0x03; break; case 64: mode = 0x04; break; case 128: mode = 0x05; break; case 256: mode = 0x06; break; case 1024: mode = 0x7; break; default: return; } TCCR2B = TCCR2B & 0b11111000 | mode; } }