Zum Inhalt

sensor_read.tc

Read analog sensor and display via serial

Source on GitHub

// Read analog sensor and display via serial
#define SENSOR_PIN 34
#define LED_PIN 2
#define INPUT         0x01
#define OUTPUT        0x03
#define INPUT_PULLUP  0x05
#define INPUT_PULLDOWN 0x09
#define THRESHOLD 2000

int main() {
    serialBegin(3, 1, 115200, 3, 64);  // RX=3, TX=1, 115200 baud, 8N1, 64 byte buf
    pinMode(LED_PIN, OUTPUT);

    int count = 0;
    while (count < 20) {
        int value = analogRead(SENSOR_PIN);

        serialPrint("Sensor: ");
        serialPrintInt(value);
        serialPrintln("");

        if (value > THRESHOLD) {
            digitalWrite(LED_PIN, 1);
        } else {
            digitalWrite(LED_PIN, 0);
        }

        delay(100);
        count++;
    }
    return 0;
}