#include <Wire.h> // Include Wire library (required for I2C devices)
#include <Adafruit_GFX.h> // Include Adafruit graphics library
#include <Adafruit_SSD1306.h> // Include Adafruit SSD1306 OLED driver
#include <EEPROM.h>
#define SCREEN_WIDTH 128 // SzerokoÅÄ w pikselach
#define SCREEN_HEIGHT 64 // WysokoÅÄ w pikselach
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define button1 5 // Button B1 is co nnected to Arduino pin 5
#define button2 3 // Button B2 is connected to Arduino pin 3
#define button3 2 // Button B2 is connected to Arduino pin 2
#define button4 4 // Button B2 is connected to Arduino pin 4
#define out4 17 //PC0 -A3
#define out3 16 //PC1 -A2
#define out2 15 //PC2 -A1
#define out1 14 //PC3 -A0
#define pwm_1 8 //PB0
#define pwm_2 9 //PB1
#define pwm_3 10 //PB2
#define pwm_4 11 //PB3
#define outled 13
#define MAX_TASKS 20
#define blink_s_time 200 //400 200 dla symulatora
#define alt_s_time 100 // 200 100 dla symulatora wokwi
byte modeOut[8] = {0,0,0,0,0,0,0,0}; // 0=AUTO, 1=MANUAL
bool manualState[4] = {0,0,0,0}; // dla OUT1–4
byte manualPWM[4] = {0,0,0,0}; // dla OUT5–6 (0–100%)
byte set_m_out1, reset_m_out1, set_m_out2, reset_m_out2, set_m_out3, reset_m_out3, set_m_out4, reset_m_out4;
byte set_h_out1, reset_h_out1, set_h_out2, reset_h_out2, set_h_out3, reset_h_out3, set_h_out4, reset_h_out4;
byte time_menu, menu_nr_old;
byte button_delay;
char Time[] = " : : ";
char Calendar[] = " / /20 ";
char text_text[] = " ";
char Time_hhmm[] = " : ";
char Time_ss[] = " ";
const char* dayShortPL[8] = {
"", "Pn", "Wt", "Sr", "Cz", "Pt", "So", "Nd"
};
byte editMode = 0;
// 0 = podgląd
// 1 = day
// 2 = date
// 3 = month
// 4 = year
// 5 = hour
// 6 = minute
// 7 = potwierdzenie OK?
unsigned long blinkTimer = 0;
unsigned long okTimer = 0;
unsigned long button_up_pressTime = 0;
unsigned long button_up_lastRepeat = 0;
unsigned long button_down_pressTime = 0;
unsigned long button_down_lastRepeat = 0;
bool blinkState = false;
bool altState = false;
const byte dayWidth = 18; // "Pn" = 12px + 6px spacja
byte button_ok;
byte button_back;
byte button_up;
byte button_down;
byte i, second, minute, hour, day, date, month, year;
byte menu_nr = 1;
byte button_back_ok;
byte button_up_down;
struct Task {
byte enable;
byte type_day; // na razie tylko liczba
byte hourOn;
byte minuteOn;
byte hourOff;
byte minuteOff;
byte output;
byte mode;
byte out_pwm;
byte ramp_on;
byte ramp_off;
};
Task task;
byte task_durH;
byte task_durM;
byte currentTask = 0;
volatile bool out_1 = false;
volatile bool out_2 = false;
volatile bool out_3 = false;
volatile bool out_4 = false;
volatile bool button_back_raw; //d la przerwania
volatile bool button_up_raw;
volatile bool button_down_raw;
volatile bool button_ok_raw;
bool button_back_state;
bool button_up_state;
bool button_down_state;
bool button_ok_state;
bool button_back_old;
bool button_up_old;
bool button_down_old;
bool button_ok_old;
bool button_up_long = false;
bool button_down_long = false;
unsigned long up_press_time = 0;
unsigned long down_press_time = 0;
const uint16_t LONG_PRESS_TIME = 500; // 0.5 sekundy
byte LED_PWM_1=0;
byte LED_PWM_2=0;
byte LED_PWM_3=0;
byte LED_PWM_4=0;
void setup(void) {
Serial.begin(2000000); // Inicjalizacja portu szeregowego
//Serial.println(" ");
noInterrupts();
TCCR2A = 0; // Resetowanie rejestru kontrolnego A
TCCR2B = 0; // Resetowanie rejestru kontrolnego B
TCNT2 = 0; // Inicjalizacja licznika na 0
TCCR2A |= (1 << WGM21);
OCR2A = 155; //10ms
TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20);
TIMSK2 |= (1 << OCIE2A);
interrupts(); // wlacz globalne przerwania
TCCR1A = 0;
TCCR1B = (1 << WGM12) | (1 << CS10);
OCR1A = 799; // 50 µs TIMSK1 |= (1 << OCIE1A);
LED_PWM_1 = 0;
LED_PWM_2 = 0;
LED_PWM_3 = 0;
LED_PWM_4 = 0;
Wire.begin(); // 1. Zainicjuj I2C, dla nano SDA=18(PC4-A4); SCL=19(PC5-A5)
Wire.setClock(100000);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
pinMode(out3, OUTPUT);
pinMode(out4, OUTPUT);
pinMode(pwm_1, OUTPUT);
pinMode(pwm_2, OUTPUT);
pinMode(pwm_3, OUTPUT);
pinMode(pwm_4, OUTPUT);
pinMode(outled, OUTPUT);
delay(100);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
display.setTextColor(WHITE, BLACK);
// this code for new (not used) RTC.
//Wire.beginTransmission(0x68); // Start I2C protocol with DS1307 address
//Wire.write(0); // Send register address
//Wire.write(0); // start osc and reset sec
// Wire.endTransmission(); // Stop transmission and release the I2C bus
//delay(200);
//readAlarmsFromEEPROM();
} //end setup
// -----------------------------------------------
// ||| LOOP |||
// -----------------------------------------------
void loop() {
// 1. Najpierw obsługa przycisków
button_count();
// 2. Zmiana menu tylko gdy NIE jesteśmy w edycji
if (editMode == 0) {
if (button_up) {
button_up = 0;
menu_nr++;
}
if (button_down) {
button_down = 0;
menu_nr--;
}
if (menu_nr > 3) menu_nr = 1;
if (menu_nr < 1) menu_nr = 3;
if (menu_nr != menu_nr_old) {
display.clearDisplay();
menu_nr_old = menu_nr;
}
}
// 3. Odczyt DS1307 tylko gdy NIE edytujesz czasu
if (menu_nr != 2 || editMode == 0) {
time_ds1307_read_convert();
}
// 4. Wywołanie odpowiedniego ekranu
switch (menu_nr) {
case 10: screen_out(); break;
case 9: screen_time(); break;
case 8: screen_time(); break; // ustawianie czasu
case 7: screen_time(); break;
case 6: screen_time(); break;
case 5: screen_task(); break;
case 4: screen_time(); break;
case 3: screen_task(); break;
case 2: screen_time(); break;
case 1: screen_out(); break;
}
updateOutputs(hour, minute, day-1);
} //end loop
+
-
ok
back