// Length measurement using a soft potentiometer.
// Keyboard input of the received length (value in inches).
#include <Keyboard.h>
#define BUTTON_PIN 7
#define TAPE_PIN A0 // Pin connected to softpot wiper
#define TAPE_LENGTH 200 // Softpot length in tenths (1 tenth = 1/10 inch)
void setup() {
pinMode(BUTTON_PIN, INPUT);
pinMode(TAPE_PIN, INPUT);
Serial.begin(9600); // debug
Keyboard.begin();
}
void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) { // if the button is pressed
int x = analogRead(TAPE_PIN); // Read the value from the sensor (0-1023)
if (x > 0) {
float d = map(x, 0, 1023, 0, TAPE_LENGTH); // Map the 0-1023 value to 0-TAPE_LENGTH
float l = d / 10; // Convert length to inches
Serial.println("measured length: " + String(l) + " inches"); // debug
Keyboard.print(l); // Input value
}
delay(500);
}
}
Comments