#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <Wire.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int hysteresis = 1;
unsigned int ampMeasurement = 0;
unsigned int voltMeasurement = 0;
int ampOffset = 0;
unsigned int ampGoal = 0;
unsigned int ampBaseSetting = 40;
unsigned int pwm = 0;
void setup()
{
Serial.begin(115200);
Serial.println("started generator controller");
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
// SSD1306_EXTERNALVCC for 5V power
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
}
void loop()
{
//get measurements and user input
ampMeasurement = map(analogRead(A0), 0, 1023, 0, 100);
ampOffset = map(analogRead(A1), 0, 1023, -40, 40);
voltMeasurement = map(analogRead(A2), 0, 1023, 20, 32);
//control logic
ampGoal = ampBaseSetting + ampOffset;
if (ampMeasurement < ampGoal) {
pwm = pwm +1;
}
else if (ampMeasurement > ampGoal) {
pwm = pwm -1;
}
//display parameters
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner
display.println("DisOtto Generator");
display.setTextSize(2);
display.setCursor(0,10);
display.println(ampMeasurement);
display.setCursor(80, 10);
display.println("A");
display.setCursor(0,30);
display.println(voltMeasurement);
display.setCursor(80, 30);
display.println("V");
display.setTextSize(1);
display.setCursor(0,48);
display.println(pwm);
display.setCursor(0,56);
display.println("Offset: ");
display.setCursor(60,56);
display.println(ampOffset);
display.display();
/*
lastMillis = millis();
*/
}