#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
//#if (SSD1306_LCDHEIGHT != 64)
//#error("Height incorrect, please fix Adafruit_SSD1306.h!");
//#endif
//int p1 = random(0,100);
int p2 = random(0,100);
int p3 = random(0,100);
volatile int counter = 0;
volatile bool int_flag = false;
void setup() {
// put your setup code here, to run once:
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
Serial.begin(9600);
pinMode(2, INPUT);
attachInterrupt(0, buttonTick, CHANGE);
}
void loop() {
// Clear the buffer.
display.clearDisplay();
int val = analogRead(A0); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 100);
drawPercentbar( 0, 0, 50, 15,counter);
drawPercentbar( 0, 20, 100, 15,val);
drawPercentbar( 0, 40, 128, 20,val);
display.display();
/*p1++;p2++;p3++;
if( p1 > 100) p1 =0;
if( p2 > 100) p2 =0;
if( p3 > 100) p3 =0; */
delay(100);
if (int_flag == true) {
Serial.print("btn pressed, №");
Serial.println(counter);
int_flag = false;
}
}
void drawPercentbar(int x,int y, int width,int height, int progress)
{
progress = progress > 100 ? 100 : progress;
progress = progress < 0 ? 0 :progress;
float bar = ((float)(width-4) / 100) * progress;
display.drawRect(x, y, width, height, WHITE);
display.fillRect(x+2, y+2, bar , height-4, WHITE);
// Display progress text
if( height >= 15){
display.setCursor((width/2) - 3, y + 5);
display.setTextSize(1);
display.setTextColor(WHITE);
if( progress >=50)
display.setTextColor(BLACK, WHITE); // 'inverted' text
display.print(progress);
display.print("%");
}
}
volatile uint32_t debounce;
void buttonTick() {
// оставим 100 мс таймаут на гашение дребезга
// CHANGE не предоставляет состояние пина,
// придётся узнать его при помощи digitalRead
if (millis() - debounce >= 2000 && digitalRead(2)) {
debounce = millis();
counter ++;
int_flag = true;
}
}