/*
vcc = 3.3v
gnd = gnd
clk = IO22
DT = IO23
SW = IO21
*/
#include "AiEsp32RotaryEncoder.h"
#include <SPI.h>
#include "Arduino.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <ESP32Servo.h>
#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
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#if defined(ESP8266)
#define ROTARY_ENCODER_A_PIN D6
#define ROTARY_ENCODER_B_PIN D5
#define ROTARY_ENCODER_BUTTON_PIN D7
#else
#define ROTARY_ENCODER_A_PIN 12
#define ROTARY_ENCODER_B_PIN 13
#define ROTARY_ENCODER_BUTTON_PIN 15
#endif
#define ROTARY_ENCODER_STEPS 4
//instead of changing here, rather change numbers above
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, -1, ROTARY_ENCODER_STEPS);
/*
Select fast food - Hot dog, Pizza, Hamburger, Cheeseburger
Then select how many pieces 1 to 10
At the end select if you want fast delivery for extra 2$
*/
String options[] = {"Length", "Quantity", "Ready?", "Process"};
String menus[] = {"Length", "Quantity", "Start"};
String selectedMenu = "";
String command[] = {"Start", "Cancel"};
String selectedCommand = "";
int selectedLength = 10;
int selectedQuantity = 1;
int currentQuantity = 0;
int currentOption = 0;
const int servoPin = 18;
Servo servo;
void drawCentreString(const String &buf, int x, int y, int fontsize=1)
{
int16_t x1, y1;
uint16_t w, h;
display.setTextSize(fontsize);
display.getTextBounds(buf, 0, 0, &x1, &y1, &w, &h); //calc width of new string
display.setTextSize(fontsize);
display.setCursor(x - w / 2, y);
display.print(buf);
}
void setForOption(int newOption)
{
currentOption = newOption;
switch (newOption)
{
case 0:
// rotaryEncoder.setBoundaries(0, 2, true); // Main menu
// rotaryEncoder.setEncoderValue(0);
rotaryEncoder.setBoundaries(10, 1000, false); //how many Length 10 to 1000;
rotaryEncoder.setEncoderValue(10);
currentQuantity = 0;
break;
case 1:
rotaryEncoder.setBoundaries(1, 1000, false); //how many pieces 1 to 1000; do not go from 10 to 1
rotaryEncoder.setEncoderValue(1);
break;
case 2:
rotaryEncoder.setBoundaries(0, 1, false); //select start stop
rotaryEncoder.setEncoderValue(0);
break;
case 3:
rotaryEncoder.setBoundaries(0, 1, false); //select start stop
rotaryEncoder.setEncoderValue(0);
break;
default:
break;
}
Serial.println(options[currentOption]);
//display.setTextSize(2);
//display.setCursor(1, 5);
//display.clearDisplay();
//display.println(options[currentOption]);
//display.display();
//delay(2000); // Pause for 2 seconds
showSelection();
}
void rotary_onButtonClick()
{
static unsigned long lastTimePressed = 0;
if (millis() - lastTimePressed < 200)
return;
lastTimePressed = millis();
int selecedValue = rotaryEncoder.readEncoder();
switch (currentOption)
{
case 0: //"Select Length"
selectedLength = selecedValue;
setForOption(1);
break;
case 1: //"Select quantity"
selectedQuantity = selecedValue;
setForOption(2);
break;
case 2: //"Select command"
selectedCommand = command[selecedValue];
showOrder();
setForOption(3);
break;
case 3: //"Select command"
selectedCommand = command[selecedValue];
setForOption(0);
break;
default:
break;
}
}
void showOrder()
{
Serial.println("++++++++++++++++++++++++ ORDER DETAILS ++++++++++++++++++++++++++++++++++++++++++");
//Serial.print("Menu ");
//Serial.print(selectedMenu);
Serial.print("Length: ");
Serial.print(selectedLength);
Serial.print(", quantity: ");
Serial.print(selectedQuantity);
Serial.print(", command: ");
Serial.println(selectedCommand);
Serial.println("---------------------------------------------------------------------------------");
Serial.println("");
Serial.print("NEW ORDER ");
}
void IRAM_ATTR readEncoderISR()
{
rotaryEncoder.readEncoder_ISR();
}
void servoCut()
{
if(currentQuantity < selectedQuantity){
servo.write(180);
delay(500);
servo.write(0);
delay(500);
currentQuantity = currentQuantity + 1;
Serial.print("Cut: ");
Serial.println(currentQuantity);
showSelection();
}
}
void setup()
{
servo.attach(servoPin, 500, 2400);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// display.display();
delay(2000); // Pause for 2 seconds
servo.write(0);
// Clear the buffer
display.clearDisplay();
// Draw a single pixel in white
// display.drawPixel(1, 1, SSD1306_WHITE);
// display.drawPixel(SCREEN_WIDTH, 1, SSD1306_WHITE);
// display.drawPixel(1, SCREEN_HEIGHT, SSD1306_WHITE);
// display.drawPixel(SCREEN_WIDTH, SCREEN_HEIGHT, SSD1306_WHITE);
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
// display.println(F("HEIGHT"));
// display.display(); // Show initial text
Serial.begin(115200);
rotaryEncoder.begin();
rotaryEncoder.setup(readEncoderISR);
rotaryEncoder.setAcceleration(0);
rotaryEncoder.correctionOffset=2; //try with zero or ROTARY_ENCODER_STEPS/2
rotaryEncoder.isButtonPulldown = false;
rotaryEncoder.areEncoderPinsPulldownforEsp32 = true;
setForOption(0);
}
void showSelection()
{
int selecedValue = rotaryEncoder.readEncoder();
switch (currentOption)
{
case 9: //"main menu"
Serial.println(menus[selecedValue]);
//display.clearDisplay();
//drawCentreString(options[currentOption], 1, 64);
//drawCentreString(menus[selecedValue], 32, 64);
//display.println(options[currentOption]);
//display.println(menus[selecedValue]);
//display.display();
break;
case 0: //"Select Length"
Serial.print("Length: ");
Serial.println(selecedValue);
display.setTextSize(2);
display.setCursor(1, 5);
display.clearDisplay();
//display.println(options[currentOption]);
//display.println(selecedValue);
drawCentreString(options[currentOption], 64, 1, 2);
drawCentreString(String(selecedValue), 64, 25, 2);
display.display();
break;
case 1: //"Select quantity"
Serial.print("Quantity: ");
Serial.println(selecedValue);
display.setTextSize(2);
display.setCursor(1, 5);
display.clearDisplay();
//display.println(options[currentOption]);
//display.println(selecedValue);
drawCentreString(options[currentOption], 64, 1, 2);
drawCentreString(String(selecedValue), 64, 25, 2);
display.display();
break;
case 2: //"Select command"
Serial.println(command[selecedValue]);
display.setTextSize(2);
display.setCursor(1, 5);
display.clearDisplay();
//display.println(options[currentOption]);
//display.println(command[selecedValue]);
drawCentreString(options[currentOption], 64, 1, 2);
drawCentreString(command[selecedValue], 64, 25, 2);
display.display();
break;
case 3: //"Display"
//Serial.println(command[selecedValue]);
//display.setTextSize(1);
//display.setCursor(1, 25);
display.clearDisplay();
//display.println(options[currentOption]);
drawCentreString(options[currentOption], 64, 1, 2);
display.setTextSize(1);
display.setCursor(1, 25);
display.print("Length: ");
display.println(selectedLength);
display.print("Quantity: ");
display.print(selectedQuantity);
display.print("/");
display.println(currentQuantity);
display.println(command[selecedValue]);
display.display();
break;
default:
break;
}
}
void loop()
{
if (rotaryEncoder.encoderChanged())
{
showSelection();
}
if (rotaryEncoder.isEncoderButtonClicked())
{
rotary_onButtonClick();
}
switch (currentOption)
{
case 3:
servoCut();
break;
default:
break;
}
}