#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define T A6
#define E 5
int V;
float C;
void setup() {
pinMode(T, INPUT);
pinMode(E, OUTPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
Serial.begin(9600);
}
void loop() {
V = map(analogRead(T), 0, 1023, 0, 255);
analogWrite(E, V);
C = map(V, 0, 255, 0, 500);
C = C / 100;
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("valeur =");
display.println(analogRead(T));
decimalTobinary(analogRead(T));
display.print("tension =");
display.print(C);
display.display();
delay(200);
}
void decimalTobinary(int decimal) {
String binary = "";
for (int i = 0; i < 10; i++) {
int tmp = decimal % 2;
if (tmp == 1) {
binary = "1" + binary;
} else {
binary = "0" + binary;
}
decimal = floor(decimal / 2);
}
Serial.println(binary);
}