Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * main.cpp
- *
- *
- * 500K resistor and 5pF cap between D1 and ground
- * measured time 10-15uS without touch, >150uS with touch
- *
- */
- #include "Arduino.h"
- #define D1 5
- #define CHARGE_TIME 500000 // 500ms
- #define ERR_TIMEOUT -2
- #define ERR_NOTREADY -1
- long waitFor(int value) {
- unsigned long start = micros();
- unsigned long end = start + CHARGE_TIME; // 100ms is far too long
- unsigned long now = start;
- while(true) {
- if(now>end) { return ERR_TIMEOUT; }
- if(digitalRead(D1)==value) { break; }
- yield();
- now = micros();
- }
- return now - start;
- }
- long waitHigh() {
- return waitFor(HIGH);
- }
- long waitLow() {
- return waitFor(LOW);
- }
- void charge() {
- pinMode(D1, OUTPUT);
- digitalWrite(D1, HIGH);
- // Serial.print("charging...");
- delayMicroseconds(CHARGE_TIME); // wait for cap to charge
- // Serial.println("done");
- }
- void setup() {
- Serial.begin(9600);
- Serial.println("running test_captouch");
- // WiFi.forceSleepBegin();
- charge();
- Serial.println("setup complete");
- }
- long measure() {
- pinMode(D1, INPUT);
- long out = waitLow();
- charge();
- return out;
- }
- void loop() {
- long time = measure();
- // print sort of a bar graph -- #chars width = time/10
- // watch width change as you touch and release
- int i;
- for(i=0;i<time/10;i++) {
- Serial.print("-");
- }
- Serial.println("X");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement