Skip to content
This repository
Browse code

Count nodes

  • Loading branch information...
commit 7d4643dc11da8150663f385090d1bf01f1b2cd6a 1 parent f5ba205
Robin Garen Aaberg technocake authored

Showing 1 changed file with 74 additions and 15 deletions. Show diff stats Hide diff stats

  1. +74 15 shiftregister_test/shiftregister_test.ino
89 shiftregister_test/shiftregister_test.ino
... ... @@ -1,10 +1,33 @@
  1 +// we need fundamental FILE definitions and printf declarations
  2 +#include <stdio.h>
  3 +
1 4 int SER_Pin = 13; //pin 14 on the 75HC595
2 5 int RCLK_Pin = 12; //pin 12 on the 75HC595
3   -int SRCLK_Pin = 8; //pin 11 on the 75HC595
  6 +int SRCLK_Pin = 11; //pin 11 on the 75HC595
  7 +int BKTRK_Pin = 10; //last output in a shiftregister chain.
  8 +int MR_Pin = 9; // Master Reset
  9 +
  10 +
  11 +// create a FILE structure to reference our UART output function
  12 +
  13 +static FILE uartout = {0} ;
  14 +
  15 +// create a output function
  16 +// This works because Serial.write, although of
  17 +// type virtual, already exists.
  18 +static int uart_putchar (char c, FILE *stream)
  19 +{
  20 + Serial.write(c) ;
  21 + return 0 ;
  22 +}
  23 +
4 24
5 25 //How many of the shift registers - change this
6 26 #define number_of_74hc595s 2
7   -
  27 +// Delay between node count. Visual fx only!
  28 +#define count_delay 500
  29 +//Repulse - timeslots between retransmitting pulse.
  30 +#define RE_PULSE 8
8 31 //do not touch
9 32 #define numOfRegisterPins number_of_74hc595s * 8
10 33
@@ -14,7 +37,17 @@ void setup(){
14 37 pinMode(SER_Pin, OUTPUT);
15 38 pinMode(RCLK_Pin, OUTPUT);
16 39 pinMode(SRCLK_Pin, OUTPUT);
  40 + pinMode(BKTRK_Pin, INPUT);
  41 + pinMode(MR_Pin, OUTPUT);
  42 +
  43 +
  44 + Serial.begin(9600);
  45 +
  46 + // fill in the UART file descriptor with pointer to writer.
  47 + fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
17 48
  49 + // The uart is the standard output device STDOUT.
  50 + stdout = &uartout ;
18 51
19 52 //reset all register pins
20 53 clearRegisters();
@@ -57,20 +90,46 @@ void setRegisterPin(int index, int value){
57 90 }
58 91
59 92
60   -void loop(){
  93 +void reset(int dt) {
  94 + digitalWrite(MR_Pin, LOW);
  95 + delay(dt);
  96 + digitalWrite(MR_Pin, HIGH);
  97 +}
61 98
62   - setRegisterPin(0, HIGH);
63   - setRegisterPin(1, HIGH);
64   - setRegisterPin(2, LOW);
65   - setRegisterPin(3, HIGH);
66   - setRegisterPin(4, HIGH);
67   - setRegisterPin(5, LOW);
68   - setRegisterPin(6, HIGH);
69   - setRegisterPin(7, LOW);
  99 +// Dynamically count the number of nodes on one line.
  100 +
  101 +void countNodes (int line, int * nodes) {
  102 +
  103 + int bkval = 0;
  104 + //reset
  105 + reset(count_delay);
  106 + //Sending pulse
  107 + digitalWrite(RCLK_Pin, LOW);
  108 + digitalWrite(SRCLK_Pin, LOW);
  109 + digitalWrite(SER_Pin, 1);
  110 + digitalWrite(SRCLK_Pin, HIGH);
  111 + digitalWrite(RCLK_Pin, HIGH);
  112 + // Pushed one bit, so atleast one node:
  113 + *nodes = 1;
  114 +
  115 + do {
  116 + delay (count_delay);
  117 + digitalWrite(RCLK_Pin, LOW);
  118 + digitalWrite(SRCLK_Pin, LOW);
  119 + digitalWrite(SER_Pin, (*nodes)%RE_PULSE == 0);
  120 + digitalWrite(SRCLK_Pin, HIGH);
  121 + digitalWrite(RCLK_Pin, HIGH);
  122 + (*nodes)++;
  123 + } while (int(bkval = digitalRead(BKTRK_Pin)) == 0);
70 124
  125 +}
71 126
72 127
73   - writeRegisters(); //MUST BE CALLED TO DISPLAY CHANGES
74   - writeRegisters();
75   - //Only call once after the values are set how you need.
76   -}
  128 +void loop(){
  129 + int nodes = 0;
  130 + countNodes(0, &nodes);
  131 + if (Serial.available() > 0) {
  132 + Serial.flush();
  133 + printf("%u nodes counted\n", nodes);
  134 + }
  135 + }

0 comments on commit 7d4643d

Please sign in to comment.
Something went wrong with that request. Please try again.