I've managed to read button clicks from M5Unit 8Servos.
Here is how i connected the button to the unit.
I've managed to read button click from M5Unit 8Servos with this code.
I'm using an M5Stack AtomS3 Lite.
#include <Arduino.h>
#include <M5Unified.h>
#include <Wire.h>
#include <M5_UNIT_8SERVO.h>
#define SERVO_PIN 0
#define START_BUTTON_PIN 1
// put variables here:
M5_UNIT_8SERVO unit_8servo;
bool btnState;
unsigned long btnClieckedMs;
// put function declarations here:
void setup() {
M5.begin();
Serial.begin(115200);
Serial.println("### Setup - start");
while (!unit_8servo.begin(&Wire, G2, G1, M5_UNIT_8SERVO_DEFAULT_ADDR)) {
Serial.println("extio Connect Error");
delay(100);
}
//unit_8servo.setAllPinMode(SERVO_CTL_MODE);
unit_8servo.setOnePinMode(SERVO_PIN, SERVO_CTL_MODE);
unit_8servo.setOnePinMode(START_BUTTON_PIN, DIGITAL_INPUT_MODE);
btnState = false;
Serial.println("### Setup - end");
}
void loop() {
bool prevBtnState = btnState; // save last state for comparison
btnState = unit_8servo.getDigitalInput(START_BUTTON_PIN); // read new state
if(prevBtnState == false && btnState == true) { // if state has changed
btnClieckedMs = millis(); // save millis
} else if(prevBtnState == true && btnState == false) { // if state has changed again
if(millis() - btnClieckedMs > 50) { // check how long it took from 'false' to 'true' (debounce)
Serial.print("Button clicked "); // do some fancy things when button clicked
Serial.println(btnClieckedMs);
}
}
}