2021-03-09 11:43:42 +01:00
|
|
|
#include <SPI.h>
|
|
|
|
#include <Wire.h>
|
|
|
|
#include <Adafruit_GFX.h>
|
|
|
|
#include <Adafruit_SSD1306.h>
|
2021-03-19 12:05:02 +01:00
|
|
|
#include <EEPROM.h>
|
2021-03-09 11:43:42 +01:00
|
|
|
|
|
|
|
#define SCREEN_WIDTH 128 // OLED display width, in pixels
|
|
|
|
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
|
|
|
|
|
|
|
|
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
|
|
|
|
// The pins for I2C are defined by the Wire-library.
|
|
|
|
// On an arduino UNO: A4(SDA), A5(SCL)
|
|
|
|
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
|
|
|
|
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
|
2021-03-19 11:23:51 +01:00
|
|
|
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
|
2021-03-09 11:43:42 +01:00
|
|
|
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
|
|
|
|
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
2021-03-19 11:23:51 +01:00
|
|
|
#define LOGO_HEIGHT 64
|
|
|
|
#define LOGO_WIDTH 64
|
|
|
|
static const unsigned char PROGMEM logo_bmp[] =
|
|
|
|
{ B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B11110000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B11110000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B11110000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B11110000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B11111111, B00001111, B11110000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B11111111, B00001111, B11110000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B11111111, B00001111, B11110000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B11111111, B00001111, B11110000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00001111, B00000000, B00000000, B00001111, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00001111, B00000000, B00000000, B00001111, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00001111, B00000000, B00000000, B00001111, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00001111, B00000000, B00000000, B00001111, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B11110000, B00001111, B00001111, B00000000, B11110000, B00000000,
|
|
|
|
B00000000, B00000000, B11110000, B00001111, B00001111, B00000000, B11110000, B00000000,
|
|
|
|
B00000000, B00000000, B11110000, B00001111, B00001111, B00000000, B11110000, B00000000,
|
|
|
|
B00000000, B00000000, B11110000, B00001111, B00001111, B00000000, B11110000, B00000000,
|
|
|
|
B00000000, B00000000, B11110000, B11111111, B00001111, B11110000, B11110000, B00000000,
|
|
|
|
B00000000, B00000000, B11110000, B11111111, B00001111, B11110000, B11110000, B00000000,
|
|
|
|
B00000000, B00000000, B11110000, B11111111, B00001111, B11110000, B11110000, B00000000,
|
|
|
|
B00000000, B00000000, B11110000, B11111111, B00001111, B11110000, B11110000, B00000000,
|
|
|
|
B00000000, B00000000, B00001111, B00000000, B11110000, B00001111, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00001111, B00000000, B11110000, B00001111, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00001111, B00000000, B11110000, B00001111, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00001111, B00000000, B11110000, B00001111, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00001111, B00001111, B11111111, B00001111, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00001111, B00001111, B11111111, B00001111, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00001111, B00001111, B11111111, B00001111, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00001111, B00001111, B11111111, B00001111, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B11111111, B00001111, B11110000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B11111111, B00001111, B11110000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B11111111, B00001111, B11110000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B11111111, B00001111, B11110000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00001111, B00001111, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00001111, B00001111, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00001111, B00001111, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00001111, B00001111, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B11110000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B11110000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B11110000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B11110000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
|
|
|
|
};
|
|
|
|
unsigned char MotorDelay = 1;
|
|
|
|
unsigned char MotorDelayOld = 0;
|
2021-03-19 12:05:02 +01:00
|
|
|
unsigned char MotorDelayEEPROM = 0;
|
|
|
|
unsigned long previousMillis = 0;
|
2021-03-19 20:03:28 +01:00
|
|
|
unsigned long previousMillisEti = 0;
|
2021-03-19 11:23:51 +01:00
|
|
|
int UP = 0;
|
|
|
|
int DOWN = 0;
|
|
|
|
int ENTER = 0;
|
|
|
|
int UP_OLD = 0;
|
|
|
|
int DOWN_OLD = 0;
|
|
|
|
int ENTER_OLD = 0;
|
|
|
|
|
2021-03-19 20:03:28 +01:00
|
|
|
int gapDetected = 0;
|
|
|
|
int run = 0;
|
|
|
|
int finish = 1;
|
|
|
|
unsigned long Motor;
|
|
|
|
|
2021-03-09 10:33:48 +01:00
|
|
|
void setup() {
|
2021-03-19 11:23:51 +01:00
|
|
|
pinMode(SCK, INPUT);
|
|
|
|
pinMode(MOSI, INPUT);
|
|
|
|
pinMode(MISO, INPUT);
|
2021-03-19 20:03:28 +01:00
|
|
|
pinMode(12, OUTPUT);
|
|
|
|
pinMode(11, INPUT);
|
|
|
|
pinMode(7, INPUT);
|
|
|
|
|
|
|
|
pinMode(8, OUTPUT);
|
|
|
|
pinMode(9, OUTPUT);
|
|
|
|
pinMode(10, OUTPUT);
|
|
|
|
|
2021-03-19 12:05:02 +01:00
|
|
|
Serial.begin(9600);
|
2021-03-09 11:43:42 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2021-03-19 20:03:28 +01:00
|
|
|
MotorDelayEEPROM = EEPROM.read(0);
|
|
|
|
MotorDelay = MotorDelayEEPROM;
|
|
|
|
|
2021-03-19 11:23:51 +01:00
|
|
|
testdrawbitmap(); // Draw a small bitmap image
|
2021-03-09 11:43:42 +01:00
|
|
|
delay(2000); // Pause for 2 seconds
|
2021-03-19 11:23:51 +01:00
|
|
|
// Clear the buffer
|
2021-03-09 11:43:42 +01:00
|
|
|
display.clearDisplay();
|
2021-03-19 11:23:51 +01:00
|
|
|
display.display();
|
2021-03-19 20:03:28 +01:00
|
|
|
}
|
2021-03-09 10:33:48 +01:00
|
|
|
|
2021-03-19 11:23:51 +01:00
|
|
|
void loop() {
|
2021-03-19 12:05:02 +01:00
|
|
|
unsigned long currentMillis = millis();
|
2021-03-19 11:23:51 +01:00
|
|
|
UP = digitalRead(SCK);
|
|
|
|
DOWN = digitalRead(MOSI);
|
|
|
|
ENTER = digitalRead(MISO);
|
2021-03-19 12:57:13 +01:00
|
|
|
if (MotorDelay != MotorDelayOld){
|
|
|
|
writeDisplay();
|
2021-03-19 11:23:51 +01:00
|
|
|
MotorDelayOld = MotorDelay;
|
|
|
|
}
|
|
|
|
if(UP == 1 && UP != UP_OLD){
|
|
|
|
MotorDelay++;
|
|
|
|
}
|
|
|
|
if(DOWN == 1 && DOWN != DOWN_OLD){
|
|
|
|
MotorDelay--;
|
|
|
|
}
|
2021-03-19 12:05:02 +01:00
|
|
|
if (ENTER == 1){
|
|
|
|
if (currentMillis - previousMillis >= 2000) {
|
|
|
|
previousMillis = currentMillis;
|
|
|
|
EEPROM.write(0, MotorDelay);
|
|
|
|
MotorDelayEEPROM = EEPROM.read(0);
|
2021-03-19 12:57:13 +01:00
|
|
|
writeDisplay();
|
|
|
|
MotorDelayOld = MotorDelay;
|
2021-03-19 12:05:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
previousMillis = currentMillis;
|
|
|
|
}
|
2021-03-19 20:03:28 +01:00
|
|
|
|
|
|
|
unsigned long currentMillisEti = millis();
|
2021-04-12 21:42:08 +02:00
|
|
|
Motor = MotorDelay * 20;
|
|
|
|
if (digitalRead(11) == LOW && run == 0 && finish == 1){
|
2021-03-19 20:03:28 +01:00
|
|
|
run = 1;
|
|
|
|
finish = 0;
|
|
|
|
}
|
|
|
|
if (run == 1) {
|
|
|
|
digitalWrite(12, HIGH);
|
|
|
|
if (digitalRead(7) == HIGH){
|
|
|
|
gapDetected = 1;
|
2021-04-12 21:42:08 +02:00
|
|
|
}
|
|
|
|
if(gapDetected == 1) {
|
2021-03-19 20:03:28 +01:00
|
|
|
if (currentMillisEti - previousMillisEti >= Motor) {
|
|
|
|
previousMillisEti = currentMillisEti;
|
|
|
|
run = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
previousMillisEti = currentMillisEti;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
digitalWrite(12, LOW);
|
|
|
|
previousMillisEti = currentMillisEti;
|
|
|
|
}
|
2021-04-12 21:42:08 +02:00
|
|
|
if (run == 0 && digitalRead(11) == HIGH){
|
2021-03-19 20:03:28 +01:00
|
|
|
finish = 1;
|
2021-04-12 21:42:08 +02:00
|
|
|
gapDetected = 0;
|
2021-03-19 20:03:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-19 11:23:51 +01:00
|
|
|
UP_OLD = UP;
|
|
|
|
DOWN_OLD = DOWN;
|
|
|
|
ENTER_OLD = ENTER;
|
2021-04-12 21:42:08 +02:00
|
|
|
|
2021-03-09 10:33:48 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 11:23:51 +01:00
|
|
|
void testdrawbitmap(void) {
|
|
|
|
display.clearDisplay();
|
2021-03-09 10:33:48 +01:00
|
|
|
|
2021-03-19 11:23:51 +01:00
|
|
|
display.drawBitmap(
|
|
|
|
(display.width() - LOGO_WIDTH ) / 2,
|
|
|
|
(display.height() - LOGO_HEIGHT) / 2,
|
|
|
|
logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
|
|
|
|
display.display();
|
2021-03-09 10:33:48 +01:00
|
|
|
}
|
2021-03-19 12:57:13 +01:00
|
|
|
|
|
|
|
void writeDisplay(void) {
|
|
|
|
display.setTextSize(2);
|
|
|
|
display.setTextColor(SSD1306_WHITE); // Draw white text
|
|
|
|
display.setCursor(0,0); // Start at top-left corner
|
|
|
|
display.clearDisplay();
|
|
|
|
display.println(F("Delay: ")); display.println(MotorDelay); display.println(F("EEPROM: ")); display.println(MotorDelayEEPROM);
|
|
|
|
display.display();
|
|
|
|
}
|