#include <Arduino.h>
#include <U8g2lib.h>
// megszakítás
#define PIN_SENSOR_ECHO (2)
#define PIN_SENSOR_TRIGGER (7)
// gombok
#define DOWN_BUTTON (10)
#define UP_BUTTON (11)
#define POWER_BUTTON (12)
int DOWN_BUTTON_OLDSTATE ; //MENU DOWN Button
int UP_BUTTON_OLDSTATE ; // MENU UP BUtton
int POWER_BUTTON_OLDSTATE ; // PR POWER BUTTON
int DOWN_BUTTON_STATE ; //MENU DOWN Button
int UP_BUTTON_STATE ; // MENU UP BUtton
int POWER_BUTTON_STATE ; // PR POWER BUTTON
unsigned long(POWER_BUTTON_START) = millis();
unsigned long(UP_BUTTON_START) = millis();
unsigned long(DOWN_BUTTON_START) = millis();
// LEDek
#define DO_LED (3)
// Mérés
void isr_us(void);
unsigned long us_start;
unsigned long us_end;
bool measurementReady = false;
bool us_start_state = false;
bool us_end_state = false;
unsigned long timeDelta;
bool TEST_State = false;
// OLED u8g2
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ A5, /* data=*/ A4, /* reset=*/ U8X8_PIN_NONE);
int16_t offset; // current offset for the scrolling text
u8g2_uint_t width; // pixel width of the scrolling text (must be lesser than 128 unless U8G2_16BIT is defined
const char *text = "Shutter Speed Tester"; // scroll this text from right to left
const uint8_t tile_area_x_pos = 0; // Update area left position (in tiles)
const uint8_t tile_area_y_pos = 0; // Update area upper position (distance from top in tiles)
const uint8_t tile_area_width = 16;
const uint8_t tile_area_height = 2; // this will allow cour18 chars to fit into the area
const u8g2_uint_t pixel_area_x_pos = tile_area_x_pos*8;
const u8g2_uint_t pixel_area_y_pos = tile_area_y_pos*8;
const u8g2_uint_t pixel_area_width = tile_area_width*8;
const u8g2_uint_t pixel_area_height = tile_area_height*8;
const uint8_t tile_area_x_pos2 = 0; // Update area left position (in tiles)
const uint8_t tile_area_y_pos2 = 2; // Update area upper position (distance from top in tiles)
const uint8_t tile_area_width2 = 16;
const uint8_t tile_area_height2 = 5; // this will allow cour18 chars to fit into the area
const u8g2_uint_t pixel_area_x_pos2 = tile_area_x_pos2*8;
const u8g2_uint_t pixel_area_y_pos2 = tile_area_y_pos2*8;
const u8g2_uint_t pixel_area_width2 = tile_area_width2*8;
const u8g2_uint_t pixel_area_height2 = tile_area_height2*8;
void oled_time_write(float);
void oled_default(void);
// Serial
void serial_default(void);
char buff[7];
//**************************************************************************************************
void setup() {
Serial.begin(9600);
// Init PR Interupt
pinMode(PIN_SENSOR_ECHO, INPUT_PULLUP);
pinMode(PIN_SENSOR_TRIGGER, OUTPUT);
attachInterrupt(digitalPinToInterrupt(PIN_SENSOR_ECHO), isr_us, CHANGE);
// Init BUTTONS
pinMode(DOWN_BUTTON, INPUT_PULLUP);
pinMode(UP_BUTTON, INPUT_PULLUP);
pinMode(POWER_BUTTON, INPUT_PULLUP);
DOWN_BUTTON_OLDSTATE = digitalRead(DOWN_BUTTON); //MENU DOWN Button
UP_BUTTON_OLDSTATE = digitalRead(UP_BUTTON); // MENU UP BUtton
POWER_BUTTON_OLDSTATE = digitalRead(POWER_BUTTON); // PR POWER BUTTON
// Init LED
pinMode(DO_LED, OUTPUT);
// Wokwi-photoresistor-sensor ON
digitalWrite(PIN_SENSOR_TRIGGER, HIGH);
// Oled and Serial default
u8g2.begin();
u8g2.clearBuffer(); // clear the internal memory
//u8g2.setFont(u8g2_font_helvR10_tr); // choose a suitable font
//u8g2.drawStr(0,30,"UpdateDisplayArea"); // write something to the internal memory
// draw a frame, only the content within the frame will be updated
// the frame is never drawn again, but will stay on the display
//u8g2.drawBox(pixel_area_x_pos-1, pixel_area_y_pos-1, pixel_area_width+2, pixel_area_height+2);
//u8g2.drawBox(pixel_area_x_pos2-1, pixel_area_y_pos2-1, pixel_area_width2+2, pixel_area_height2+2);
u8g2.sendBuffer(); // transfer internal memory to the display
u8g2.setFont(u8g2_font_tenthinguys_tr); // set the target font for the text width calculation
width = u8g2.getUTF8Width(text); // calculate the pixel width of the text
offset = width+pixel_area_width;
oled_default();
serial_default();
}
//**************************************************************************************************
void loop() {
// Ha a mérés kész
if (measurementReady and TEST_State == false){
noInterrupts();
measurementReady = false;
timeDelta = (us_end - us_start) /100;
interrupts();
if (timeDelta != 0) {
oled_time_write(timeDelta);
}
}
// POWER BUTTON RED
POWER_BUTTON_STATE = digitalRead(POWER_BUTTON);
if (POWER_BUTTON_STATE != POWER_BUTTON_OLDSTATE and millis() >= POWER_BUTTON_START + 5) {
POWER_BUTTON_OLDSTATE = POWER_BUTTON_STATE;
if (POWER_BUTTON_STATE == HIGH) {
// RELEASED
POWER_BUTTON_START = millis();
}
if (POWER_BUTTON_STATE == LOW) {
// PRESSED
TEST_State = false;
measurementReady = false;
POWER_BUTTON_START = millis();
int triggerState = !digitalRead(PIN_SENSOR_TRIGGER);
digitalWrite(PIN_SENSOR_TRIGGER, triggerState);
oled_default();
//oled.setFont( Cooper19);
//oled.setCursor(0, 4);
//oled.clearToEOL();
//oled.print("READY: ");
if (triggerState) {
//oled.print("ON");
serial_default();
}
else {
//oled.print("OFF");
}
}
}
// UP BUTTON BLUE
UP_BUTTON_STATE = digitalRead(UP_BUTTON);
if (UP_BUTTON_STATE != UP_BUTTON_OLDSTATE and millis() >= UP_BUTTON_START + 5) {
UP_BUTTON_OLDSTATE = UP_BUTTON_STATE;
if (UP_BUTTON_STATE == HIGH) {
// RELEASED
UP_BUTTON_START = millis();
TEST_State = !TEST_State;
measurementReady = false;
oled_default();
if (TEST_State == false) {
serial_default();
}
}
if (UP_BUTTON_STATE == LOW) {
// PRESSED
UP_BUTTON_START = millis();
}
}
if (TEST_State == true){
int AO = 1024 - analogRead(7);
//oled.setCursor(0, 3);
//oled.print("LIGHTE : ");
//oled.print(AO);
//oled.print(" ");
//oled.setCursor(0, 5);
for ( int i = 0; i <= 13; i++){
if (i <= AO/74) {
//oled.print("#");
}
else {
//oled.print(" ");
}
}
}
//**************************************************************************************************
// Szöveg görgtés
u8g2.clearBuffer(); // clear the complete internal memory
// draw the scrolling text at current offset
u8g2.setFont(u8g2_font_tenthinguys_tr); // set the target font
u8g2.drawUTF8(pixel_area_x_pos-width+offset,pixel_area_y_pos+pixel_area_height+u8g2.getDescent()-1, text); // draw the scolling text
// now only update the selected area, the rest of the display content is not changed
u8g2.updateDisplayArea(tile_area_x_pos, tile_area_y_pos, tile_area_width, tile_area_height);
offset--; // scroll by one pixel
if ( offset == 0 )
offset = width+pixel_area_width; // start over again
//**************************************************************************************************
} //LOOP end
//**************************************************************************************************
void isr_us(void){
bool ECHO = digitalRead(PIN_SENSOR_ECHO);
digitalWrite(DO_LED, !ECHO);
if (measurementReady == false) {
if (ECHO == LOW and us_start_state == false){
us_start = micros();
us_start_state = true;
us_end_state = false;
digitalWrite(DO_LED, HIGH);
} else if (ECHO == HIGH and us_end_state == false){
measurementReady = true;
us_end = micros();
us_end_state = true;
us_start_state = false;
}
}
}
//**************************************************************************************************
void oled_time_write(float timeDelta){
int ezer = 0;
float time =0.000;
//Átszámítás
if (timeDelta > 10000) {
ezer = (timeDelta / 10000 );
timeDelta = timeDelta - (ezer * 10000) ;
time = 1 / float (timeDelta /10000);
}
else if (timeDelta > 1000){
time = 1 / float (timeDelta /10000);
}
else if (timeDelta >= 10){
time = 10000 * (1 / timeDelta);
}
float t = (ezer* 10000 + timeDelta) /10;
// Kiírás oledre
u8g2.clearBuffer(); // clear the complete internal memory
u8g2.setFont(u8g2_font_tenthinguys_tr); // set the target font
u8g2.drawUTF8(0,24, "Time:");
snprintf (buff, sizeof(buff), "%f", t);
u8g2.drawUTF8(40,24, buff); // draw the scolling text
// now only update the selected area, the rest of the display content is not changed
u8g2.updateDisplayArea(tile_area_x_pos2, tile_area_y_pos2, tile_area_width2, tile_area_height2);
//oled.setFont(Arial14);
//oled.setCursor(0, 3);
//oled.clearToEOL();
//oled.print("Time: ");
//oled.print(t,1);
//oled.print(" ms");
//oled.setFont(Cooper19);
//oled.setCursor(0, 5);
//oled.clearToEOL();
if (ezer >0) {
//oled.print(ezer);
//oled.print(" + 1/");
}
else {
//oled.print(" 1/");
}
//oled.print(time, 1);
//oled.setFont(Arial14);
//oled.println(" sec");
//kiírás serila monitorra
Serial.print("\t");
Serial.print(t, 1);
Serial.print("\t");
Serial.print("\t");
Serial.print(ezer);
Serial.print("\t");
Serial.println(time);
}
//**************************************************************************************************
void oled_default(void){
//oled.setFont(Arial14);
//oled.clear();
//oled.print("Shutter SPEED Tester");
}
//**************************************************************************************************
void serial_default(){
Serial.print("Time: ");
Serial.print("\t");
Serial.print("ms");
Serial.print("\t");
Serial.print("Time: " );
Serial.print("\t");
Serial.print("sec" );
Serial.print("\t");
Serial.println("+1/sec");
}