@ishootstuff: Nice. There are however two things I do not "like":
* Using platform dependent types (like [unsigned) int; it is 16 bits on e.g. the ATmega328 processors in the "classical" Arduinos (Uno, Nano, Mega), 32 bits in the more modern 32-bit processors). This leads to a program that behaves differently on different platforms
* Harvard-architecture processors like the ones in the Arduinos have "small" RAM memory and "large" flash (read-only) memory. Therefore it is a bad idea to use the former for static stuff as your data arrays. Using the standard tools or the Arduino, you can force the program to use read-only memory for this data, using the attribute PROGMEM, and some associated functions. It is not quite straight forward however. (See the sketch below.)
IrScrutinizer can generate this sort of code automatically; just "export" using the export format "Arduino Raw". The enclosed sketch supports not only the traditional IRremote, but also my Arduino IR library, Infrared4Arduino.
Code: Select all
// This Arduino sketch was automatically generated by IrScrutinizer.
// It supports:
//
// * IRremote https://github.com/z3t0/Arduino-IRremote
// http://z3t0.github.io/Arduino-IRremote/
// * IRLib https://github.com/cyborg5/IRLib
// * Infrared4Arduino https://github.com/bengtmartensson/Infrared4Arduino,
// http://www.harctoolbox.org/Infrared4Arduino.html (planned)
// For problems, bugs, and suggestions, please open an issue at
// https://github.com/bengtmartensson/harctoolboxbundle/issues
// This file contains C identifiers which have been translated from command names.
// Define exactly one of these
// #define IRREMOTE
// #define IRLIB
#define INFRARED4ARDUINO
/////////////////////////////////////////////////////
// Sanity check
#if defined(IRREMOTE) & ( defined(IRLIB) | defined(INFRARED4ARDUINO) )
#error Must define only one of IRREMOTE, IRLIB, or INFRARED4ARDUINO
#endif
#if defined(IRLIB) & ( defined(IRREMOTE) | defined(INFRARED4ARDUINO) )
#error Must define only one of IRREMOTE, IRLIB, or INFRARED4ARDUINO
#endif
#if defined(INFRARED4ARDUINO) & ( defined(IRLIB) | defined(IRREMOTE) )
#error Must define only one of IRREMOTE, IRLIB, or INFRARED4ARDUINO
#endif
#ifdef IRREMOTE
#include <IRremote.h>
#elif defined(IRLIB)
#include <IRLib.h>
#define sendRaw send
#elif defined(INFRARED4ARDUINO)
#include <IrSenderPwm.h>
#else
#error Must defined one of IRREMOTE, IRLIB, or INFRARED4ARDUINO
#endif
#if defined(IRREMOTE) | defined(IRLIB)
typedef uint16_t microseconds_t; // Change if desired
typedef uint16_t frequency_t; // Change if desired
static inline unsigned hz2khz(frequency_t f) { return f/1000U; }
#endif
// Constants
static const long BAUD = 115200UL; // Change if desired
// Global variables
#ifdef IRREMOTE
IRsend irsend;
#elif defined(IRLIB)
IRsendRaw irsend;
#else
IrSender *irsend = IrSenderPwm::getInstance(true);
#endif
// Command #1: off
// Protocol: gwts, Parameters: CRC=166 F=96
const microseconds_t intro_off[] PROGMEM = { 2085U, 417U, 834U, 834U, 2502U, 834U, 417U, 417U, 834U, 834U, 834U, 417U, 417U, 834 };
// Command #2: blue
// Protocol: gwts, Parameters: CRC=248 F=97
const microseconds_t intro_blue[] PROGMEM = { 2085U, 417U, 834U, 834U, 417U, 417U, 1668U, 834U, 417U, 417U, 1668U, 2502 };
// Command #3: green
// Protocol: gwts, Parameters: CRC=26 F=98
const microseconds_t intro_green[] PROGMEM = { 2085U, 417U, 834U, 834U, 834U, 417U, 1251U, 834U, 417U, 417U, 834U, 417U, 417U, 834U, 1251U, 417 };
// Command #4: cyan
// Protocol: gwts, Parameters: CRC=68 F=99
const microseconds_t intro_cyan[] PROGMEM = { 2085U, 417U, 834U, 834U, 417U, 834U, 1251U, 834U, 417U, 417U, 1251U, 417U, 1251U, 417U, 417U, 417 };
// Command #5: red
// Protocol: gwts, Parameters: CRC=199 F=100
const microseconds_t intro_red[] PROGMEM = { 2085U, 417U, 834U, 834U, 1251U, 417U, 834U, 834U, 417U, 417U, 417U, 1251U, 1251U, 1251 };
// Command #6: magenta
// Protocol: gwts, Parameters: CRC=153 F=101
const microseconds_t intro_magenta[] PROGMEM = { 2085U, 417U, 834U, 834U, 417U, 417U, 417U, 417U, 834U, 834U, 417U, 417U, 417U, 417U, 834U, 834U, 834U, 834 };
// Command #7: yellow
// Protocol: gwts, Parameters: CRC=123 F=102
const microseconds_t intro_yellow[] PROGMEM = { 2085U, 417U, 834U, 834U, 834U, 834U, 834U, 834U, 417U, 417U, 417U, 834U, 417U, 1668U, 417U, 417 };
// Command #8: white
// Protocol: gwts, Parameters: CRC=37 F=103
const microseconds_t intro_white[] PROGMEM = { 2085U, 417U, 834U, 834U, 417U, 1251U, 834U, 834U, 417U, 417U, 417U, 417U, 417U, 417U, 834U, 417U, 834U, 417 };
// Command #9: off_r
// Protocol: gwts, Parameters: CRC=100 F=104
const microseconds_t intro_off_r[] PROGMEM = { 2085U, 417U, 834U, 834U, 1668U, 417U, 417U, 834U, 417U, 417U, 1251U, 417U, 834U, 834U, 417U, 417 };
// Command #10: blue_r
// Protocol: gwts, Parameters: CRC=58 F=105
const microseconds_t intro_blue_r[] PROGMEM = { 2085U, 417U, 834U, 834U, 417U, 417U, 834U, 417U, 417U, 834U, 417U, 417U, 834U, 417U, 417U, 1251U, 834U, 417 };
// Command #11: green_r
// Protocol: gwts, Parameters: CRC=216 F=106
const microseconds_t intro_green_r[] PROGMEM = { 2085U, 417U, 834U, 834U, 834U, 417U, 417U, 417U, 417U, 834U, 417U, 417U, 1668U, 834U, 417U, 1251 };
// Command #12: cyan_r
// Protocol: gwts, Parameters: CRC=134 F=107
const microseconds_t intro_cyan_r[] PROGMEM = { 2085U, 417U, 834U, 834U, 417U, 834U, 417U, 417U, 417U, 834U, 417U, 417U, 834U, 834U, 1668U, 834 };
// Command #13: red_r
// Protocol: gwts, Parameters: CRC=5 F=108
const microseconds_t intro_red_r[] PROGMEM = { 2085U, 417U, 834U, 834U, 1251U, 834U, 417U, 834U, 417U, 417U, 417U, 417U, 417U, 417U, 2085U, 417 };
// Command #14: magenta_r
// Protocol: gwts, Parameters: CRC=91 F=109
const microseconds_t intro_magenta_r[] PROGMEM = { 2085U, 417U, 834U, 834U, 417U, 417U, 417U, 834U, 417U, 834U, 417U, 417U, 417U, 834U, 417U, 834U, 417U, 417U, 417U, 417 };
// Command #15: yellow_r
// Protocol: gwts, Parameters: CRC=185 F=110
const microseconds_t intro_yellow_r[] PROGMEM = { 2085U, 417U, 834U, 834U, 834U, 1251U, 417U, 834U, 417U, 417U, 417U, 417U, 834U, 1251U, 417U, 834 };
// Command #16: white_r
// Protocol: gwts, Parameters: CRC=231 F=111
const microseconds_t intro_white_r[] PROGMEM = { 2085U, 417U, 834U, 834U, 417U, 1668U, 417U, 834U, 417U, 417U, 417U, 1251U, 834U, 1668 };
static void sendRaw(const microseconds_t intro_P[], size_t lengthIntro, const microseconds_t repeat_P[],
size_t lengthRepeat, frequency_t frequency, unsigned times) {
microseconds_t intro[lengthIntro];
microseconds_t repeat[lengthRepeat];
memcpy_PF(intro, (uint_farptr_t) intro_P, sizeof(microseconds_t) * lengthIntro);
memcpy_PF(repeat, (uint_farptr_t) repeat_P, sizeof(microseconds_t) * lengthRepeat);
#if defined(IRREMOTE) | defined(IRLIB)
if (lengthIntro > 0U)
irsend.sendRaw(intro, lengthIntro, hz2khz(frequency));
if (lengthRepeat > 0U)
for (unsigned i = 0U; i < times - (lengthIntro > 0U); i++)
irsend.sendRaw(repeat, lengthRepeat, hz2khz(frequency));
#else // INFRARED4ARDUINO
IrSignal irSignal(intro, lengthIntro, repeat, lengthRepeat, NULL, 0U, frequency);
irsend->sendIrSignal(irSignal, times);
#endif
}
void setup() {
Serial.begin(BAUD);
Serial.setTimeout(60000UL);
}
// A pretty silly main loop; just intended as an example.
void loop() {
Serial.println(F("Enter number of signal to send (1 .. 16)"));
long commandno = Serial.parseInt();
Serial.println(F("Enter number of times to send it"));
long times = Serial.parseInt();
switch (commandno) {
case 1U:
sendRaw(intro_off, 14U, NULL, 0U, 38005U, times);
break;
case 2U:
sendRaw(intro_blue, 12U, NULL, 0U, 38005U, times);
break;
case 3U:
sendRaw(intro_green, 16U, NULL, 0U, 38005U, times);
break;
case 4U:
sendRaw(intro_cyan, 16U, NULL, 0U, 38005U, times);
break;
case 5U:
sendRaw(intro_red, 14U, NULL, 0U, 38005U, times);
break;
case 6U:
sendRaw(intro_magenta, 18U, NULL, 0U, 38005U, times);
break;
case 7U:
sendRaw(intro_yellow, 16U, NULL, 0U, 38005U, times);
break;
case 8U:
sendRaw(intro_white, 18U, NULL, 0U, 38005U, times);
break;
case 9U:
sendRaw(intro_off_r, 16U, NULL, 0U, 38005U, times);
break;
case 10U:
sendRaw(intro_blue_r, 18U, NULL, 0U, 38005U, times);
break;
case 11U:
sendRaw(intro_green_r, 16U, NULL, 0U, 38005U, times);
break;
case 12U:
sendRaw(intro_cyan_r, 16U, NULL, 0U, 38005U, times);
break;
case 13U:
sendRaw(intro_red_r, 16U, NULL, 0U, 38005U, times);
break;
case 14U:
sendRaw(intro_magenta_r, 20U, NULL, 0U, 38005U, times);
break;
case 15U:
sendRaw(intro_yellow_r, 16U, NULL, 0U, 38005U, times);
break;
case 16U:
sendRaw(intro_white_r, 14U, NULL, 0U, 38005U, times);
break;
default:
Serial.println(F("Invalid number entered, try again"));
break;
}
}