Untitled
unknown
plain_text
4 months ago
2.2 kB
6
Indexable
#include <Wire.h>
#include <VL53L0X.h>
// --- Config ---
const int MIN_DIST = 10; // Valid Hand Range Start
const int MAX_DIST = 100; // Valid Hand Range End
const int MIN_DELAY = 15; // Fast scroll delay
const int MAX_DELAY = 200; // Slow scroll delay
const int HOVER_THRESHOLD = 25; // Tolerance for "approximate same height" (mm)
const int XSHUT_PIN1 = D5;
const int XSHUT_PIN2 = D6;
VL53L0X sensor1;
VL53L0X sensor2;
void setup() {
Serial.begin(115200);
Wire.begin();
// Reset sequence
pinMode(XSHUT_PIN1, OUTPUT);
pinMode(XSHUT_PIN2, OUTPUT);
digitalWrite(XSHUT_PIN1, LOW);
digitalWrite(XSHUT_PIN2, LOW);
delay(10);
// Init S1
pinMode(XSHUT_PIN1, INPUT);
delay(10);
if (!sensor1.init()) { Serial.println("S1 Failed!"); }
sensor1.setAddress(0x2A);
sensor1.startContinuous();
// Init S2
pinMode(XSHUT_PIN2, INPUT);
delay(10);
if (!sensor2.init()) { Serial.println("S2 Failed!"); }
sensor2.startContinuous();
}
void loop() {
uint16_t d1 = sensor1.readRangeContinuousMillimeters();
uint16_t d2 = sensor2.readRangeContinuousMillimeters();
// Filter out-of-range or error readings
bool s1_active = (d1 > 0 && d1 < MAX_DIST);
bool s2_active = (d2 > 0 && d2 < MAX_DIST);
// LOGIC:
if (s1_active && s2_active) {
// BOTH sensors detect a hand within range
// Check if they are at approximately the same height
if (abs((int)d1 - (int)d2) < HOVER_THRESHOLD) {
// Both hovered at same height -> Output 'U'
// We use the average distance for the speed calculation
performScroll("U", (d1 + d2) / 2);
} else {
// Both detected, but heights are too different (conflicting)
delay(50);
}
}
else if (s1_active || s2_active) {
// ONLY ONE sensor detects a hand -> Output 'D'
int activeDist = s1_active ? d1 : d2;
performScroll("D", activeDist);
}
else {
// No hands detected
delay(50);
}
}
void performScroll(String dir, int distance) {
int constrainedDist = constrain(distance, MIN_DIST, MAX_DIST);
// Distance to Speed mapping: Closer = Faster (shorter delay)
int speedDelay = map(constrainedDist, MIN_DIST, MAX_DIST, MIN_DELAY, MAX_DELAY);
Serial.println(dir);
delay(speedDelay);
}Editor is loading...
Leave a Comment