#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
////////////////////////// WYSWIETLACZE I2C //////////////////////////
#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
#define SCREEN_ADDRESS_1 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//////////////////////////////////////////////////////////////////////////////
unsigned long currentMillis = millis();
////////////////////////// DIODA LED NIEBIESKA //////////////////////////
const int B_LED = 26; // the number of the LED pin
//int B_ledState = LOW; // ledState used to set the LED
//unsigned long B_previousMillis = 0; // will store last time LED was updated
//const long B_interval = 250; // interval at which to blink (milliseconds)
//////////////////////////////////////////////////////////////////////////////
////////////////////////// DIODA LED POMARANCZOWA //////////////////////////
const int O_LED = 27; // the number of the LED pin
//int O_ledState = LOW; // ledState used to set the LED
//////////////////////////////////////////////////////////////////////////////
////////////////////////// PRZYCISKI //////////////////////////
const int button_L = 18; // LEFT the number of the pushbutton pin
const int button_R = 19; // RIGHT the number of the pushbutton pin
const int button_D = 20; // DOWN the number of the pushbutton pin
const int button_U = 21; // UP the number of the pushbutton pin
const int button_E = 22; // ENTER the number of the pushbutton pin
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
//////////////////
///SILNIK KROKOWY
#define DIR_PIN 16
#define STEP_PIN 17
//////////////////
void setup() {
//////////////////
//pinMode(button_Pin_L, INPUT_PULLUP);
//pinMode(button_Pin_R, INPUT_PULLUP);
//pinMode(button_Pin_D, INPUT_PULLUP);
//pinMode(button_Pin_L, INPUT_PULLUP);
//pinMode(button_Pin_L, INPUT_PULLUP);
//////////////////
//digitalWrite(O_LED, O_ledState);
//////////////////
//pinMode(B_LED, OUTPUT);
//////////////////
//pinMode(O_LED, OUTPUT);
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display1.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS_1)) {
Serial.println(F("SSD1306 1 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
display1.display();
delay(2000); // Pause for 2 seconds
display.invertDisplay(true);
delay(500);
display1.invertDisplay(true);
delay(500);
display.invertDisplay(false);
delay(500);
display1.invertDisplay(false);
delay(500);
// Clear the buffer
display.clearDisplay();
display1.clearDisplay();
// Draw a single pixel in white
display.drawPixel(10, 10, SSD1306_WHITE);
display1.drawPixel(10, 10, SSD1306_WHITE);
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
display.display();
// delay(1000);
display1.display();
// delay(1000);
// Invert and restore display, pausing in-between
display.invertDisplay(true);
delay(500);
display1.invertDisplay(true);
delay(500);
display.invertDisplay(false);
delay(500);
display1.invertDisplay(false);
delay(500);
//testanimate(logo_bmp, LOGO_WIDTH, LOGO_HEIGHT); // Animate bitmaps
testdrawchar1(); // Draw characters of the default font
testdrawstyles(); // Draw 'stylized' characters
testscrolltext1(); // Draw scrolling text
cleardisplay();
cleardisplay1();
///SILNIK KROKOWY
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(STEP_PIN, LOW);
}
////////////////
void loop() {
////////////////////////// MIGANIE DIODY POMARANCZOWEJ PRZY WCISNIECIU PRZYCISKU
// int reading_L = digitalRead(buttonPin_L);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
// if (reading_L != lastButtonState_L) {
// reset the debouncing timer
// lastDebounceTime_L = millis();
// }
// if ((millis() - lastDebounceTime_L) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
// if (reading_L != buttonState_L) {
// buttonState_L = reading_L;
// only toggle the LED if the new button state is HIGH
// if (buttonState_L == HIGH) {
// O_ledState = !O_ledState;
// miganie_diody(O_LED, 500);
// }
// }
// }
// set the LED:
//digitalWrite(O_LED, O_ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
// lastButtonState_L = reading_L;
////////////////////////////////////////////////////
//miganie_diody(27, 250);
miganie_diody(B_LED, 250);
sprawdzanie_przycisku(18, 27, 500);
// Move 200 steps (one rotation) CW over one second
// digitalWrite(DIR_PIN, HIGH);
// for (int i = 0; i < 200; i++) {
// digitalWrite(STEP_PIN, HIGH);
// digitalWrite(STEP_PIN, LOW);
// delay(5); // 5 ms * 200 = 1 second
// }
// delay(500); // Wait half a second
// // Move 200 steps (one rotation) CCW over 400 millis
// digitalWrite(DIR_PIN, LOW);
// for (int i = 0; i < 200; i++) {
// digitalWrite(STEP_PIN, HIGH);
// digitalWrite(STEP_PIN, LOW);
// delay(2); // 2 ms * 200 = 0.4 seconds
// }
// delay(1000); // Wait another second
}
void sprawdzanie_przycisku(const int button_Pin, const int LED_pin, int LED_interval) {
pinMode(button_Pin, INPUT_PULLUP);
pinMode(LED_pin, OUTPUT);
//const int buttonPin_L = 2; // Przykładowy pin przycisku
//const int ledPin_L = 13; // Przykładowy pin diody LED
int lastButtonState = LOW; // Poprzedni stan przycisku
int buttonState = digitalRead(button_Pin); // Aktualny stan przycisku
static unsigned long lastDebounceTime = 0; // Czas ostatniego odbicia
unsigned long debounceDelay = 50; // Opóźnienie debouncingu
Serial.println(buttonState);
if (buttonState != lastButtonState) {
// Zresetuj timer debouncingu
lastDebounceTime = millis();
}
// Jeśli minęło wystarczająco czasu od ostatniego odbicia
if ((millis() - lastDebounceTime) > debounceDelay) {
// Jeśli stan przycisku się zmienił
if (buttonState != lastButtonState) {
lastButtonState = buttonState;
// Jeśli nowy stan przycisku jest HIGH (wciśnięty)
if (buttonState == HIGH) {
digitalWrite(LED_pin, HIGH);
} else {
digitalWrite(LED_pin, LOW);
}
}
}
}
//niebieska port 26
void miganie_diody(const int LED_pin, int LED_interval){
////////////////////////// MIGANIE DIODY NIEBIESKIEJ STATUSU //////////////////////////
pinMode(LED_pin, OUTPUT);
int LED_State = LOW; // ledState used to set the LED
static unsigned long LED_previousMillis = 0; // will store last time LED was updated
//const long LED_interval = 250; // interval at which to blink (milliseconds)
unsigned long currentMillis = millis();
if (currentMillis - LED_previousMillis >= LED_interval) {
// save the last time you blinked the LED
LED_previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (LED_State == LOW) {
LED_State = HIGH;
} else {
LED_State = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(LED_pin, LED_State);
}
//////////////////////////////////////////////////////////////////////////////
}
void miganie_diody1(const int LED_pin, int LED_interval){
////////////////////////// MIGANIE DIODY NIEBIESKIEJ STATUSU //////////////////////////
pinMode(LED_pin, OUTPUT);
int LED_State = LOW; // ledState used to set the LED
static unsigned long LED_previousMillis = 0; // will store last time LED was updated
//const long LED_interval = 250; // interval at which to blink (milliseconds)
unsigned long currentMillis = millis();
if (currentMillis - LED_previousMillis >= LED_interval) {
// save the last time you blinked the LED
LED_previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (LED_State == LOW) {
LED_State = HIGH;
} else {
LED_State = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(LED_pin, LED_State);
}
//////////////////////////////////////////////////////////////////////////////
}
void testdrawchar1(void) {
display1.clearDisplay();
display1.setTextSize(1); // Normal 1:1 pixel scale
display1.setTextColor(SSD1306_WHITE); // Draw white text
display1.setCursor(0, 0); // Start at top-left corner
display1.cp437(true); // Use full 256 char 'Code Page 437' font
// Not all the characters will fit on the display. This is normal.
// Library will draw what it can and the rest will be clipped.
for(int16_t i=0; i<256; i++) {
if(i == '\n') display1.write(' ');
else display1.write(i);
}
display1.display();
delay(2000);
}
void testdrawstyles(void) {
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(F("Siema cwoki!"));
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
display.println(3.141592);
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.print(F("0x")); display.println(0xDEADBEEF, HEX);
display.display();
delay(2000);
}
void testscrolltext1(void) {
display1.clearDisplay();
display1.setTextSize(2); // Draw 2X-scale text
display1.setTextColor(SSD1306_WHITE);
display1.setCursor(10, 0);
display1.println(F("POZDRAWIAMY"));
display1.display(); // Show initial text
//delay(100);
// Scroll in various directions, pausing in-between:
display1.startscrollright(0x00, 0x0F);
//delay(2000);
display1.stopscroll();
//delay(1000);
display1.startscrollleft(0x00, 0x0F);
//delay(2000);
display1.stopscroll();
//delay(1000);
display1.startscrolldiagright(0x00, 0x07);
//delay(2000);
display1.startscrolldiagleft(0x00, 0x07);
//delay(2000);
display1.stopscroll();
//delay(1000);
}
void cleardisplay(void){
display.clearDisplay();
display.display();
}
void cleardisplay1(void){
display1.clearDisplay();
display1.display();
}