Wow PaulS Thanks! That was a really constructive reply! You should win some sort of award for it!
#include <MeetAndroid.h>
#include <SPI.h>
#include "ircodes.h"
/* SPI Defines */
#define DATAIN 50 //MISO
#define DATAOUT 51 //MOSI
#define SPICLOCK 52 //SCK
#define SLAVESELECT 53 //ss
#define IRpin_PIN PIND
#define IRpin 2
// the maximum pulse we'll listen for - 65 milliseconds is a long time
#define MAXPULSE 9000
// what our timing resolution should be, larger is better
// as its more 'precise' - but too large and you wont get
// accurate timing
#define RESOLUTION 20
// What percent we will allow in variation to match the same code
#define FUZZINESS 20
// we will store up to 100 pulse pairs (this is -a lot-)
uint16_t pulses[100][2]; // pair is high and low pulse
uint8_t currentpulse = 0; // index for pulses we're storing
// declare MeetAndroid so that you can call functions with it
MeetAndroid meetAndroid;
// we need 3 PWM pins to control the leds
int redLed = 9;
int greenLed = 10;
int blueLed = 11;
void setup()
{
// use the baud rate your bluetooth module is configured to
// not all baud rates are working well, i.e. ATMEGA168 works best with 57600
Serial.begin(57600);
// register callback functions, which will be called when an associated event occurs.
meetAndroid.registerFunction(red, 'o');
meetAndroid.registerFunction(green, 'p');
meetAndroid.registerFunction(blue, 'q');
meetAndroid.registerFunction(TVb, 'r');
meetAndroid.registerFunction(MP3b, 's');
meetAndroid.registerFunction(AUX1b, 't');
meetAndroid.registerFunction(AUX2b, 'u');
// set all color leds as output pins
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
// just set all leds to high so that we see they are working well
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, HIGH);
digitalWrite(blueLed, HIGH);
pinMode(SLAVESELECT,OUTPUT);
SPI.setDataMode(0);
SPI.setBitOrder(MSBFIRST);
SPI.begin();
//Serial.begin(9600);
}
void loop()
{
// you need to keep this in your loop() to receive events
meetAndroid.receive();
/* IR code */
ReadIR();
}
/*
* Whenever the multicolor lamp app changes the red value
* this function will be called
*/
void red(byte flag, byte numOfValues)
{
analogWrite(redLed, meetAndroid.getInt());
}
/*
* Whenever the multicolor lamp app changes the green value
* this function will be called
*/
void green(byte flag, byte numOfValues)
{
analogWrite(greenLed, meetAndroid.getInt());
}
/*
* Whenever the multicolor lamp app changes the blue value
* this function will be called
*/
void blue(byte flag, byte numOfValues)
{
analogWrite(blueLed, meetAndroid.getInt());
}
void TVb(byte flag, byte numOfValues)
{
if(meetAndroid.getInt())
{
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(0x01);
digitalWrite(SLAVESELECT,HIGH);
delay(10);
Serial.println("BUT1");
}
}
void MP3b(byte flag, byte numOfValues)
{
if(meetAndroid.getInt()){
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(0x02);
digitalWrite(SLAVESELECT,HIGH);
delay(10);
Serial.println("BUT2");
}
}
void AUX1b(byte flag, byte numOfValues)
{
if(meetAndroid.getInt()){
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(0x04);
digitalWrite(SLAVESELECT,HIGH);
delay(10);
Serial.println("BUT3");
}
}
void AUX2b(byte flag, byte numOfValues)
{
if(meetAndroid.getInt()){
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(0x08);
digitalWrite(SLAVESELECT,HIGH);
delay(10);
Serial.println("BUT4");
}
}
/* ----- IR Functions -------*/
boolean IRcompare(int numpulses, int Signal[]) {
for (int i=0; i< numpulses-1; i++) {
int oncode = pulses[i][1] * RESOLUTION / 10;
int offcode = pulses[i+1][0] * RESOLUTION / 10;
/*
Serial.print(oncode); // the ON signal we heard
Serial.print(" - ");
Serial.print(Signal[i*2 + 0]); // the ON signal we want
*/
// check to make sure the error is less than FUZZINESS percent
if ( abs(oncode - Signal[i*2 + 0]) <= (Signal[i*2 + 0] * FUZZINESS / 100)) {
//Serial.print(" (ok)");
} else {
//Serial.print(" (x)");
// we didn't match perfectly, return a false match
return false;
}
/*
Serial.print(" \t"); // tab
Serial.print(offcode); // the OFF signal we heard
Serial.print(" - ");
Serial.print(Signal[i*2 + 1]); // the OFF signal we want
*/
if ( abs(offcode - Signal[i*2 + 1]) <= (Signal[i*2 + 1] * FUZZINESS / 100)) {
//Serial.print(" (ok)");
} else {
//Serial.print(" (x)");
// we didn't match perfectly, return a false match
return false;
}
//Serial.println();
}
// Everything matched!
return true;
}
int listenForIR(void) {
currentpulse = 0;
while (1) {
uint16_t highpulse, lowpulse; // temporary storage timing
highpulse = lowpulse = 0; // start out with no pulse length
// while (digitalRead(IRpin)) { // this is too slow!
while (IRpin_PIN & (1 << IRpin)) {
// pin is still HIGH
// count off another few microseconds
highpulse++;
delayMicroseconds(RESOLUTION);
// If the pulse is too long, we 'timed out' - either nothing
// was received or the code is finished, so print what
// we've grabbed so far, and then reset
if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
return currentpulse;
}
}
// we didn't time out so lets stash the reading
pulses[currentpulse][0] = highpulse;
// same as above
while (! (IRpin_PIN & _BV(IRpin))) {
// pin is still LOW
lowpulse++;
delayMicroseconds(RESOLUTION);
if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
return currentpulse;
}
}
pulses[currentpulse][1] = lowpulse;
// we read one high-low pulse successfully, continue!
currentpulse++;
}
}
void printpulses(void) {
Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
for (uint8_t i = 0; i < currentpulse; i++) {
Serial.print(pulses[i][0] * RESOLUTION, DEC);
Serial.print(" usec, ");
Serial.print(pulses[i][1] * RESOLUTION, DEC);
Serial.println(" usec");
}
// print it in a 'array' format
Serial.println("int IRsignal[] = {");
Serial.println("// ON, OFF (in 10's of microseconds)");
for (uint8_t i = 0; i < currentpulse-1; i++) {
Serial.print("\t"); // tab
Serial.print(pulses[i][1] * RESOLUTION / 10, DEC);
Serial.print(", ");
Serial.print(pulses[i+1][0] * RESOLUTION / 10, DEC);
Serial.println(",");
}
Serial.print("\t"); // tab
Serial.print(pulses[currentpulse-1][1] * RESOLUTION / 10, DEC);
Serial.print(", 0};");
}
void ReadIR(){
int numberpulses;
numberpulses = listenForIR();
if (IRcompare(numberpulses, IRsignalPower)) {
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(0x00);
digitalWrite(SLAVESELECT,HIGH);
delay(1);
}
if (IRcompare(numberpulses, IRsignal1)) {
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(0x01);
digitalWrite(SLAVESELECT,HIGH);
delay(1);
}
if (IRcompare(numberpulses, IRsignal2)) {
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(0x02);
digitalWrite(SLAVESELECT,HIGH);
delay(1);
}
if (IRcompare(numberpulses, IRsignal3)) {
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(0x04);
digitalWrite(SLAVESELECT,HIGH);
delay(1);
}
if (IRcompare(numberpulses, IRsignal4)) {
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(0x08);
digitalWrite(SLAVESELECT,HIGH);
delay(1);
}
}