#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using I2C
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 12; // the number of the LED pin
const int SENSOR_PIN = A1;
const int NUM_AVG = 1000; //many ADC reads + average = lower noise
const float RESISTOR_VALUE = 150.7;
const float ANALOG_REFERENCE_VALUE = 3.375; //Analog reference, which is also the full scale voltage of ADC in this case
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
//----------------------------------------
long Timeout1 = 0; // variable for the shutdown timer of the display
long refreshTimer = 0; // variable for the messuring time
//----------------------------------------
void setup() { // put your setup code here, to run once:
Serial.begin(9600);
analogReference(EXTERNAL); //use external 3.3V reference
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output
pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input
// initialize the OLED object
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);} // Don't proceed, loop forever
}
//-----------------------------------------
void display_sensor_value() {
display.clearDisplay();
long int adc_reading = 0;
for (int i = 0; i<NUM_AVG; i++) {adc_reading += analogRead(SENSOR_PIN);}
float voltage_value = ((float)adc_reading/NUM_AVG + 0.5 )/1024.0*ANALOG_REFERENCE_VALUE; // +0.5 is to change from round down to round to nearest for ADCs
float current_value_mA = voltage_value / RESISTOR_VALUE * 1000.0;
float percentage_value = current_value_mA*6.25 - 25.0;
float Temp_value = percentage_value / 100 * 500;
//from Lopaka to display everything
static const unsigned char PROGMEM image_Battery_16x16_bits[] = {0x00,0x00,0x00,0x00,0x01,0x80,0x03,0xc0,0x04,0x20,0x04,0x20,0x05,0xa0,0x04,0x20,0x05,0xa0,0x04,0x20,0x05,0xa0,0x04,0x20,0x03,0xc0,0x00,0x00,0x00,0x00,0x00,0x00};
display.drawBitmap( 117, -2, image_Battery_16x16_bits, 16, 16, WHITE);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(5, 10);
display.print(String("Voltage: ") + String(voltage_value,3) + " V");
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(5, 25);
display.print(String("Current: ") + String(current_value_mA, 3) + " mA");
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(5, 40);
display.print(String("Percentage: ") + String(percentage_value,2) + '%');
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(5, 55);
display.print(String("Temperature: ") + String(Temp_value,1) + 'C');
if (millis() > refreshTimer) {
refreshTimer = (millis() + 1000); // time how fast the display is showing a new messure
display.display();
display.clearDisplay();}
}
//-----------------------------------------
void button_led() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is LOW.
if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
//-----------------------------------------
void loop() { // main code here, to run repeated
button_led();
if (buttonState == LOW) { // if the button is pushed
display_sensor_value(); // jumps in the void to display the sensor value
Timeout1 = (millis() + 5000); // time when the display shut´s down after messuring
}
else if (millis() > Timeout1) {
display.clearDisplay();
display.display();}
}