so im making a oled display read my motorcycles throttle position in volts and percent of opening. Iv got the voltage part working fine, however the percentage part is not working. It shows 100% at 5V, but at any other voltage below 5V it shows 0%...Iv forum searched a bit and tryed a few different things like adding Floats and whatnot but it still isnt working...I could use alittle help here as im kind of a newb in the coding.
the idea:
the bikes throttle position sensor operates @ 5V. When at idle the sensor puts out a .475v signal when the throttle is full closed. as i apply throttle, the sensors voltage changes from .475v up to 4.30v @ wide open throttle. so i have a voltage sweep from .475 thru 4.30 based on its position.
while i can get the voltage to read, I cant get the percentage to read. how can i get percentage to read 0% @ .475 thru 100% @ 4.30v ?
heres my full code:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
//uncomment line 17 - 35 and 55,56 to show splash screen
//#define LOGO16_GLCD_HEIGHT 16
//#define LOGO16_GLCD_WIDTH 16
//static const unsigned char PROGMEM logo16_glcd_bmp[] =
//{ B00000000, B11000000,
// B00000001, B11000000,
// B00000001, B11000000,
// B00000011, B11100000,
// B11110011, B11100000,
// B11111110, B11111000,
// B01111110, B11111111,
// B00110011, B10011111,
// B00011111, B11111100,
// B00001101, B01110000,
// B00011011, B10100000,
// B00111111, B11100000,
// B00111111, B11110000,
// B01111100, B11110000,
// B01110000, B01110000,
// B00000000, B00110000 };
#if (SH1106_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SH1106.h!");
#endif
const float referenceVolts = 5.0; // the default reference on a 5 volt board
const int batteryPin = 0; // +V from battery is connected to analog pin 0
float battv;
void setup() {
Serial.begin(9600);
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SH1106_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
// init done
// Show image buffer on the display hardware.
// Since the buffer is intialized with an Adafruit splashscreen
// internally, this will display the splashscreen.
//display.display();
//delay(2000);
// Clear the buffer.
display.clearDisplay();
}
void loop() {
// text display
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(32,0);
display.println("TPS MONITOR");
display.display();
//display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,14);
display.println("Volts");
display.display();
//display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,30);
display.println("Percent");
display.display();
//display.clearDisplay();
checkbatt();
display.setTextSize(2);
display.setTextColor(WHITE,BLACK);
display.setCursor(40,10);
display.print (battv);
display.println("V");
delay(250);
//display.clearDisplay();
Percent();
display.setTextSize(2);
display.setTextColor(WHITE,BLACK);
display.setCursor(25,45);
display.print (battv);
display.println("%");
delay(250);
//display.clearDisplay();
}
void checkbatt()
{
int val = analogRead(batteryPin); // read the value from the A0 battery monitoring pin with voltage divider
battv = (val/1023.0) * referenceVolts; // Full 1023 with no voltage divider
// divide val by (1023/2)because the resistors divide the voltage in half, be sure to validate the value of the resistors. The example uses two
//exact 10k ohm resistors. But if your value is off, the equation changes. (511)for exact 10k.
}
void Percent()
{
int val = analogRead(batteryPin); // reads the value from A0
battv = (val/1023)*100; // Divids val by 1023 to get a decimal, then multiplies it by 100 to get a percentage.
if (battv == 100);
}