summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2023-10-12 06:01:08 +0200
committerJonas Smedegaard <dr@jones.dk>2023-10-12 06:08:45 +0200
commit9b548e54d6905098d1e31cb7dee5dc4680dc935a (patch)
treee4e3c056ce470e7f462b27c53a403721dbc60d4f
parent0428d37b64da766b8f0c408161cffac48ab9ae8d (diff)
integrate servo functionality
-rw-r--r--main.ino58
1 files changed, 56 insertions, 2 deletions
diff --git a/main.ino b/main.ino
index 41c61ee..3850fc1 100644
--- a/main.ino
+++ b/main.ino
@@ -20,11 +20,15 @@ const int longpress_duration = 2000; // time in milliseconds
const byte BUTTON_PIN_MOVE = BUTTON_1;
const byte BUTTON_PIN_SELECT = BUTTON_2;
+const byte BUTTON_PIN_ACTION = BUTTON_3;
BlinkControl led(LED_PIN);
+#define SERVO_PIN 3
#else
-const byte BUTTON_PIN_MOVE = 2;
-const byte BUTTON_PIN_SELECT = 3;
+const byte BUTTON_PIN_MOVE = 4;
+const byte BUTTON_PIN_SELECT = 5;
+const byte BUTTON_PIN_ACTION = 2;
BlinkControl led(LED_BUILTIN); // pin 13 is built-in LED on many Arduino boards
+#define SERVO_PIN 3
#endif
// serial port speed
@@ -32,6 +36,7 @@ BlinkControl led(LED_BUILTIN); // pin 13 is built-in LED on many Arduino boards
EasyButton button_move(BUTTON_PIN_MOVE);
EasyButton button_select(BUTTON_PIN_SELECT);
+EasyButton button_action(BUTTON_PIN_ACTION);
#if defined(HAS_DISPLAY)
#define ENABLE_GxEPD2_GFX 0
@@ -68,6 +73,19 @@ GEMPage menuPageMain("Main Menu");
GEM_adafruit_gfx menu(displayEPaper, GEM_POINTER_ROW, GEM_ITEMS_COUNT_AUTO);
#endif
+#include <Servo.h>
+
+Servo servo;
+
+int servo_angle = 89;
+int servo_angle_delta = 3;
+const int servo_angle_min = 0;
+const int servo_angle_max = 90;
+
+const int servo_mode = 1;
+
+int servo_should_move = 0;
+
void setup() {
Serial.begin(BAUDRATE);
while (!Serial) {
@@ -77,9 +95,11 @@ void setup() {
button_move.begin();
button_select.begin();
+ button_action.begin();
button_move.onPressed(onButtonMovePressed);
button_move.onPressedFor(longpress_duration, onButtonMoveLongpressed);
button_select.onPressed(onButtonSelectPressed);
+ button_action.onPressed(onButtonActionPressed);
led.begin();
led.breathe(2000);
@@ -98,12 +118,17 @@ void setup() {
menu.init();
setupMenu();
menu.drawMenu();
+
+ servo.attach(SERVO_PIN);
+ servo.write(servo_angle); // place serve at initial position
}
void loop() {
led.loop();
button_move.read();
button_select.read();
+ button_action.read();
+ servoLoop();
}
#if (ESP32)
@@ -130,6 +155,15 @@ void onButtonSelectPressed() {
led.on();
}
+#if (ESP32)
+IRAM_ATTR // avoid crash e.g. when other code reads SD card
+#endif
+void onButtonActionPressed() {
+ Serial.println("button ACTION clicked");
+ led.fastBlinking();
+ servo_should_move = 1;
+}
+
void setupMenu() {
// Add menu items to menu page
menuPageMain.addMenuItem(menuItemInt);
@@ -150,3 +184,23 @@ void printData() {
Serial.println("Printing is disabled, sorry:(");
}
}
+
+void servoLoop() {
+ if (servo_should_move == 1) {
+ servo_angle = servo_angle + servo_angle_delta;
+ if (servo_angle >= servo_angle_max) {
+ servo_angle_delta = -servo_angle_delta;
+ if (servo_mode == 1) {
+ servo_should_move = 0;
+ }
+ }
+ if (servo_angle <= servo_angle_min) {
+ servo_angle_delta = -servo_angle_delta;
+ if (servo_mode == 2) {
+ servo_should_move = 0;
+ }
+ }
+ servo.write(servo_angle);
+ delay(10);
+ }
+}