1 | int LED = 13;
|
2 | unsigned long interruptCounter = 0;
|
3 | unsigned long now = 0;
|
4 | unsigned long lastInterrupt = 0;
|
5 | int debounceTime = 20; // Zeit in millis
|
6 | unsigned long lastPrint = 0;
|
7 | int intervalPrint = 5000; // Zeit in millis
|
8 |
|
9 | int PinA0 = 0; //analog Pin A0
|
10 | int PinA1 = 1; //analog Pin A1
|
11 | int PinA2 = 2; //analog Pin A2
|
12 | int PinA3 = 3; //analog Pin A3
|
13 |
|
14 | int val0 = 0;
|
15 | int val1 = 0;
|
16 | int val2 = 0;
|
17 | int val3 = 0;
|
18 |
|
19 | int val0StateNew = 0;
|
20 | int val1StateNew = 0;
|
21 | int val2StateNew = 0;
|
22 | int val3StateNew = 0;
|
23 |
|
24 | int val0State = 0;
|
25 | int val1State = 0;
|
26 | int val2State = 0;
|
27 | int val3State = 0;
|
28 |
|
29 | int val0Counter = 0;
|
30 | int val1Counter = 0;
|
31 | int val2Counter = 0;
|
32 | int val3Counter = 0;
|
33 |
|
34 | void setup() {
|
35 | Serial.begin(9600);
|
36 | pinMode(LED, OUTPUT); // LED Pin
|
37 | pinMode(2, INPUT); // Pin 2 ist INT0
|
38 | digitalWrite(2, HIGH); // interner Pull up Widerstand auf 5V
|
39 | attachInterrupt(0, interruptRoutine, LOW); // Pin 2 (INT 0) geht auf 0V (LOW) dann interruptRoutine aufrufen
|
40 | Serial.println(">>>--- new setup done --- --- --- --- ---");
|
41 | }
|
42 |
|
43 | void interruptRoutine() {
|
44 | if((millis() - lastInterrupt) > debounceTime) {
|
45 | lastInterrupt = millis(); // letzter Interrupt merken
|
46 | // innerhalb der entprellZeit nichts machen
|
47 |
|
48 | val0 = analogRead(PinA0); // read the input pin (takes about 100 microseconds)
|
49 | val1 = analogRead(PinA1); // read the input pin (takes about 100 microseconds)
|
50 | val2 = analogRead(PinA2); // read the input pin (takes about 100 microseconds)
|
51 | val3 = analogRead(PinA3); // read the input pin (takes about 100 microseconds)
|
52 | interruptCounter++;
|
53 | val0Function();
|
54 | val1Function();
|
55 | val2Function();
|
56 | val3Function();
|
57 | digitalWrite(LED, !digitalRead(LED)); // LED umschalten, optische Anzeige
|
58 | }
|
59 | }
|
60 | void val0Function(){
|
61 | if((val0 >= 823) && (val0 <= 1023)) {
|
62 | val0StateNew = 1;
|
63 | }
|
64 | if ((val0 >= 0) && (val0 <= 300)) {
|
65 | val0StateNew = 0; //pull-down transistor to GND needed
|
66 | }
|
67 | if (val0StateNew != val0State) {
|
68 | val0State = val0StateNew;
|
69 | val0Counter++;
|
70 | Serial.print("Counter 0: "), Serial.println(val0Counter);
|
71 | }
|
72 | }
|
73 |
|
74 | void val1Function(){
|
75 | if((val1 >= 823) && (val1 <= 1023)) {
|
76 | val1StateNew = 1;
|
77 | }
|
78 | if ((val1 >= 0) && (val1 <= 300)) {
|
79 | val1StateNew = 0; //pull-down transistor to GND needed
|
80 | }
|
81 | if (val1StateNew != val1State) {
|
82 | val1State = val1StateNew;
|
83 | val1Counter++;
|
84 | Serial.print("Counter 1: "), Serial.println(val1Counter);
|
85 | }
|
86 | }
|
87 |
|
88 | void val2Function(){
|
89 | if((val2 >= 823) && (val2 <= 1023)) {
|
90 | val2StateNew = 1;
|
91 | }
|
92 | if ((val2 >= 0) && (val2 <= 300)) {
|
93 | val2StateNew = 0; //pull-down transistor to GND needed
|
94 | }
|
95 | if (val2StateNew != val2State) {
|
96 | val2State = val2StateNew;
|
97 | val2Counter++;
|
98 | Serial.print("Counter 2: "), Serial.println(val2Counter);
|
99 | }
|
100 | }
|
101 |
|
102 | void val3Function(){
|
103 | if((val3 >= 823) && (val3 <= 1023)) {
|
104 | val3StateNew = 1;
|
105 | }
|
106 | if ((val3 >= 0) && (val3 <= 300)) {
|
107 | val3StateNew = 0; //pull-down transistor to GND needed
|
108 | }
|
109 | if (val3StateNew != val3State) {
|
110 | val3State = val3StateNew;
|
111 | val3Counter++;
|
112 | Serial.print("Counter 3: "), Serial.println(val3Counter);
|
113 | }
|
114 | }
|
115 |
|
116 | void loop() {
|
117 | if((millis() - lastPrint) >= intervalPrint) {
|
118 | now = millis();
|
119 | Serial.println(">>--- new if (in void loop) --- --- --- --- --- ---");
|
120 | Serial.print("runtime (in millis): "), Serial.print(now), Serial.print(" >> (in min): "), Serial.println(now/1000/60);
|
121 |
|
122 | Serial.print("Counter 0: "), Serial.println(val0Counter);
|
123 | Serial.print("Counter 1: "), Serial.println(val1Counter);
|
124 | Serial.print("Counter 2: "), Serial.println(val2Counter);
|
125 | Serial.print("Counter 3: "), Serial.println(val3Counter);
|
126 |
|
127 | lastPrint = (lastPrint + intervalPrint);
|
128 | }
|
129 | }
|