summaryrefslogtreecommitdiff
path: root/datasources/arduino/arduinoAnalogSerial/analog.ino
blob: 47bf63dd6595e55a3edb41bf53321529513f8f7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// vim: filetype=cpp expandtab shiftwidth=4 softtabstop=4

const int ledPin = 13;      // select the pin for the LED

const long ADC_REF_MV = 3300;
const long ADC_MAX_STEP = 1024;
const long OV_FACTOR = 4; // Op. Verstärker Faktor = 4; 10mV = 1dB

void sendKeyValue(const char *key, const char *value) {
    Serial.print(key);
    Serial.print("=");
    Serial.println(value);
}

void sendPinValue(int pin) {
    long sensorValue,
         sensorValueMV, 
         sensorValueDBA;
    char buf[12];
    char *sensorKey = "arduino.aX", 
         *sensorKeyMV = "arduino.aX.mv",
         *sensorKeyDBA = "arduino.aX.dba";
    int port = A0 + pin;

    sensorValue = analogRead(port);
    sensorKey[9] = '0' + pin;
    snprintf(buf, 8, "%u", sensorValue);
    sendKeyValue(sensorKey, buf);

    sensorValueMV = sensorValue * (ADC_REF_MV*100/ADC_MAX_STEP);
    sensorKeyMV[9] = '0' + pin;
    snprintf(buf, 8, "%ld.%03ld", sensorValueMV/100, sensorValue%100);
    sendKeyValue(sensorKeyMV, buf);

    sensorValueDBA = sensorValueMV / OV_FACTOR; 
    sensorKeyDBA[9] = '0' + pin;
    snprintf(buf, 12, "%ld.%04ld", sensorValueDBA/1000, sensorValueDBA%1000);
    sendKeyValue(sensorKeyDBA, buf);
}

void setup() {
    Serial.begin(115200);
    // declare the ledPin as an OUTPUT:
    pinMode(ledPin, OUTPUT);  
    // Reference is EXTERNAL (3v)
    analogReference(EXTERNAL);
}

void loop() {
    if (Serial.available() > 0) {
        digitalWrite(ledPin, HIGH);
        
        int pin = Serial.read();
        while (Serial.available()) Serial.read();

        sendPinValue(pin-'0');
        Serial.flush();

        digitalWrite(ledPin, LOW);
    }
}