ESP8266 throttle with oled code

#include <Arduino.h>
#include <U8x8lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif

U8X8_SH1106_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);

const int analogInPin = A0; // ESP8266 Analog Pin ADC0 = A0

int sensorValue = 0; // value read from the pot
int outputValue = 0; // value to output to a PWM pin
int analogInput = A0;
float Vout = 0.00;
float Vin = 0.00;
float R1 = 10000.00; // resistance of R1 (10K)
float R2 = 1000.00; // resistance of R2 (1K)
int val = 0;

void setup() {
u8x8.begin();
// initialize serial communication at 115200
Serial.begin(115200);
}

void loop() {
// read the analog in value
sensorValue = analogRead(analogInPin);

val = analogRead(analogInput);//reads the analog input
Vout = (val * 5.00) / 1024.00; // formula for calculating voltage out i.e. V+, here 5.00
Vin = Vout / (R2/(R1+R2)); // formula for calculating voltage in i.e. GND

Serial.print(“Voltage = “);
Serial.println(Vin);
float currentValue = Vin;

// map it to the range of the PWM out
// outputValue = map(sensorValue, 0, 1024, 0, 255);
outputValue = map(sensorValue, 279, 868, 0, 255);
char buf[20];
sprintf(buf, “In:%d”, sensorValue);
// print the readings in the Serial Monitor
Serial.print(“sensor = “);
Serial.print(sensorValue);
Serial.print(“\t output = “);
Serial.println(outputValue);
u8x8.setFont(u8x8_font_px437wyse700b_2x2_r); // Version 2.25.x
u8x8.drawString(0, 0, buf);
sprintf(buf, “Out:%d”, outputValue);
u8x8.drawString(0, 3, buf);
u8x8.drawString(0, 6, “Naujas”);
delay(1000);
u8x8.clear();
u8x8.drawString(0, 0, “Voltage:”);
u8x8.drawString(0, 3, “50.4V”);
delay(1000);
u8x8.clear();
u8x8.drawString(0, 0, “Speed:”);
u8x8.drawString(0, 3, “25km/h”);
delay(1000);
u8x8.clear();

}

Leave a Reply