|
| 1 | +/* |
| 2 | + * Simple logic analyzer. Triggers when the data pin goes low. |
| 3 | + * Records bits until 30 1's in a row are received. |
| 4 | + * Attemps to debounce the button (poorly). Resets the board |
| 5 | + * (reset pin low) when the button is pressed, and starts listening. |
| 6 | +*/ |
| 7 | + |
| 8 | +const int buttonPin = 7; // the number of the pushbutton pin |
| 9 | +const int dataPin = 2; // corresponds to pin 6 on the KB connector (and pin 6 of the RJ12) |
| 10 | +const int resetPin = 5; // corresponds to pin 2 on the KB connector (and pin 4 on the RJ12) |
| 11 | + |
| 12 | +int buttonState = 0; // variable for reading the pushbutton status |
| 13 | + |
| 14 | +void setup() { |
| 15 | + pinMode(LED_BUILTIN, OUTPUT); |
| 16 | + pinMode(buttonPin, INPUT); |
| 17 | + pinMode(resetPin, OUTPUT); |
| 18 | + pinMode(dataPin, INPUT); |
| 19 | + |
| 20 | + // Set the reset pin low for 10 ms. |
| 21 | + digitalWrite(resetPin, LOW); |
| 22 | + delay(10); |
| 23 | + digitalWrite(resetPin, HIGH); |
| 24 | + |
| 25 | + Serial.begin(9600); |
| 26 | + Serial.println("Hello logic_analyzer"); |
| 27 | +} |
| 28 | + |
| 29 | +int changed = 0; |
| 30 | +int shouldRead = 0; |
| 31 | +int data = 0; |
| 32 | +int inKey = 0; // are we reading a key? |
| 33 | +int numOnes = 0; // the # of 1s in a row |
| 34 | + |
| 35 | +void loop() { |
| 36 | + // read the state of the pushbutton value: |
| 37 | + buttonState = digitalRead(buttonPin); |
| 38 | + |
| 39 | + // check if the pushbutton is pressed. If it is, the buttonState is HIGH: |
| 40 | + if (buttonState == HIGH && !changed) { |
| 41 | + // turn LED on, and toggle recording. |
| 42 | + digitalWrite(LED_BUILTIN, HIGH); |
| 43 | + Serial.println("on"); |
| 44 | + shouldRead = 1 - shouldRead; |
| 45 | + Serial.print("ShouldRead is "); Serial.println(shouldRead); |
| 46 | + changed = 1; |
| 47 | + } else if (buttonState == LOW && changed) { |
| 48 | + // turn LED off: |
| 49 | + digitalWrite(LED_BUILTIN, LOW); |
| 50 | + Serial.println("off"); |
| 51 | + changed = 0; |
| 52 | + |
| 53 | + // beep/reset, and keep reading. |
| 54 | + digitalWrite(resetPin , LOW); |
| 55 | + delay(10); |
| 56 | + digitalWrite(resetPin , HIGH); |
| 57 | + } |
| 58 | + |
| 59 | + if (shouldRead == 1) { |
| 60 | + data = digitalRead(dataPin); |
| 61 | + if (data == LOW && !inKey) { |
| 62 | + inKey=1; // we're recording until numones==30 |
| 63 | + numOnes=0; |
| 64 | + Serial.println("START"); |
| 65 | + Serial.println("0"); |
| 66 | + } else if (inKey) { |
| 67 | + if (data == HIGH) { |
| 68 | + Serial.println("1"); |
| 69 | + numOnes++; |
| 70 | + if (numOnes == 30) { |
| 71 | + inKey=0; |
| 72 | + numOnes=0; |
| 73 | + Serial.println("END"); |
| 74 | + delay(500); |
| 75 | + } |
| 76 | + } else { |
| 77 | + numOnes=0; |
| 78 | + Serial.println("0"); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments