int potVal;
float volt;
int ledNr;
#include <Adafruit_NeoPixel.h>
#include "U8glib.h"
U8GLIB_SSD1306_128X64 oled(U8G_I2C_OPT_NONE);
const byte neoPin = 10;
const byte neoPixels = 24;
byte neoBright = 100;
Adafruit_NeoPixel ring = Adafruit_NeoPixel(neoPixels, neoPin, NEO_GRB);
void setup() {
Serial.begin(9600);
ring.begin();
ring.setBrightness(neoBright);
ring.show();
}
void loop() {
potVal=analogRead(A0);
volt=potVal*5.0/1023;
ledNr=map(potVal, 0, 1023, 0, 24);
Serial.print(potVal);
for(int i = 0; i < ledNr; i++){
ring.setPixelColor(i, ring.Color(0,255,145));
ring.show();
}
for(int i = ledNr; i < neoPixels; i++){
ring.setPixelColor(i, ring.Color(255,0,0));
ring.show();
}
oled.firstPage();
do {
draw();
}
while(oled.nextPage());
}
void draw(void) {
oled.setFont(u8g_font_helvR08);
int X_koord=map(potVal, 0, 1023, 0, 115);
//Draw frame
oled.drawFrame(12,45,115,17);
//Draw box
oled.drawBox(12,45,X_koord, 17);
//skapa en sträng för plats för mist 3 tecken där Volt ska sparas
char buf1[5];
//gör om variabelvärdet Volt (flyttal, float) till 3 tecken med 2 decimaler och spara i buf1
dtostrf (volt, 3, 2, buf1);
char buf2[50];
sprintf(buf2,"PotVal %u - %s V", potVal, buf1);
oled.drawStr(0,12,buf2);
}