Braulio777

Arduino OLED Voltmeter

Jan 5th, 2017
833
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Arduino OLED Voltmeter
  2. #include <Wire.h>
  3. #include "U8glib.h"
  4. U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);            
  5.  
  6. int analogInput = 0;
  7. float vout = 0.0;
  8. float vin = 0.0;
  9. float R1 = 100000.0;
  10. float R2 = 10000.0;
  11. int value = 0;
  12. void draw(void)
  13. {
  14.   u8g.setFont(u8g_font_profont17r);        
  15.   u8g.drawStr(18, 12, "VOLTAGE");
  16.   u8g.setPrintPos(33,40);
  17.   u8g.drawRFrame(15, 20, 100, 30, 10);    
  18.   u8g.println(vin);                        
  19.   u8g.println("V");
  20. }
  21. void setup(){
  22.    pinMode(analogInput, INPUT);
  23. }
  24. void loop(){
  25.    value = analogRead(analogInput);
  26.    vout = (value * 5.0) / 1024.0;
  27.    vin = vout / (R2/(R1+R2));
  28.    if (vin<0.09) {
  29.    vin=0.0;
  30. }
  31.   u8g.firstPage();  
  32.   do
  33.     {
  34.      draw();      
  35.     }
  36.   while( u8g.nextPage() );
  37. delay(500);
  38. }
RAW Paste Data