Skip to content

Instantly share code, notes, and snippets.

@JamesTheBard
Created October 25, 2022 01:14
Show Gist options
  • Save JamesTheBard/bae4de6daef07d82a72fdd3db2a0834c to your computer and use it in GitHub Desktop.
Save JamesTheBard/bae4de6daef07d82a72fdd3db2a0834c to your computer and use it in GitHub Desktop.
How does one track states?
#include <Arduino.h>
#include <Serial.h>
#define THRESHOLD 1.23
String states[4] = {
"Ready to dock", // Phone is not in cradle || !ps1 & !ps2 & !ds
"Docking", // Phone is docking || ps1 & ps2 & !ds
"Is Docked", // Phone is docked in cradle || ps1 & ps2 & ds
"Ejecting" // Phone is ejecting || ps1 & ps2 & !ds
};
// Is phone in cradle?
bool phone_sensor_1;
bool phone_sensor_2;
// Is the phone docked?
bool dock_sensor;
int current_state;
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
}
void loop() {
updateSensors();
current_state = getCurrentState(current_state);
if (current_state == 0 && (phone_sensor_1 && phone_sensor_2)) {
current_state = 1;
dockPhone();
}
else if (current_state == 2 && readIRSensor()) {
bool do_it = true;
for (int i = 0; i < 10; i++) {
delay(50);
if (!readIRSensor()) {
do_it = false;
break;
}
}
if (do_it) {
current_state = 3;
ejectPhone();
}
}
}
void dockPhone() {
// Do docking stuff here
}
void updateSensors() {
phone_sensor_1 = readOpticalSensor(1, THRESHOLD);
phone_sensor_2 = readOpticalSensor(2, THRESHOLD);
dock_sensor = readDockSensor();
}
bool readIRSensor() {
// Read your sensor here and return the value
return true;
}
int getCurrentState(int current_state) {
if (phone_sensor_1 && phone_sensor_2 && dock_sensor) {
return 2;
}
if (!phone_sensor_1 && !phone_sensor_2 && !dock_sensor) {
return 0;
}
return current_state;
}
bool doServoStuff() {
return true;
}
bool readOpticalSensor(int sensor, float threshold) {
if (getProximityValue(sensor) > threshold) {
return true;
};
return false;
}
float getProximityValue(int sensor) {
return 10.0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment