Permalink
Browse files

Count nodes

  • Loading branch information...
1 parent f5ba205 commit 7d4643dc11da8150663f385090d1bf01f1b2cd6a @technocake technocake committed Mar 19, 2013
Showing with 74 additions and 15 deletions.
  1. +74 −15 shiftregister_test/shiftregister_test.ino
View
89 shiftregister_test/shiftregister_test.ino
@@ -1,10 +1,33 @@
+// we need fundamental FILE definitions and printf declarations
+#include <stdio.h>
+
int SER_Pin = 13; //pin 14 on the 75HC595
int RCLK_Pin = 12; //pin 12 on the 75HC595
-int SRCLK_Pin = 8; //pin 11 on the 75HC595
+int SRCLK_Pin = 11; //pin 11 on the 75HC595
+int BKTRK_Pin = 10; //last output in a shiftregister chain.
+int MR_Pin = 9; // Master Reset
+
+
+// create a FILE structure to reference our UART output function
+
+static FILE uartout = {0} ;
+
+// create a output function
+// This works because Serial.write, although of
+// type virtual, already exists.
+static int uart_putchar (char c, FILE *stream)
+{
+ Serial.write(c) ;
+ return 0 ;
+}
+
//How many of the shift registers - change this
#define number_of_74hc595s 2
-
+// Delay between node count. Visual fx only!
+#define count_delay 500
+//Repulse - timeslots between retransmitting pulse.
+#define RE_PULSE 8
//do not touch
#define numOfRegisterPins number_of_74hc595s * 8
@@ -14,7 +37,17 @@ void setup(){
pinMode(SER_Pin, OUTPUT);
pinMode(RCLK_Pin, OUTPUT);
pinMode(SRCLK_Pin, OUTPUT);
+ pinMode(BKTRK_Pin, INPUT);
+ pinMode(MR_Pin, OUTPUT);
+
+
+ Serial.begin(9600);
+
+ // fill in the UART file descriptor with pointer to writer.
+ fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
+ // The uart is the standard output device STDOUT.
+ stdout = &uartout ;
//reset all register pins
clearRegisters();
@@ -57,20 +90,46 @@ void setRegisterPin(int index, int value){
}
-void loop(){
+void reset(int dt) {
+ digitalWrite(MR_Pin, LOW);
+ delay(dt);
+ digitalWrite(MR_Pin, HIGH);
+}
- setRegisterPin(0, HIGH);
- setRegisterPin(1, HIGH);
- setRegisterPin(2, LOW);
- setRegisterPin(3, HIGH);
- setRegisterPin(4, HIGH);
- setRegisterPin(5, LOW);
- setRegisterPin(6, HIGH);
- setRegisterPin(7, LOW);
+// Dynamically count the number of nodes on one line.
+
+void countNodes (int line, int * nodes) {
+
+ int bkval = 0;
+ //reset
+ reset(count_delay);
+ //Sending pulse
+ digitalWrite(RCLK_Pin, LOW);
+ digitalWrite(SRCLK_Pin, LOW);
+ digitalWrite(SER_Pin, 1);
+ digitalWrite(SRCLK_Pin, HIGH);
+ digitalWrite(RCLK_Pin, HIGH);
+ // Pushed one bit, so atleast one node:
+ *nodes = 1;
+
+ do {
+ delay (count_delay);
+ digitalWrite(RCLK_Pin, LOW);
+ digitalWrite(SRCLK_Pin, LOW);
+ digitalWrite(SER_Pin, (*nodes)%RE_PULSE == 0);
+ digitalWrite(SRCLK_Pin, HIGH);
+ digitalWrite(RCLK_Pin, HIGH);
+ (*nodes)++;
+ } while (int(bkval = digitalRead(BKTRK_Pin)) == 0);
+}
- writeRegisters(); //MUST BE CALLED TO DISPLAY CHANGES
- writeRegisters();
- //Only call once after the values are set how you need.
-}
+void loop(){
+ int nodes = 0;
+ countNodes(0, &nodes);
+ if (Serial.available() > 0) {
+ Serial.flush();
+ printf("%u nodes counted\n", nodes);
+ }
+ }

0 comments on commit 7d4643d

Please sign in to comment.