| 1 | /* Arduino TV-B-Gone |
| 2 | * |
| 3 | * A recreation of the TV-B-Gone kit on an Arduino Duemilanove. |
| 4 | * By Daedalus12, mtbf0, westfw and Pedrocrespo |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <avr/pgmspace.h> |
| 9 | |
| 10 | struct codeElement { |
| 11 | uint32_t onTime; // duration of "On" time |
| 12 | uint32_t offTime; // duration of "Off" time |
| 13 | }; |
| 14 | |
| 15 | struct powercode { |
| 16 | uint32_t freq; // frequency |
| 17 | struct codeElement codes[100]; // on/off codes, supposed to be 100 as maximum |
| 18 | }; |
| 19 | |
| 20 | int i = 0; // we use this for iterating |
| 21 | long cfreq; // we'll store each frequency here when needed |
| 22 | |
| 23 | const struct powercode sonyCode PROGMEM = { |
| 24 | 37470, |
| 25 | {{245, 60}, |
| 26 | {123, 60}, |
| 27 | {61 , 60}, |
| 28 | {123, 60}, |
| 29 | {61 , 60}, |
| 30 | {123, 60}, |
| 31 | {61 , 60}, |
| 32 | {61 , 60}, |
| 33 | {123, 60}, |
| 34 | {61 , 60}, |
| 35 | {61 , 60}, |
| 36 | {61 , 60}, |
| 37 | {61 , 2759}, |
| 38 | {245, 60}, |
| 39 | {123, 60}, |
| 40 | {61 , 60}, |
| 41 | {123, 60}, |
| 42 | {61 , 60}, |
| 43 | {123, 60}, |
| 44 | {61 , 60}, |
| 45 | {61 , 60}, |
| 46 | {123, 60}, |
| 47 | {61 , 60}, |
| 48 | {61 , 60}, |
| 49 | {61 , 60}, |
| 50 | {61 , 0}} |
| 51 | }; |
| 52 | const struct powercode panasonicCode PROGMEM = { |
| 53 | 36130, |
| 54 | {{358,179}, |
| 55 | {44, 45}, |
| 56 | {44, 135}, |
| 57 | {44, 45}, |
| 58 | {44, 45}, |
| 59 | {44, 45}, |
| 60 | {44, 45}, |
| 61 | {44, 45}, |
| 62 | {44, 45}, |
| 63 | {44, 45}, |
| 64 | {44, 45}, |
| 65 | {44, 45}, |
| 66 | {44, 45}, |
| 67 | {44, 45}, |
| 68 | {44, 135}, |
| 69 | {44, 45}, |
| 70 | {44, 45}, |
| 71 | {44, 45}, |
| 72 | {44, 45}, |
| 73 | {44, 45}, |
| 74 | {44, 45}, |
| 75 | {44, 45}, |
| 76 | {44, 45}, |
| 77 | {44, 45}, |
| 78 | {44, 135}, |
| 79 | {44, 45}, |
| 80 | {44, 45}, |
| 81 | {44, 45}, |
| 82 | {44, 45}, |
| 83 | {44, 45}, |
| 84 | {44, 45}, |
| 85 | {44, 45}, |
| 86 | {44, 45}, |
| 87 | {44, 135}, |
| 88 | {44, 45}, |
| 89 | {44, 135}, |
| 90 | {44, 135}, |
| 91 | {44, 135}, |
| 92 | {44, 135}, |
| 93 | {44, 45}, |
| 94 | {44, 45}, |
| 95 | {44, 135}, |
| 96 | {44, 45}, |
| 97 | {44, 135}, |
| 98 | {44, 135}, |
| 99 | {44, 135}, |
| 100 | {44, 135}, |
| 101 | {44, 45}, |
| 102 | {44, 135}, |
| 103 | {44, 7720}, |
| 104 | {358, 180}, |
| 105 | {44, 45}, |
| 106 | {44, 135}, |
| 107 | {44, 45}, |
| 108 | {44, 45}, |
| 109 | {44, 45}, |
| 110 | {44, 45}, |
| 111 | {44, 45}, |
| 112 | {44, 45}, |
| 113 | {44, 45}, |
| 114 | {44, 45}, |
| 115 | {44, 45}, |
| 116 | {44, 45}, |
| 117 | {44, 45}, |
| 118 | {44, 135}, |
| 119 | {44, 45}, |
| 120 | {44, 45}, |
| 121 | {44, 45}, |
| 122 | {44, 45}, |
| 123 | {44, 45}, |
| 124 | {44, 45}, |
| 125 | {44, 45}, |
| 126 | {44, 45}, |
| 127 | {44, 45}, |
| 128 | {44, 135}, |
| 129 | {44, 45}, |
| 130 | {44, 45}, |
| 131 | {44, 45}, |
| 132 | {44, 45}, |
| 133 | {44, 45}, |
| 134 | {44, 45}, |
| 135 | {44, 45}, |
| 136 | {44, 45}, |
| 137 | {44, 135}, |
| 138 | {44, 45}, |
| 139 | {44, 135}, |
| 140 | {44, 135}, |
| 141 | {44 ,135}, |
| 142 | {44 ,135}, |
| 143 | {44, 45}, |
| 144 | {44, 45}, |
| 145 | {44, 135}, |
| 146 | {44, 45}, |
| 147 | {44, 135}, |
| 148 | {44, 135}, |
| 149 | {44, 135}, |
| 150 | {44, 135}, |
| 151 | {44, 45}, |
| 152 | {44, 135}, |
| 153 | {44, 0}} |
| 154 | }; |
| 155 | |
| 156 | const struct powercode sony2Code PROGMEM = { |
| 157 | |
| 158 | 37470, |
| 159 | |
| 160 | { {245, 60}, |
| 161 | |
| 162 | {123, 60}, |
| 163 | |
| 164 | {123, 60}, |
| 165 | |
| 166 | {123, 60}, |
| 167 | |
| 168 | {123, 60}, |
| 169 | |
| 170 | {61, 60}, |
| 171 | |
| 172 | {123, 60}, |
| 173 | |
| 174 | {61, 60}, |
| 175 | |
| 176 | {123, 60}, |
| 177 | |
| 178 | {61, 60}, |
| 179 | |
| 180 | {61, 60}, |
| 181 | |
| 182 | {61, 60}, |
| 183 | |
| 184 | {61, 2636}, |
| 185 | |
| 186 | {246, 60}, |
| 187 | |
| 188 | {123, 60}, |
| 189 | |
| 190 | {123, 60}, |
| 191 | |
| 192 | {123, 60}, |
| 193 | |
| 194 | {123, 60}, |
| 195 | |
| 196 | {61, 60}, |
| 197 | |
| 198 | {123, 60}, |
| 199 | |
| 200 | {61, 60}, |
| 201 | |
| 202 | {123, 60}, |
| 203 | |
| 204 | {61, 60}, |
| 205 | |
| 206 | {61, 60}, |
| 207 | |
| 208 | {61, 60}, |
| 209 | |
| 210 | {61, 0} } |
| 211 | |
| 212 | }; |
| 213 | |
| 214 | |
| 215 | |
| 216 | const struct powercode sony3Code PROGMEM = { |
| 217 | |
| 218 | 74940, |
| 219 | |
| 220 | { {250, 63}, |
| 221 | |
| 222 | {121, 63}, |
| 223 | |
| 224 | { 60, 63}, |
| 225 | |
| 226 | {121, 63}, |
| 227 | |
| 228 | { 60, 63}, |
| 229 | |
| 230 | {121, 63}, |
| 231 | |
| 232 | { 60, 63}, |
| 233 | |
| 234 | { 60, 63}, |
| 235 | |
| 236 | {121, 63}, |
| 237 | |
| 238 | { 60, 63}, |
| 239 | |
| 240 | { 60, 63}, |
| 241 | |
| 242 | { 60, 63}, |
| 243 | |
| 244 | { 60, 2819}, |
| 245 | |
| 246 | {250, 63}, |
| 247 | |
| 248 | {121, 63}, |
| 249 | |
| 250 | { 60, 63}, |
| 251 | |
| 252 | {121, 63}, |
| 253 | |
| 254 | { 60, 63}, |
| 255 | |
| 256 | {121, 63}, |
| 257 | |
| 258 | { 60, 63}, |
| 259 | |
| 260 | { 60, 63}, |
| 261 | |
| 262 | {121, 63}, |
| 263 | |
| 264 | { 60, 63}, |
| 265 | |
| 266 | { 60, 63}, |
| 267 | |
| 268 | { 60, 63}, |
| 269 | |
| 270 | { 60, 0} |
| 271 | |
| 272 | } |
| 273 | |
| 274 | }; |
| 275 | PROGMEM const powercode *powerCodes[] = { |
| 276 | &sonyCode, &panasonicCode, &sony2Code, &sony3Code |
| 277 | }; // select the codes you like |
| 278 | |
| 279 | int num_codes = (sizeof(powerCodes)/sizeof(*powerCodes)); // count how many codes are in the list |
| 280 | |
| 281 | void setup() { |
| 282 | DDRD = _BV(DDD6); // sets Pin 6 as output |
| 283 | } |
| 284 | |
| 285 | void loop(){ |
| 286 | for(i=0;i<num_codes;i++){ //Iterate between powercodes |
| 287 | cfreq = pgm_read_word(pgm_read_word(&(powerCodes[i]))+0); // store the frequency |
| 288 | unsigned char compareVal = (F_CPU/cfreq - 1)/2; // transform it to cpu cycles |
| 289 | TCCR0A = 0; // make sure timer is off before updating OCR0A |
| 290 | OCR0A = compareVal; // update timer |
| 291 | for (int arrayStep = 0; pgm_read_word(pgm_read_word(&(powerCodes[i]))+(8*arrayStep)+8)!=0; arrayStep++){ |
| 292 | transmitCode(pgm_read_word(pgm_read_word(&(powerCodes[i]))+(8*arrayStep)+4),pgm_read_word(pgm_read_word(&(powerCodes[i]))+(8*arrayStep)+8)); // transmit the code using function described below |
| 293 | } |
| 294 | delayMicroseconds(250);// Avoid interferring a powercode and the next one if frequencies are similar |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void transmitCode(int onTime, int offTime){ |
| 299 | // transmits one element of the POWER code |
| 300 | TCNT0 = 0; // reset timer 0 |
| 301 | TCCR0A = _BV(COM0A0) | _BV(WGM01); // set up timer 0 |
| 302 | TCCR0B = _BV(CS00); // turn on timer 0 |
| 303 | delayMicroseconds(10*onTime); // wait for onTime |
| 304 | TCNT0 = 0; // stop timer |
| 305 | TCCR0A = 0; // clear register A |
| 306 | TCCR0B = 0; // clear register B |
| 307 | PORTD = ~_BV(PORTD6); // turn off the LED |
| 308 | delayMicroseconds(10*offTime); // wait for offTime |
| 309 | } |