#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneButton.h>
/*
+++++++++++++++++++++++++++++++++++++++
Display (OLED 128x64)
fontsize 6x8 (default)
+++++++++++++++++++++++++++++++++++++++
*/
#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);
/*
+++++++++++++++++++++++++++++++++++++++
Encoder
+++++++++++++++++++++++++++++++++++++++
*/
#define inputCLKEncoder 19
#define inputDTEncoder 18
#define inputSWEncoder 17
float rotatedCounterE = 1; // at beginning, between 0.00 and 1.42
uint16_t rotatedCountert = 0; // at beginning, between 0 and 999
uint8_t currentStateCLK;
uint8_t previousStateCLK;
String encdir = ""; //löschbar
uint8_t pressedCounter[] = {9,25,54,63,115}; //y-Coordinate to write in
uint8_t i = 0; //counter to go through pressedCounter[]
float distance = 2.78; //distance at beginning
float dose;
OneButton button(inputSWEncoder,true);
/*
+++++++++++++++++++++++++++++++++++++++
Setup
+++++++++++++++++++++++++++++++++++++++
*/
void setup() {
pinMode (inputCLKEncoder,INPUT);
pinMode (inputDTEncoder,INPUT);
button.attachClick(validateValue);
button.attachDoubleClick(goBackValue);
button.setDebounceMs(20);
Serial.begin(9600); // Serial display //löschbar
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed")); //löschbar
while (true);
}
delay(2000); // wait for initializing
startDisplay();
previousStateCLK = digitalRead(inputCLKEncoder);
}
/*
+++++++++++++++++++++++++++++++++++++++
Loop
+++++++++++++++++++++++++++++++++++++++
*/
void loop() {
unsigned long now=millis();
button.tick();
currentStateCLK = digitalRead(inputCLKEncoder);
setValue();
}
void startDisplay(){
oled.clearDisplay(); // clear display
oled.setTextSize(1); // text size, minimum 1
oled.setTextColor(WHITE); // text color
oled.setRotation(3); // rotate text (270°)
oled.setCursor(0, 0); // position to display
oled.println("E ="); // text to display
oled.setCursor(27,9);
oled.println("mW/cm2"); // line 2
oled.println("t ="); // line 3
oled.setCursor(27,25);
oled.println("s"); // line 4
oled.println("Shutter- position:"); // line 5 + 6
oled.println("l[ ], r[ ]"); // line 7
oled.println("m[X], n[ ]"); // line 8
oled.drawLine(0,70,64,70,WHITE); // seperate input and output
oled.setCursor(0,73);
oled.println("Distance ="); // line 9
oled.print(distance);
oled.setCursor(27,82);
oled.println("cm"); // line 10
oled.println("Dose ="); // line 11
oled.setCursor(27,100);
oled.println("mJ/cm2"); // line 12
oled.setCursor(11,115);
oled.println("[Start]"); //line 13
oled.setCursor(0,9);
oled.writeFillRect(0,9,25,7,WHITE);
oled.setTextColor(BLACK);
oled.print(rotatedCounterE,1);
oled.display(); // show on OLED
}
void displayValue(uint8_t i){
oled.setCursor(0,pressedCounter[i]);
oled.writeFillRect(0,pressedCounter[i],25,7,WHITE); // delete previous value
oled.setTextColor(BLACK);
}
void deleteValue(uint8_t i){
oled.setCursor(0,pressedCounter[i]);
oled.writeFillRect(0,pressedCounter[i],25,7,BLACK); // delete previous value
oled.setTextColor(WHITE);
}
void validateValue(){
if (pressedCounter[i] == 9 || pressedCounter[i] == 25){
deleteValue(i);
switch (pressedCounter[i]){
case 9:
oled.println(rotatedCounterE,1);
displayValue(i+1);
oled.println(rotatedCountert);
break;
case 25:
oled.println(rotatedCountert);
break;
}
oled.display();
}
if (i<4){
i++;
}
Serial.println(i);
Serial.println("forward");
}
void goBackValue(){
if (pressedCounter[i] == 9 || pressedCounter[i] == 25){
deleteValue(i);
switch (pressedCounter[i]){
case 9:
oled.println(rotatedCounterE,1);
break;
case 25:
oled.writeFillRect(0,pressedCounter[i-1],25,7,WHITE);
oled.println(rotatedCountert);
displayValue(i-1);
oled.print(rotatedCounterE,1);
oled.display();
break;
}
oled.display();
}
if (i>0){
i--;
}
Serial.println(i);
Serial.println("backward");
}
void getDistance(float rotatedCounterE){ // höchste Bestrahlungsstärke 1.42 mW/cm2 mit Abstand 0.04
distance = (169.01-sqrt(pow(169.01,2)-4*5.5552*(1426.7-rotatedCounterE*1000)))/(2*5.552);
oled.setCursor(0,82);
oled.setTextColor(WHITE);
oled.writeFillRect(0,82,25,8,BLACK); // delete previous value
oled.print(distance);
}
void getDose(float rotatedCounterE, uint16_t rotatedCountert){
dose = rotatedCounterE*rotatedCountert;
oled.setCursor(0,100);
oled.setTextColor(WHITE);
oled.writeFillRect(0,100,25,8,BLACK); // delete previous value
oled.print(dose);
}
void setValue(){
switch (pressedCounter[i]){
case 9:
if (currentStateCLK != previousStateCLK){
if (digitalRead(inputDTEncoder)!=currentStateCLK && rotatedCounterE < 1.4){
rotatedCounterE = rotatedCounterE+0.1;
encdir = "CW"; // Encoder rotated counterclockwise //löschbar
} else if (rotatedCounterE > 0 && rotatedCounterE != 1.4){
rotatedCounterE = rotatedCounterE-0.1; //
encdir = "CCW"; // Encoder rotated counterclockwise //löschbar
}
getDistance(rotatedCounterE);
displayValue(i);
oled.print(rotatedCounterE,1);
oled.display();
Serial.println(encdir); //löschbar
}
break;
case 25:
if (currentStateCLK != previousStateCLK){
if (digitalRead(inputDTEncoder)!=currentStateCLK && rotatedCountert < 999){
rotatedCountert = rotatedCountert+1;
encdir = "CW"; // Encoder rotated counterclockwise //löschbar
} else if (rotatedCountert > 0){
rotatedCountert = rotatedCountert-1; //
encdir = "CCW"; // Encoder rotated counterclockwise //löschbar
}
getDose(rotatedCounterE,rotatedCountert);
displayValue(i);
oled.print(rotatedCountert);
oled.display();
Serial.println(encdir); //löschbar
}
break;
/*
case 54:
oled.writeFillRect(11,57,8,7,WHITE);
oled.setCursor(12,57);
oled.setTextColor(BLACK);
oled.print("X");
oled.display();
break;
case 63:
oled.display();
break;
case 115:
oled.writeFillRect(11,115,40,7,WHITE);
oled.setTextColor(BLACK);
oled.setCursor(11,115);
oled.println("[Start]"); //line 14
oled.display();
break;
*/
}
}