|
2 | 2 |
|
3 | 3 | // potentiometer wiper (middle terminal) connected to analog pin 3
|
4 | 4 | // outside leads to ground and +5V
|
5 |
| -int analogPin = A3; |
| 5 | +int analogPin = A3; |
| 6 | +// Pin to trigger |
| 7 | +const int triggerPin = 2; |
| 8 | + |
| 9 | +int trigger = 0; |
6 | 10 | int val = 0; // variable to store the value read
|
| 11 | +int shouldRead = 0; |
| 12 | +int ones = 0; |
7 | 13 |
|
8 | 14 | void setup() {
|
9 |
| - Serial.begin(9600); |
10 |
| - Serial.println("Hello scope"); |
| 15 | + pinMode(triggerPin, INPUT); |
| 16 | + |
| 17 | + Serial.begin(1000000); |
| 18 | + Serial.println("Hello trigger scope"); |
11 | 19 | }
|
12 | 20 |
|
13 | 21 | void loop() {
|
14 |
| - // read the analog port |
15 |
| - // send back to the result as an appropriately formatted string |
16 |
| - val = analogRead(analogPin); // read the input pin |
| 22 | + trigger = digitalRead(triggerPin); |
| 23 | + if (!shouldRead && trigger == LOW) { |
| 24 | + Serial.println("triggered"); |
| 25 | + shouldRead = 1; |
| 26 | + ones = 0; |
| 27 | + } |
| 28 | + if (shouldRead) { |
| 29 | + Serial.print("kb "); Serial.print(trigger); Serial.print(" Analog: "); |
| 30 | + } |
| 31 | + if (shouldRead && trigger == HIGH) { |
| 32 | + // Stop reading after 30 ones in a row. |
| 33 | + ones++; |
| 34 | + if (ones == 300) { |
| 35 | + Serial.println("untriggered"); |
| 36 | + shouldRead = 0; |
| 37 | + } |
| 38 | + } |
| 39 | + if (shouldRead) { |
| 40 | + // read the analog port |
| 41 | + // send back to the result as an appropriately formatted string |
| 42 | + val = analogRead(analogPin); // read the input pin |
17 | 43 |
|
18 |
| - // Scale to 80 chars wide, assuming a 10bit A/D |
19 |
| - int width=80*(val/1024.0); |
20 |
| - for (int x = 0; x < width; ++x) { |
21 |
| - Serial.print(" "); |
| 44 | + // Scale to 80 chars wide, assuming a 10bit A/D |
| 45 | + int width = 80 * (val / 1024.0); |
| 46 | + for (int x = 0; x < width; ++x) { |
| 47 | + Serial.print(" "); |
| 48 | + } |
| 49 | + Serial.print("* "); |
| 50 | + Serial.println(5.0*val/1024); // debug value |
22 | 51 | }
|
23 |
| - Serial.println(val); // debug value |
24 | 52 | }
|
0 commit comments