#include <GyverTimers.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
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
//unsigned long startMillis; //some global variables available anywhere in the program
//unsigned long currentMillis;
//const unsigned long period = 1000; //the value is a number of milliseconds
float vbus = 0.0;
float current = 0.0;
float as = 0.0;
float ah = 0.0;
float ws = 0.0;
float wh = 0.0;
int upbutton = HIGH;
int downbutton;
int leftbutton;
int rightbutton;
int exebutton;
int vertical = 0;
int horizontal = 0;
String name = "Trigger";
String version = " v1";
void setup()
{
Serial.begin(9600);
Timer1.setFrequency(10);
Timer1.enableISR();
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
oled.clearDisplay();
//startMillis = millis();
pinMode(13, INPUT_PULLUP);//UP
pinMode(12, INPUT_PULLUP);//DOWN
pinMode(11, INPUT_PULLUP);//LEFT
pinMode(10, INPUT_PULLUP);//RIGHT
pinMode(9, INPUT_PULLUP);//EXECUTE
pinMode(8, OUTPUT);digitalWrite(8, LOW);//POWER ENABLE
delay(500);
pinMode(7, OUTPUT);digitalWrite(7, LOW);//18V
pinMode(6, OUTPUT);digitalWrite(6, LOW);//15V
pinMode(5, OUTPUT);digitalWrite(5, LOW);//12V
pinMode(4, OUTPUT);digitalWrite(4, HIGH);//9V
pinMode(3, OUTPUT);digitalWrite(3, LOW);//5V
//oled.begin(SSD1306_SWITCHCAPVCC, 0x3D); //or 0x3C
}
void loop()
{
upbutton = digitalRead(13);
downbutton = digitalRead(12);
leftbutton = digitalRead(11);
rightbutton = digitalRead(10);
exebutton = digitalRead(9);
optionselect();
execution();
//displaypage();
}
void optionselect(){
if(upbutton == LOW){
delay(200);
vertical--;
if(vertical < 0) vertical = 4;
}
if(downbutton == LOW){
delay(200);
vertical++;
if(vertical > 4) vertical = 0;
}
if(leftbutton == LOW){
delay(200);
horizontal--;
if(horizontal < 0) horizontal = -1;
}
if(rightbutton == LOW){
delay(200);
horizontal++;
if(horizontal > 1) horizontal = 1;
}
displaypage();
}
void execution(){
if(exebutton == LOW){
//digitalWrite(8, LOW);
displaypage();
delay(200);
if(vertical == 0){
digitalWrite(7, LOW);//18V
digitalWrite(6, LOW);//15V
digitalWrite(5, LOW);//12V
digitalWrite(4, HIGH);//9V Enable
digitalWrite(3, LOW);//5V
}
else if(vertical == 1){
digitalWrite(7, LOW);//18V
digitalWrite(6, LOW);//15V
digitalWrite(5, HIGH);//12V Enable
digitalWrite(4, LOW);//9V
digitalWrite(3, LOW);//5V
}
else if(vertical == 2){
digitalWrite(7, LOW);//18V
digitalWrite(6, HIGH);//15V Enable
digitalWrite(5, LOW);//12V
digitalWrite(4, LOW);//9V
digitalWrite(3, LOW);//5V
}
else if(vertical == 3){
digitalWrite(7, HIGH);//18V Enable
digitalWrite(6, LOW);//15V
digitalWrite(5, LOW);//12V
digitalWrite(4, LOW);//9V
digitalWrite(3, LOW);//5V
}
else if(vertical == 4){
//20V Enable
digitalWrite(7, LOW);//18V
digitalWrite(6, LOW);//15V
digitalWrite(5, LOW);//12V
digitalWrite(4, LOW);//9V
digitalWrite(3, LOW);//5V
}
displaypage();
}
displaypage();
}
void displaypage(){
if(horizontal == 0) mainpage();
else if(horizontal == 1) meterpage();
else if(horizontal == -1) manualpage();
}
void manualpage(){
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE, BLACK);
oled.setCursor(0, 0);
oled.println("INSTRUCTION v1.0");
printline();
nextline(0, 10);
oled.print("1.Left/Right: Display");
nextline(0, 10);
oled.print("2.Up/Down : Voltage");
nextline(0, 10);
oled.print("3.Centre : Confirm");
nextline(0, 15);
oled.print("Designed by Sober Lam");
oled.display();
}
void mainpage(){
oled.clearDisplay();
//delay(200);
oled.setTextSize(1);
oled.setTextColor(WHITE, BLACK);
oled.setCursor(0, 0);
//if(vertical == 0) arrow();
//oled.print(pdv,3);
displayreading();
oled.setTextSize(4);
if(vertical == 0) oledDisplayCenter("9 V");
else if(vertical == 1) oledDisplayCenter("12 V");
else if(vertical == 2) oledDisplayCenter("15 V");
else if(vertical == 3) oledDisplayCenter("18 V");
else if(vertical == 4) oledDisplayCenter("20 V");
oled.display();
}
void meterpage(){
oled.clearDisplay();
oled.setTextSize(3);
oled.setTextColor(WHITE, BLACK);
oled.setCursor(8, 8);
if(vbus < 10){
String v1 = "";
v1.concat(" ");
v1.concat(vbus);
oled.print(v1);
}
else oled.print(vbus);
oled.setCursor(103, oled.getCursorY());
oled.print("V");
oled.setCursor(8, 33);
String displayc = "";
displayc.concat(" ");
displayc.concat(current);
oled.print(displayc);
oled.setCursor(103, oled.getCursorY());
oled.print("A");
oled.display();
}
/*void clearOLEDLine(int row){
for (int y=row; y<=row+6; y++){
for (int x=0; x<127; x++){
oled.drawPixel(x, y, BLACK);
}
}
//lcd.setCursor(0,line);
}*/
void displayreading(){
oled.setTextSize(1);
//oled.setCursor(80, 10);
if(vbus < 1) oled.print("DISCONNECTED");
else oled.print("CONNECTED");
oled.setCursor(85, 0);
if(vbus < 10){
String v1 = "";
v1.concat(" ");
v1.concat(vbus);
oled.print(v1);
}
else oled.print(vbus);
oled.setCursor(117, oled.getCursorY());
oled.print("V");
/*nextline(80, 10);
String displayc1 = "";
displayc1.concat(" ");
displayc1.concat(current);
oled.print(displayc1);
oled.setCursor(115, oled.getCursorY());
oled.print("A");*/
}
void printline(){
for (int y = oled.getCursorY(); y<= oled.getCursorY(); y++){
for (int x=0; x<127; x++){
oled.drawPixel(x, y, WHITE);
}
}
}
void nextline(int x, int yoffset){
oled.setCursor(x, oled.getCursorY()+yoffset);
}
void arrow(){
oled.setCursor(30, oled.getCursorY());
oled.print(" <<-");
//oled.invertDisplay(true);
}
void oledDisplayCenter(String text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
oled.setTextColor(WHITE, BLACK);
//oled.setTextSize(2);
oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
oled.print(text); // text to display
//oled.display();
}
ISR(TIMER1_A) {
// float analogvalue = analogRead(A0);
// float analogvalue1 = analogRead(A1);
// float analogvalue2 = analogRead(A2);
// float analogvalue3 = analogRead(A3);
vbus = ((analogRead(A0) * 5.0) / 1024) / 0.24;// ~24K/99K
vbus = (vbus * 4.8) / 5;// VUSB tune
if(vbus < 4) vbus = 0;
//current = (analogRead(A2) * 5.0) / 1024;
for (int i = 0; i < 10; i++){
current = current + (analogRead(A2) * 5.0) / 1024;;
}
current = (current * 4.8) / 50;
current = (2.5 - current) / 0.49;
if(current < 0.2) current = 0;
else if(current > 2.3) current = 0;
as = current / 36000;
ah = ah + as;
ws = (vbus * current) / 36000;
wh = wh + ws;
if(vbus < 4){
ah = 0;
wh = 0;
}
//pdv = analogvalue / 0.4152; //
//Serial.println(ah,3);
//Serial.println(wh,3);
}