abloom / arduino_messaging

Random experiments with my Ardunio and some Nixie tubes

This URL has Read+Write access

arduino_messaging / led_dimmer.pde
100644 166 lines (136 sloc) 4.021 kb
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdlib.h>
#include <string.h>
#include <Nixie.h>
#include <MultiDAC.h>
 
// message types
#define NO_MESSAGE_ID '0'
#define DEMO_ID '1'
#define MESSAGE_ID '3'
#define ZERO_MESSAGE_ID '4'
#define CLEAR_ID '2'
#define BAR_GRAPH_ID '5'
 
#define MESSAGE_SIZE 2
#define DEMO_DELAY 175
#define DEMO_DELAY_2 20
 
#define numDigits 2
 
#define nixieDataPin 2 // data line or SER
#define nixieClockPin 3 // clock pin or SCK
#define nixieLatchPin 4 // latch pin or RCK
 
#define barGraphDataPin 5 // data line or SER
#define barGraphClockPin 6 // clock pin or SCK
#define barGraphLatchPin 7 // latch pin or RCK
 
Nixie nixie(nixieDataPin, nixieClockPin, nixieLatchPin);
MultiDAC barGraph(barGraphDataPin, barGraphClockPin, barGraphLatchPin);
 
char message_type = DEMO_ID;
 
int demo_count = 0;
int demo_direction = 1;
unsigned long demo_timer = 0;
 
int demo_count_2 = 0;
int demo_direction_2 = 1;
unsigned long demo_timer_2 = 0;
 
void setup() {
  Serial.begin(9600);
  Serial.println("ready");
  
  nixie.clear(numDigits);
  barGraph.clear(1);
}
 
void loop() {
  if (Serial.available() > 0) {
    // new message
    message_type = Serial.read();
    
    Serial.print("cmd: ");
    Serial.println(message_type);
  }
  
  // process message
  switch(message_type) {
    case NO_MESSAGE_ID: // NOOP
      break;
    case DEMO_ID: // keep the demo running, no NOOP
      run_demo();
      break;
    case ZERO_MESSAGE_ID: // recieve a message and then NOOP
    case MESSAGE_ID:
      recieve_message();
      message_type = NO_MESSAGE_ID;
      break;
    case CLEAR_ID: // clear the tubes and then NOOP
      nixie.clear(numDigits);
      message_type = NO_MESSAGE_ID;
      break;
    case BAR_GRAPH_ID: // write a value to the bar graph
      recieve_bargraph_message();
      message_type = NO_MESSAGE_ID;
      break;
    default: // report the error and then NOOP
      Serial.print("unknown message_type: ");
      Serial.println(message_type);
      message_type = NO_MESSAGE_ID;
      break;
  }
}
 
void run_demo() {
  unsigned long timer = millis();
  
  if (timer >= (demo_timer_2 + DEMO_DELAY_2)) {
    demo_timer_2 = timer;
    barGraph.writeValue(1, demo_count_2);
    
    demo_count_2 += demo_direction_2;
    if ((demo_count_2 >= 170) || (demo_count_2 < 1))
      demo_direction_2 *= -1;
  }
  
  // only swap digits if weve waited 250ms
  if (timer >= (demo_timer + DEMO_DELAY)) {
    demo_timer = timer;
 
    if (demo_direction < 0) { // right justified
      nixie.writeNumTrim(demo_count, numDigits);
    } else { // left justified with no shift
      nixie.clear(numDigits);
      nixie.writeNumLeft(demo_count);
    }
    
    // reverse the direction if we've left the bounds (0-9)
    demo_count += demo_direction;
    if ((demo_count >= 9) || (demo_count < 0))
      demo_direction *= -1;
  }
}
 
void recieve_bargraph_message() {
  char msg[2];
  int graph = 0;
  int percent = 0;
  int value = 0;
  
  while (Serial.available() < 3)
    delay(10);
  
  graph = Serial.read() - 48;
  
  for(int i = 0; i < 2; i++)
    msg[i] = Serial.read();
    
  percent = atoi(msg);
  value = (int) (((float) percent / 99.0) * 254.0);
  Serial.print("Graph: ");
  Serial.print(graph);
  Serial.print(" percent: ");
  Serial.print(percent);
  Serial.print(" value: ");
  Serial.println(value);
  barGraph.writeValue(graph, value);
}
 
void recieve_message() {
  char msg[MESSAGE_SIZE];
  
  // sleep until the buffer is full
  while (Serial.available() < MESSAGE_SIZE)
    delay(10);
    
  // collect the message
  for(int i = 0; i < MESSAGE_SIZE; i++)
    msg[i] = Serial.read();
      
  // convert and update the tubes
  int val = atoi(msg);
  switch (message_type) {
    case ZERO_MESSAGE_ID: // right justified with 0 padding
      nixie.writeNumZero(val, MESSAGE_SIZE);
      break;
    case MESSAGE_ID: // right justified with blank padding
      nixie.writeNumTrim(val, MESSAGE_SIZE);
      break;
    default: // left justified (continue to shift)
      nixie.writeNumLeft(val);
      break;
  }
}