My favorites
▼
|
Sign in
makezine
Sample code from the MAKE magazine blog
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
arduino
/
EnoughAlready
/
EnoughAlready.pde
‹r42
r45
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
Enough Already
Matt Richardson, 2011
Monitors the NTSC closed captioning text track and mutes the TV when a keyword is caught.
More information about this project is here:
http://blog.makezine.com/archive/2011/08/enough-already-the-arduino-solution-to-overexposed-celebs.html
This code is mostly from Nootropic Design's Video Experimenter Shield
closed captioning example:
http://nootropicdesign.com/projectlab/2011/03/20/decoding-closed-captioning/
And Ladyada's IR tutorial:
http://www.ladyada.net/learn/sensors/ir.html
This code is meant to accompany the video posted to MAKE and in most cases won't
work "out of the box." You may need to make adjustments to the code to get it work
in your set up.
It requires an Arduino with a Video Experimenter Shield, and an IR LED (pin 13)
*/
#include <TVout.h>
#include <fontALL.h>
#include <pollserial.h>
#define W 128
#define H 96
#define BITWIDTH 5
#define THRESHOLD 3
TVout tv;
pollserial pserial;
unsigned char x,y;
char s[32];
int start = 40;
unsigned char ccdata[16];
// TiVo, DVD player
byte bpos[][8] = {{26, 32, 38, 45, 51, 58, 64, 70}, {78, 83, 89, 96, 102, 109, 115, 121}};
// VCR
//byte bpos[][8] = {{27, 33, 40, 46, 52, 59, 65, 72}, {78, 84, 91, 97, 103, 110, 116, 123}};
boolean wroteOutput = false;
boolean newline = false;
char c[2];
char lastControlCode[2];
int line = 14;
int dataCaptureStart = 310;
unsigned int loopCount = 0;
String textLine = "";
// Add Keywords here and adjust the count of keywords:
const byte NUMBER_OF_WORDS = 4;
String keyWords[NUMBER_OF_WORDS] = {
"PALIN", "TRUMP", "KARDASHIAN", "JEFFS",
};
int IRledPin = 13;
unsigned long muteUntil = 0;
boolean muted = false;
long muteTime = 30000; // time, in milliseconds to mute
void setup() {
tv.begin(_NTSC, W, H);
tv.set_hbi_hook(pserial.begin(57600));
initOverlay();
initInputProcessing();
tv.setDataCapture(line, dataCaptureStart, ccdata);
y = 0;
tv.select_font(font6x8);
tv.fill(0);
// uncomment this to display the bit positions on the screen
// for alignment/debugging
displayBitPositions();
}
void initOverlay() {
TCCR1A = 0;
// Enable timer1. ICES0 is set to 0 for falling edge detection on input capture pin.
TCCR1B = _BV(CS10);
// Enable input capture interrupt
TIMSK1 |= _BV(ICIE1);
// Enable external interrupt INT0 on pin 2 with falling edge.
EIMSK = _BV(INT0);
EICRA = _BV(ISC11);
}
void initInputProcessing() {
// Analog Comparator setup
ADCSRA &= ~_BV(ADEN); // disable ADC
ADCSRB |= _BV(ACME); // enable ADC multiplexer
ADMUX &= ~_BV(MUX0); // select A2 for use as AIN1 (negative voltage of comparator)
ADMUX |= _BV(MUX1);
ADMUX &= ~_BV(MUX2);
ACSR &= ~_BV(ACIE); // disable analog comparator interrupts
ACSR &= ~_BV(ACIC); // disable analog comparator input capture
}
ISR(INT0_vect) {
display.scanLine = 0;
wroteOutput = false;
for(x=0;x<display.hres;x++) {
ccdata[x] = 0;
}
}
// display the captured data line on the screen
void displayccdata() {
y = 0;
for(x=0;x<display.hres;x++) {
display.screen[(y*display.hres)+x] = ccdata[x];
}
}
void loop() {
byte pxsum;
byte i;
byte parityCount;
loopCount++;
// Use this code to adjust the data capture line using a
// potentiometer connected to A5.
// The default value is line 13.
/*
if (loopCount % 1000 == 0) {
line = getValue();
line = (line / 10) - 1;
displayValue(line);
tv.setDataCapture(line, dataCaptureStart, ccdata);
}
*/
// Use this code to adjust the data capture start time using a
// potentiometer connected to A5.
// The default value is 310
/*
if (loopCount % 1000 == 0) {
dataCaptureStart = getValue();
displayValue(dataCaptureStart);
tv.setDataCapture(line, dataCaptureStart, ccdata);
}
*/
// Display the captured data line to the screen so we can see it.
displayccdata();
if ((ccdata[0] > 0) && (!wroteOutput)) {
// we have new data to decode
for(byte bytenum=0;bytenum<2;bytenum++) {
c[bytenum] = 0;
parityCount = 0;
for(int bit=0;bit<8;bit++) {
pxsum = 0;
for(int w=0;w<BITWIDTH;w++) {
i = bpos[bytenum][bit]+w;
if (((ccdata[i/8] >> (7 - (i%8))) & 1) == 1) {
pxsum++;
}
}
if (pxsum >= THRESHOLD) {
// consider the bit to be "on"
c[bytenum] |= (1 << bit);
parityCount++;
}
}
if ((parityCount % 2) == 1) {
// parity check matches
// strip off the MSB because it's the parity bit
c[bytenum] &= 0x7F;
} else {
// parity check failed
c[bytenum] = 0;
}
}
// output the data
if ((c[0] > 0) && (c[0] < ' ')) {
// control character
if ((c[0] != lastControlCode[0]) && (c[1] != lastControlCode[1])) {
if (!newline) {
pserial.write('\r');
pserial.write('\n');
for (int i = 0; i < NUMBER_OF_WORDS; i++)
{
if (textLine.indexOf(keyWords[i]) != -1){ // if we catch a keyword
pserial.write("Keyword Detected, muting for 30 sec\n");
muteUntil = millis() + muteTime; // store when we can unmute
if (!muted)
SendMute();
muted = true;
}
}
textLine = "";
newline = true;
}
lastControlCode[0] = c[0];
lastControlCode[1] = c[1];
}
} else {
if (c[0] > 0) {
newline = false;
lastControlCode[0] = 0;
pserial.write(c[0]);
textLine += c[0];
//tv.print(c[0]);
}
if (c[1] > 0) {
newline = false;
lastControlCode[0] = 0;
pserial.write(c[1]);
textLine += c[1];
//tv.print(c[1]);
}
}
wroteOutput = true;
}
if ((muted) && (millis() > muteUntil)) // to do: handle rollover of millis()
{
SendMute();
muted=false;
pserial.write("No Keyword for 30 seconds, unmuting.\n");
}
}
int getValue() {
int value;
ADCSRA |= _BV(ADEN); // enable ADC
value = analogRead(5);
initInputProcessing();
return value;
}
void displayValue(int v) {
tv.print(0, 3, " ");
sprintf(s, "%i", v);
tv.print(0, 3, s);
}
// for alignment debugging
void displayTicks() {
y = 2;
for(x=0;x<W;x++) {
if ((x % 2) == 0) {
tv.set_pixel(x, y, 1);
}
}
y = 3;
for(x=0;x<W;x++) {
if ((x % 5) == 0) {
tv.set_pixel(x, y, 1);
}
}
y = 4;
for(x=0;x<W;x++) {
if ((x % 10) == 0) {
tv.set_pixel(x, y, 1);
}
}
}
void displayBitPositions() {
y = 1;
tv.draw_line(0, y, W-1, y, 0);
for(byte bytenum=0;bytenum<2;bytenum++) {
for(byte bit=0;bit<8;bit++) {
tv.set_pixel(bpos[bytenum][bit], y, 1);
}
}
}
// This procedure sends a 38KHz pulse to the IRledPin
// for a certain # of microseconds. We'll use this whenever we need to send codes
void pulseIR(long microsecs) {
// we'll count down from the number of microseconds we are told to wait
cli(); // this turns off any background interrupts
while (microsecs > 0) {
// 38 kHz is about 13 microseconds high and 13 microseconds low
digitalWrite(IRledPin, HIGH); // this takes about 3 microseconds to happen
delayMicroseconds(10); // hang out for 10 microseconds
digitalWrite(IRledPin, LOW); // this also takes about 3 microseconds
delayMicroseconds(10); // hang out for 10 microseconds
// so 26 microseconds altogether
microsecs -= 26;
}
sei(); // this turns them back on
}
void SendMute() { //This IR code is for a SHARP AQUOS. Use Ladyada's IR tutorial for your TV:
TIMSK1 &= ~(_BV(TOIE1)); // Disable inturrupts
TIMSK1 &= ~(_BV(ICIE1));
pulseIR(300);
delayMicroseconds(1740);
pulseIR(320);
delayMicroseconds(700);
pulseIR(320);
delayMicroseconds(700);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(1760);
pulseIR(300);
delayMicroseconds(1740);
pulseIR(300);
delayMicroseconds(1740);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(1760);
pulseIR(300);
delayMicroseconds(700);
pulseIR(320);
delayMicroseconds(700);
pulseIR(320);
delayMicroseconds(700);
pulseIR(320);
delayMicroseconds(1740);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delay(44); // wait 44 milliseconds
pulseIR(300);
delayMicroseconds(1740);
pulseIR(320);
delayMicroseconds(700);
pulseIR(320);
delayMicroseconds(700);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(1760);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(1740);
pulseIR(320);
delayMicroseconds(1740);
pulseIR(320);
delayMicroseconds(1740);
pulseIR(320);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(1760);
pulseIR(300);
delay(44); // wait 44 milliseconds before sending it again
pulseIR(300);
delayMicroseconds(1740);
pulseIR(320);
delayMicroseconds(700);
pulseIR(320);
delayMicroseconds(700);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(1760);
pulseIR(300);
delayMicroseconds(1740);
pulseIR(300);
delayMicroseconds(1740);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(1760);
pulseIR(300);
delayMicroseconds(700);
pulseIR(320);
delayMicroseconds(700);
pulseIR(320);
delayMicroseconds(700);
pulseIR(320);
delayMicroseconds(1740);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delay(44); // wait 44 milliseconds
pulseIR(300);
delayMicroseconds(1740);
pulseIR(320);
delayMicroseconds(700);
pulseIR(320);
delayMicroseconds(700);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(1760);
pulseIR(300);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(1740);
pulseIR(320);
delayMicroseconds(1740);
pulseIR(320);
delayMicroseconds(1740);
pulseIR(320);
delayMicroseconds(720);
pulseIR(300);
delayMicroseconds(1760);
pulseIR(300);
TIMSK1 |= _BV(TOIE1); // Enable inturrupts
TIMSK1 |= _BV(ICIE1);
}
Show details
Hide details
Change log
r43
by mrichardson23 on Sep 2, 2011
Diff
Fix muteTime declaration
Go to:
.../EnoughAlready/EnoughAlready.pde
Project members,
sign in
to write a code review
Older revisions
r42
by mrichardson23 on Aug 18, 2011
Diff
Adding blog post URL
r41
by mrichardson23 on Aug 16, 2011
Diff
Initial commit
All revisions of this file
File info
Size: 10357 bytes, 437 lines
View raw file
Powered by
Google Project Hosting