summaryrefslogtreecommitdiff
path: root/sketch_mar5a/sketch_mar5a.ino
blob: 11de40ea737231efbddce5d0537cbdb842165950 (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
// This pin is used to read data from the shift register (4021).
const uint8_t PISO_DATA_PIN = 5;

// This pin should be connected to the clock pin used to advance output in the shift regitster (4021).
const uint8_t PISO_CLOCK_PIN = 4;

// This pin should be connected to the data out of the flip-flop (4013).
const uint8_t DATA_READY_PIN = 3;

// This pin is used to reset the state of the rotary dial board.
// It should be connected to the reset pin of the flip-flop (4013) and counter (4520).
const uint8_t RESET_PIN = 2;

void setup() {
  Serial.begin(9600);
  pinMode(PISO_DATA_PIN, INPUT);
  pinMode(PISO_CLOCK_PIN, OUTPUT);
  pinMode(DATA_READY_PIN, INPUT);
  pinMode(RESET_PIN, OUTPUT);
}

void loop() {
  if (digitalRead(DATA_READY_PIN)) {
    Serial.println("Data ready from rotary dial circuit!!");

    digitalWrite(PISO_CLOCK_PIN, LOW);
    uint8_t data = 0;
    for (int i = 0; i < 8; ++i) {
      int bit = digitalRead(PISO_DATA_PIN);
      data = (data << 1) | bit;

      digitalWrite(PISO_CLOCK_PIN, HIGH);
      digitalWrite(PISO_CLOCK_PIN, LOW);
    }

    //uint8_t data = shiftIn(PISO_DATA_PIN, PISO_CLOCK_PIN, MSBFIRST);
    Serial.print("Number: ");
    Serial.print(data, DEC);
    Serial.print(", ");
    Serial.println(data, BIN);

    digitalWrite(RESET_PIN, LOW);
    digitalWrite(RESET_PIN, HIGH);
    digitalWrite(RESET_PIN, LOW);
  }
}