#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.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 rotatedCounterStaerke = 1; // at beginning, between 0.00 and 1.42
uint16_t rotatedCounterZeit = 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[]
bool buttoneread = false;
bool buttoneon = false;
unsigned long buttonemillis = 0;
float distance = 2.78; //distance at beginning
float dose;
/*
+++++++++++++++++++++++++++++++++++++++
Setup
+++++++++++++++++++++++++++++++++++++++
*/
void setup() {
pinMode (inputCLKEncoder,INPUT);
pinMode (inputDTEncoder,INPUT);
pinMode (inputSWEncoder,INPUT);
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
previousStateCLK = digitalRead(inputCLKEncoder);
startDisplay();
}
/*
+++++++++++++++++++++++++++++++++++++++
Loop
+++++++++++++++++++++++++++++++++++++++
*/
void loop() {
currentStateCLK = digitalRead(inputCLKEncoder);
buttoneread = digitalRead(inputSWEncoder);
if (buttoneread == true){
buttonemillis = millis(); // millis intern counter
}
if(buttoneread == false && buttoneon == false && millis()-buttonemillis > 5){
buttoneon = true;
}
if(buttoneread == true && buttoneon == true){
buttoneon = false;
validateValue(i);
i++;
}
switch (pressedCounter[i]){
case 9:
if (currentStateCLK != previousStateCLK){
if (digitalRead(inputDTEncoder)!=currentStateCLK && rotatedCounterStaerke < 1.41){
rotatedCounterStaerke = rotatedCounterStaerke+0.1;
encdir = "CW"; // Encoder rotated counterclockwise //löschbar
} else if (rotatedCounterStaerke > 0){
rotatedCounterStaerke = rotatedCounterStaerke-0.1; //
encdir = "CCW"; // Encoder rotated counterclockwise //löschbar
}
setValue(i);
oled.print(rotatedCounterStaerke,1);
getDistance(rotatedCounterStaerke);
oled.display();
Serial.println(rotatedCounterStaerke); //löschbar
Serial.println(encdir); //löschbar
}
break;
case 25:
if (currentStateCLK != previousStateCLK){
if (digitalRead(inputDTEncoder)!=currentStateCLK && rotatedCounterZeit < 999){
rotatedCounterZeit = rotatedCounterZeit+1;
encdir = "CW"; // Encoder rotated counterclockwise //löschbar
} else if (rotatedCounterZeit > 0){
rotatedCounterZeit = rotatedCounterZeit-1; //
encdir = "CCW"; // Encoder rotated counterclockwise //löschbar
}
setValue(i);
oled.print(rotatedCounterZeit);
getDose(rotatedCounterStaerke,rotatedCounterZeit);
oled.display();
Serial.println(rotatedCounterZeit); //löschbar
Serial.println(encdir); //löschbar
}
break;
case 54:
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;
}
}
/*
+++++++++++++++++++++++++++++++++++++++
Funktionen
+++++++++++++++++++++++++++++++++++++++
*/
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(rotatedCounterStaerke,1);
oled.display(); // show on OLED
}
void setValue(uint8_t i){
Serial.println(i);
Serial.println(pressedCounter[i]);
oled.setCursor(0,pressedCounter[i]);
oled.writeFillRect(0,pressedCounter[i],25,7,WHITE); // delete previous value
oled.setTextColor(BLACK);
}
void validateValue(uint8_t i){
if (pressedCounter[i]==9 || pressedCounter[i]==25){
oled.setCursor(0,pressedCounter[i]);
oled.writeFillRect(0,pressedCounter[i],25,7,BLACK); // delete previous value
oled.setTextColor(WHITE);
switch (pressedCounter[i]){
case 9:
oled.writeFillRect(0,pressedCounter[i+1],25,7,WHITE);
oled.println(rotatedCounterStaerke);
break;
case 25:
oled.println(rotatedCounterZeit);
break;
}
oled.display();
}
}
void getDistance(float rotatedCounterStaerke){ // 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-rotatedCounterStaerke*1000)))/(2*5.552);
Serial.println(distance); //löschbar
oled.setCursor(0,82);
oled.setTextColor(WHITE);
oled.writeFillRect(0,82,25,8,BLACK); // delete previous value
oled.print(distance);
}
void getDose(float rotatedCounterStaerke, uint16_t rotatedCounterZeit){
dose = rotatedCounterStaerke*rotatedCounterZeit;
Serial.println(dose); //löschbar
oled.setCursor(0,100);
oled.setTextColor(WHITE);
oled.writeFillRect(0,100,25,8,BLACK); // delete previous value
oled.print(dose);
}