Running Procedure from main code from a library

Ok, so I'm using an IR interface library on my arduino.
http://arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556Link

Basically, the library uses the overflow of timer2 to run itself, and since i am strapped for timers, i want to edit the library to run my own code along with the code the library runs each time the timer overflows.

I've tried editing the ISR in the library by just adding "ir_check();" which is the code i want run, but it doesn't seem to change anything at all.
I'm guessing its because my function doesnt exist in the scope of the library, but what else can i do?

i thought about sort of pasting in all the code to my main sketch, but theres two files which i dont know exactly what to do with and it would make things mighty messy.

ideally i just want to get my code to run like i hoped, but any solution or suggestions at all would be greatly appreciated (im running out of ideas fast!)

Thanks guys!

Gav

If you look in the library file you will see a file with a ".o" extension.
Delete this after changes will force the compiler to recompile it, this time with your changes in.

I believe Grumpy ment in the library folder not the library file. :slight_smile:

deed I do de :wink:

so THAT's why when i change stuff nothing happens...
i was wondering why it didnt give me any errors.
now that makes a lot more sense!

so what youre also kind of saying is that what im trying to do should work?
ill be sure to try it out tonight. (fingers crossed)

Thanks Mike and MikMo :slight_smile:

Ha, were making progress, whose up at this time?

anyway, i deleted that file and forced it to recompile but i got the out of scope error i was so expecting was the problem before.
Now what should i do?

i really dont think theres any other way to service my ir_code() routine regularly other than making it work with the ISR that the ir reciever is already using.

I'm going to muck around trying to paste in the library (dear god) but id really like a better solution...

Someone please help me out...

Yeah, i knew this would be bad.

So, pasting in...
Naturally, everything in the library that you can call seemed to be formatted:
NECIRrcv::function

so, i replaced all that with just
ir_function
so at least everything references; i think.

anyway, theres the .h file for the main code too (.cpp)
I havent edited the .h file that i can rememebr, and i dont know what to do with the things like "include NECIRrcv.h" clearly i dont need that anymore...
Well, i was fixing problems as they cropped up with a few different strategies, but now im getting an odd error...

C:\Documents and Settings\Gavin\Desktop\arduino-0011\hardware\cores\arduino/WProgram.h:13: error: default argument given for parameter 3 of 'long unsigned int pulseIn(uint8_t, uint8_t, long unsigned int)'


C:\Documents and Settings\Gavin\Desktop\arduino-0011\hardware\cores\arduino/WProgram.h:13: error: after previous specification in 'long unsigned int pulseIn(uint8_t, uint8_t, long unsigned int)'


Couldn't determine program size: C:\Documents and Settings\Gavin\Desktop\arduino-0011\hardware/tools/avr/bin/avr-size: 'C:\DOCUME~1\Gavin\LOCALS~1\Temp\build28695.tmp\New_RGB_Controller.hex': No such file

it seems to be bitching about pulseIn but funnily enough, i cant find any references to pulseIn in the code...

oh, how i hope i can get this working :-/

EDIT:
I thought it might be better to add than to repost after the triple post...

I had another idea, i rebuilt the library but i commented out the ISR, but then pasted a copy of it in my main code.
at first i was really happy because it recompiled without errors!
alas, the code is broken and i cant fix it...
id rather have errors, at least then i know stuff is broken..

Anyone have any idea why that wouldnt have worked?
i thought the idea was pure genius; but arduino didnt agree.

Gavin, this looks like the kind of compile problem that would be readily solvable if we could take a look at the sketch generating the error. Could you possibly post it?

Mikal

Really? well thats good.
although i keep hoping for someone to go "oh, just do this"
wishful eh?

Ill post it up in a few hours when i get back home.
Although, it spans a couple of tabs now and is pretty epic...

Thanks mikalhart, any help is appreciated!

You might consider adding to the library a callback function that is normally empty and that, if it is assigned, the library calls in it's timer routine. Then you can keep your code in your own files and hook into the library routine by assigning a function pointer to the callback.

This would likely be a lot easier to set up since it involves only a minimal amount of editing of the library (maybe 5 lines of code).

A Callback function?

You've intrigued me DaveK.
It sounds essentially like what i wanted to do in the first place.
How would i go about accomplishing such a task exactly?

I could elaborate more on what I'm thinking, but i will probably only expose my limited knowledge.
this function pointer you speak of sounds to be important to me though.

Due to (un)popular demand; my (horrible) code!

I started over with the pasting in of files, and documented things i changed, so it should be easier to follow.
theres 4 tabs for the code, which i will now rattle off...

RGB_Controller

// #include <NECIRrcv.h> not included anymore

//Assignments
#define red_out 9
#define green_out 5
#define blue_out 6
#define red_in 0
#define green_in 1
#define blue_in 2
#define speakerpin 4
#define btn1 2
#define ir_pin 12
#define led 13
#define max_state 5

//Logitech Remote Codes
#define Test 4194694920
#define Input 4127848200
#define Effect 3793614600
#define Settings 3760191240
#define Power 4010866440
#define Sub_up 4228118280
#define Sub_down 4261541640
#define Centre_up 4244829960
#define Centre_down 4177983240
#define Surround_up 4278253320
#define Surround_down 4211406600
#define Volume_up 3843749640
#define Volume_down 4044289800
#define Mute 3910596360

//Fade Variables
float redinc, greeninc, blueinc;
float floattemp;

//Global Variables
int state = 0;
int delaytime = 0;
int delaytime2 = 0;
int increment = 0;
unsigned long ir_code;

   //NECIRrcv ir(ir_pin); was here

void setup()
{ 
  ir_set(ir_pin);  //moved set to here
  
  //Reset PWM outputs
  analogWrite(red_out, 255);
  analogWrite(green_out, 255);
  analogWrite(blue_out, 255);

  //Set Pinmodes
  pinMode(red_out, OUTPUT);      
  pinMode(green_out, OUTPUT); 
  pinMode(blue_out, OUTPUT); 
  pinMode(speakerpin, OUTPUT);
  pinMode(btn1, INPUT);
  
  //Setup IR, Serial, Interrupts, Timers
  ir_begin();
  Serial.begin(19200);
  attachInterrupt(0, btn1_press, RISING);

  //Startup Sequence
  play_tone(1915, 70);
  play_tone(1136, 70);
  play_tone(800, 70);
  play_tone(400, 100);
  fade_pot(1, 0);
  state = 0;
  toggle_pin(led);
}

void loop()
{
  Serial.println(state);
  ir_check();
  
  switch (state){
    case -2:
      set_rgb(0,0,0);
      break;
    case -1:
     if ((analogRead(red_out) > 0) 
     || (analogRead(green_out) > 0) 
     || (analogRead(blue_out) > 0))
       fade_pot(-1,0);
     break;
    case 0:
      manual_mix();
      break;
    case 1:
      effect_1();
      break;
    case 2:
      effect_2();
      break;  
     case 3:
      effect_3();
      break;   
     case 4:
      effect_4();
      break; 
     case 5:
      effect_5();
      break;   
  }
}

void logWrite(int colour,int pwmval)   //adapted version of analogWrite
{
  if (pwmval < 0)
    analogWrite(colour, 255);
  else if (pwmval > 255)
    analogWrite(colour, 0);
  else
    analogWrite(colour, 255-pwmval);
}

void set_rgb(int red, int green, int blue){
    logWrite(red_out, red);
    logWrite(green_out, green);
    logWrite(blue_out, blue);
}

void toggle_pin(int pin){
    digitalWrite(pin, !digitalRead(pin));
}

void play_tone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerpin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerpin, LOW);
    delayMicroseconds(tone);
  }
}

void btn1_press(){
  state_inc(true);
  kill_effects();
}

void state_inc(boolean up){
  if (up == true){
    if (state != max_state){
      beep_std();
      state++;
    }else{
      beep_high();
    }
  } else {
    if (state != 0){
      beep_std();
      state--;
    }else{
      beep_low();
    }
  }
}

void beep_std(){
  play_tone(1000, 20);
}

void beep_low(){
  play_tone(1800, 20);
}

void beep_high(){
  play_tone(700, 20);
}

void ir_check(){ 
  if (ir_available()){
    ir_code = ir_read();
    switch (ir_code){
     case Power:
        if (state == -1){
          beep_low();
          state = -2;   
        }else{
          beep_high();
          beep_low();
          state = -1;
        }
        break;
      case Volume_up:
        state_inc(true);
        break;
      case Volume_down:
        state_inc(false);
        break;          
    }
    kill_effects();
  }
}

Effects

void manual_mix(){
  set_rgb(analogRead(red_in)/4, analogRead(green_in)/4, analogRead(blue_in)/4);
}

void effect_1(){
  fade_pot(-1, 0);
  fade_pot(1,0);
}

void effect_2(){
      increment = 1;
      delaytime = 2;
      for(int i = 0 ; i <= 255; i+=increment){ 
        logWrite(red_out, i);
        delay(delaytime);   
       } 
       for(int i = 255 ; i >= 0; i-=increment){ 
        logWrite(red_out, i);
        delay(delaytime);   
       } 
       for(int i = 0 ; i <= 255; i+=increment){ 
        logWrite(green_out, i);
        delay(delaytime);   
       } 
       for(int i = 255 ; i >= 0; i-=increment){ 
        logWrite(green_out, i);
        delay(delaytime);   
       } 
      for(int i = 0 ; i <= 255; i+=increment){ 
        logWrite(blue_out, i);
        delay(delaytime);   
       } 
       for(int i = 255 ; i >= 0; i-=increment){ 
        logWrite(blue_out, i);
        delay(delaytime);   
       } 
}


void effect_3(){
      increment = 1;
      delaytime = 2;
      for(int i = 0 ; i <= 255; i+=increment){ 
        set_rgb(i, 255-i, 0);
        delay(delaytime);   
       } 
       for(int i = 0 ; i <= 255; i+=increment){ 
        set_rgb(0, i, 255-i);
        delay(delaytime);   
       } 
       for(int i = 0 ; i <= 255; i+=increment){ 
        set_rgb(255-i, 0, i);
        delay(delaytime);   
       } 
}

void effect_4(){
      increment = 1;
      delaytime = 2;
      for(int i = 0 ; i <= 255; i+=increment){ 
        logWrite(red_out, i);
        delay(delaytime);   
       } 
       for(int i = 255 ; i >= 0; i-=increment){ 
        logWrite(red_out, i);
        delay(delaytime);   
       } 
       for(int i = 0 ; i <= 255; i+=increment){ 
        logWrite(green_out, i);
        delay(delaytime);   
       } 
       for(int i = 255 ; i >= 0; i-=increment){ 
        logWrite(green_out, i);
        delay(delaytime);   
       } 
      for(int i = 0 ; i <= 255; i+=increment){ 
        logWrite(blue_out, i);
        delay(delaytime);   
       } 
       for(int i = 255 ; i >= 0; i-=increment){ 
        logWrite(blue_out, i);
        delay(delaytime);   
       } 
}

void effect_5(){
    increment = 1;
    delaytime = 2000;
    delaytime2 = 100;
    for (int i = 1; i <= 3; i+=increment){
     switch (i) {
      case 1:
      set_rgb(255,0,0);
      break;
      case 2:
      set_rgb(0,255,0);
      break;
      case 3:
      set_rgb(0,0,255);
      break;
     }
     delay (delaytime2);
     set_rgb(0,0,0);
     delay (delaytime);
    }
}

void kill_effects(){
      delaytime = 0;
      delaytime2 = 0;
      increment = 1000;

}

void fade_pot(byte updown, int minimum){
  increment = 1;
  delaytime = 8; 
  if (updown == 1){
    for (int i = minimum ; i < 1001 ; i++){
      if (i>250) i+=increment;
      if (i>500) i+=increment;
      if (i>750) i+=increment;
      fade_pot_helper(i);
    }
  } else {
    for (int i = 1001 ; i > minimum ; i-=increment){
      if (i>250) i-=increment;
      if (i>500) i-=increment;
      if (i>750) i-=increment;
      fade_pot_helper(i);
    }
  }
}

void fade_pot_helper(int i){
    floattemp = analogRead(red_in);
    redinc = floattemp/4000;
    floattemp = analogRead(green_in);
    greeninc = floattemp/4000;
    floattemp = analogRead(blue_in);
    blueinc = floattemp/4000;
    set_rgb(redinc * i, greeninc * i, blueinc * i);
    delay(delaytime);  
}

(this ones so huge, its needs its own post)

NECIRrcvcpp

// NECIRrcv
// Joe Knapp   jmknapp AT gmail DOT com

#include "WProgram.h"
// #include "NECIRrcv.h"   doesnt need to be included anymore

void ir_set(int irpin)  //changed from: NECIRrcv::NECIRrcv(int irpin)
{
  irparams.irpin = irpin ;
}

// initialization
void ir_begin() {          //changed from: void NECIRrcv::begin()
  // setup pulse clock timer interrupt
  TCCR2A = 0;  // normal mode

  //Prescale /8 (16M/8 = 0.5 microseconds per tick)
  // Therefore, the timer interval can range from 0.5 to 128 microseconds
  // depending on the reset value (255 to 0)
  cbi(TCCR2B,CS22) ;
  sbi(TCCR2B,CS21) ;
  cbi(TCCR2B,CS20) ;

  //Timer2 Overflow Interrupt Enable
  sbi(TIMSK2,TOIE2) ;

  RESET_TIMER2;

  sei();  // enable interrupts
  
  // initialize state machine variables
  irparams.rcvstate = IDLE ;
  irparams.bitcounter = 0 ;
  irparams.ircode = 0 ;
  irparams.fptr = 0 ;
  irparams.rptr = 0 ;
  irparams.blinkflag = 0 ;

  // set pin modes
  pinMode(irparams.irpin, INPUT) ;
}

// return next IR code from buffer, or -1 if none
unsigned long ir_read()    //changed: unsigned long NECIRrcv::read()
{
  unsigned long ircode ;
  if (irparams.fptr != irparams.rptr) {
    ircode = irparams.irbuf[irparams.rptr] ;
    irparams.rptr = (irparams.rptr + 1) % MAXBUF ;
    return((unsigned long)ircode) ;
  }
  else
    return((unsigned long)-1) ;
}

// return number of IR codes in buffer
int ir_available()      //changed: int NECIRrcv::available()
{
  int n ;
  n = irparams.fptr - irparams.rptr ; 
  if (n < 0)
    n += MAXBUF ;
  return(n) ;
}

// enable/disable blinking of pin 13 on IR processing
void ir_blink13(int blinkflag)      //changed: void NECIRrcv::blink13(int blinkflag)
{
  irparams.blinkflag = blinkflag ;
  if (blinkflag)
    pinMode(BLINKLED, OUTPUT) ;
}

// flush IR code buffer
void ir_flush()      //changed: void NECIRrcv::flush()
{
  irparams.rptr = irparams.fptr ;
}

// IR receiver state machine (TIMER2 interrupt) 
ISR(TIMER2_OVF_vect)
{
  RESET_TIMER2 ;

  irparams.irdata = GETIR(irparams.irpin) ;
  
  if (irparams.blinkflag && (irparams.rcvstate != IDLE))
    PORTB |= B00100000 ;  // turn pin 13 LED on
  
  // process current state
  switch(irparams.rcvstate) {
    case IDLE:
      if (irparams.irdata == MARK) {  // got some activity
          nextstate(STARTH) ;
          irparams.timer = 0 ;
      }
      break ;
    case STARTH:   // looking for initial start MARK
      // entered on MARK
      if (irparams.irdata == SPACE) {   // MARK ended, check time
        if ((irparams.timer >= STARTMIN) && (irparams.timer <= STARTMAX)) {
          nextstate(STARTL) ;  // time OK, now look for start SPACE
          irparams.timer = 0 ;
        }
        else
          nextstate(IDLE) ;  // bad MARK time, go back to IDLE
      }
      else
        irparams.timer++ ;  // still MARK, increment timer
      break ;
    case STARTL:
      // entered on SPACE
      if (irparams.irdata == MARK) {  // SPACE ended, check time
        if ((irparams.timer >= SPACEMIN) && (irparams.timer <= SPACEMAX)) {
          nextstate(BITMARK) ;  // time OK, check first bit MARK 
          irparams.timer = 0 ;
          irparams.bitcounter = 0 ;  // initialize ircode vars
          irparams.irmask = (unsigned long)0x1 ;
          irparams.ircode = 0 ;
        }
        else if ((irparams.timer >= RPTSPACEMIN) && (irparams.timer <= RPTSPACEMAX)) {  // not a start SPACE, maybe this is a repeat signal
          nextstate(RPTMARK) ;   // yep, it's a repeat signal
          irparams.timer = 0 ;
        }
        else
          nextstate(IDLE) ;  // bad start SPACE time, go back to IDLE
      }
      else {   // still SPACE
        irparams.timer++ ;    // increment time
        if (irparams.timer >= SPACEMAX)  // check against max time for SPACE
          nextstate(IDLE) ;  // max time exceeded, go back to IDLE
      }
      break ;
    case RPTMARK:
      irparams.timer++ ;  // measuring MARK
      if (irparams.irdata == SPACE) {  // MARK ended, check time
        if ((irparams.timer >= BITMARKMIN) && (irparams.timer <= BITMARKMAX))
          nextstate(IDLE) ;  // repeats are ignored here, just go back to IDLE
        else
          nextstate(IDLE) ;  // bad repeat MARK time, go back to IDLE
      }
      break ;
    case BITMARK:
      irparams.timer++ ;   // timing MARK
      if (irparams.irdata == SPACE) {   // MARK ended, check time
        if ((irparams.timer < BITMARKMIN) || (irparams.timer > BITMARKMAX))
          nextstate(IDLE) ;  // bad MARK time, go back to idle
        else {
          irparams.rcvstate = BIT ;  // MARK time OK, go to BIT
          irparams.timer = 0 ;
        }
      }
      break ;
    case BIT:
      irparams.timer++ ; // measuring SPACE
      if (irparams.irdata == MARK) {  // bit SPACE ended, check time
        if ((irparams.timer >= ONESPACEMIN) && (irparams.timer <= ONESPACEMAX)) {
          nextstate(ONE) ;   // SPACE matched ONE timing
          irparams.timer = 0 ;
        }
        else if ((irparams.timer >= ZEROSPACEMIN) && (irparams.timer <= ZEROSPACEMAX)) {
          nextstate(ZERO) ;  // SPACE matched ZERO timimg
          irparams.timer = 0 ;
        }
        else
          nextstate(IDLE) ;  // bad SPACE time, go back to IDLE
      }
      else {  // still SPACE, check against max time
        if (irparams.timer > ONESPACEMAX)
          nextstate(IDLE) ;  // SPACE exceeded max time, go back to IDLE
      }
      break ;
    case ONE:
      irparams.ircode |= irparams.irmask ;  // got a ONE, update ircode
      irparams.irmask <<= 1 ;  // set mask to next bit
      irparams.bitcounter++ ;  // update bitcounter
      if (irparams.bitcounter < NBITS)  // if not done, look for next bit
        nextstate(BITMARK) ;
      else
        nextstate(STOP) ;  // done, got NBITS, go to STOP
      break ;
    case ZERO:
      irparams.irmask <<= 1 ;  // got a ZERO, update mask
      irparams.bitcounter++ ;  // update bitcounter
      if (irparams.bitcounter < NBITS)  // if not done, look for next bit
        nextstate(BITMARK) ;
      else
        nextstate(STOP) ;  // done, got NBITS, go to STOP
      break ;
    case STOP:
      irparams.timer++ ;  //measuring MARK
      if (irparams.irdata == SPACE) {  // got a SPACE, check stop MARK time
        if ((irparams.timer >= BITMARKMIN) && (irparams.timer <= BITMARKMAX)) {
          // time OK -- got an IR code
          irparams.irbuf[irparams.fptr] = irparams.ircode ;   // store code at fptr position
          irparams.fptr = (irparams.fptr + 1) % MAXBUF ; // move fptr to next empty slot
        }
        nextstate(IDLE) ;  // finished with this code, go back to IDLE
      }
      break ;
  }
  // end state processing

  if (irparams.blinkflag)
    PORTB &= B11011111 ;  // turn pin 13 LED off
}

NECIRrcvh

#ifndef NECIRrcv_h  //dont know what to do with these
#define NECIRrcv_h  //file is otherwise left alone pretty much (except the end comment)

#include "WConstants.h"

#define USECPERTICK 50  // microseconds per clock interrupt tick
#define CLKFUDGE 5      // fudge factor for clock interrupt overhead
#define CLKMAX 256      // max value for clock (timer 2)
#define PRESCALE 8      // timer2 clock prescale
#define SYSCLOCK 16000000  // main Arduino clock
#define CLKSPERUSEC (SYSCLOCK/PRESCALE/1000000)   // timer clocks per microsecond

#define MAXBUF 8       // IR command code buffer length (circular buffer)

// IR detector output is active low
#define MARK  0
#define SPACE 1

#define NBITS 32         // bits in IR code

#define BLINKLED 13

// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

// clock timer reset value
#define INIT_TIMER_COUNT2 (CLKMAX - USECPERTICK*CLKSPERUSEC + CLKFUDGE)
#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT2

// pulse parameters -- nominal usec
#define STARTNOM      9000
#define SPACENOM      4500
#define BITMARKNOM    620
#define ONESPACENOM   1600
#define ZEROSPACENOM  480
#define RPTSPACENOM   2180

#define TOLERANCE 20  // percent
#define LTOL (1.0 - TOLERANCE/100.) 
#define UTOL (1.0 + TOLERANCE/100.) 

// pulse parameters (tick counts)
#define STARTMIN (int)((STARTNOM/USECPERTICK)*LTOL) // start MARK
#define STARTMAX (int)((STARTNOM/USECPERTICK)*UTOL) 
#define SPACEMIN (int)((SPACENOM/USECPERTICK)*LTOL) 
#define SPACEMAX (int)((SPACENOM/USECPERTICK)*UTOL) 
#define BITMARKMIN (int)((BITMARKNOM/USECPERTICK)*LTOL-2) // extra tolerance for low counts
#define BITMARKMAX (int)((BITMARKNOM/USECPERTICK)*UTOL+2) 
#define ONESPACEMIN (int)((ONESPACENOM/USECPERTICK)*LTOL) 
#define ONESPACEMAX (int)((ONESPACENOM/USECPERTICK)*UTOL) 
#define ZEROSPACEMIN (int)((ZEROSPACENOM/USECPERTICK)*LTOL-2) 
#define ZEROSPACEMAX (int)((ZEROSPACENOM/USECPERTICK)*UTOL+2) 
#define RPTSPACEMIN (int)((RPTSPACENOM/USECPERTICK)*LTOL) 
#define RPTSPACEMAX (int)((RPTSPACENOM/USECPERTICK)*UTOL) 

// receiver states
#define IDLE     1
#define STARTH   2
#define STARTL   3
#define BIT      4
#define ONE      5
#define ZERO     6
#define STOP     7
#define BITMARK  8
#define RPTMARK  9

// macros
#define GETIR(X) ((byte)digitalRead(X))    // used to read IR pin
#define nextstate(X) (irparams.rcvstate = X)

// state machine variables irparams
static volatile struct {
  byte rcvstate ;          // IR receiver state
  byte bitcounter ;        // bit counter
  byte irdata ;            // MARK or SPACE read from IR input pin
  byte fptr ;              // irbuf front pointer
  byte rptr ;              // irbuf rear pointer
  byte irpin ;             // pin for IR data from detector
  byte blinkflag ;         // TRUE to enable blinking of pin 13 on IR processing
  unsigned int timer ;     // state timer
  unsigned long irmask ;   // one-bit mask for constructing IR code
  unsigned long ircode ;   // IR code
  unsigned long irbuf[MAXBUF] ;    // circular buffer for IR codes
} irparams ;

// main class      //dont know what to do with this?
class NECIRrcv
{
  public:
    NECIRrcv(int irpin);
    unsigned long read();
    void begin();
    int available() ;
    void flush() ;
    void blink13(int blinkflag) ;
  private:
} ;
#endif

Alright, so thats all of it.

the current error im getting is:

C:\Documents and Settings\Gavin\Desktop\arduino-0011\hardware\cores\arduino/WProgram.h:13: error: default argument given for parameter 3 of 'long unsigned int pulseIn(uint8_t, uint8_t, long unsigned int)'


C:\Documents and Settings\Gavin\Desktop\arduino-0011\hardware\cores\arduino/WProgram.h:13: error: after previous specification in 'long unsigned int pulseIn(uint8_t, uint8_t, long unsigned int)'


 In function 'void ir_set(int)':
error: 'irparams' was not declared in this scope In function 'void ir_begin()':
 In function 'long unsigned int ir_read()':
 In function 'int ir_available()':
 In function 'void ir_blink13(int)':
 In function 'void ir_flush()':
 In function 'void __vector_9()':

So, if ANYONE can help me out to get this all working, i don't know how i could repay you, but I'm getting pretty desperate!

Also, because of those last two (the IR code) the code wont compile under 0012 (or 0011 in this state :D) so if anyone knows how to fix that too, thats also good!

Oh, the ir_check(); which Ive been trying to attach to the ISR for timer 2 forever is at the bottom of the first file.

Sorry for the excess posting.
Ill be waiting fingers crossed....

Gav,

The following code is a rewrite of yours that calls the built-in NECIRrcv library. It compiles correctly. You can use this as a starting point for your project. Modify the library in small steps and your sketch in commensurately small steps, and I think you'll have success.

Mikal

#include <NECIRrcv.h>

// Special fix for Arduino 0012 library problem
#undef int
#undef round
#undef float
#undef abs

//Assignments
#define red_out 9
#define green_out 5
#define blue_out 6
#define red_in 0
#define green_in 1
#define blue_in 2
#define speakerpin 4
#define btn1 2
#define ir_pin 12
#define led 13
#define max_state 5

//Logitech Remote Codes
#define Test 4194694920
#define Input 4127848200
#define Effect 3793614600
#define Settings 3760191240
#define Power 4010866440
#define Sub_up 4228118280
#define Sub_down 4261541640
#define Centre_up 4244829960
#define Centre_down 4177983240
#define Surround_up 4278253320
#define Surround_down 4211406600
#define Volume_up 3843749640
#define Volume_down 4044289800
#define Mute 3910596360

//Fade Variables
float redinc, greeninc, blueinc;
float floattemp;

//Global Variables
int state = 0;
int delaytime = 0;
int delaytime2 = 0;
int increment = 0;
unsigned long ir_code;
NECIRrcv ir(ir_pin) ;

void manual_mix(){
  set_rgb(analogRead(red_in)/4, analogRead(green_in)/4, analogRead(blue_in)/4);
}

void effect_1(){
  fade_pot(-1, 0);
  fade_pot(1,0);
}

void effect_2(){
      increment = 1;
      delaytime = 2;
      for(int i = 0 ; i <= 255; i+=increment){
        logWrite(red_out, i);
        delay(delaytime);
       }
       for(int i = 255 ; i >= 0; i-=increment){
        logWrite(red_out, i);
        delay(delaytime);
       }
       for(int i = 0 ; i <= 255; i+=increment){
        logWrite(green_out, i);
        delay(delaytime);
       }
       for(int i = 255 ; i >= 0; i-=increment){
        logWrite(green_out, i);
        delay(delaytime);
       }
      for(int i = 0 ; i <= 255; i+=increment){
        logWrite(blue_out, i);
        delay(delaytime);
       }
       for(int i = 255 ; i >= 0; i-=increment){
        logWrite(blue_out, i);
        delay(delaytime);
       }
}


void effect_3(){
      increment = 1;
      delaytime = 2;
      for(int i = 0 ; i <= 255; i+=increment){
        set_rgb(i, 255-i, 0);
        delay(delaytime);
       }
       for(int i = 0 ; i <= 255; i+=increment){
        set_rgb(0, i, 255-i);
        delay(delaytime);
       }
       for(int i = 0 ; i <= 255; i+=increment){
        set_rgb(255-i, 0, i);
        delay(delaytime);
       }
}

void effect_4(){
      increment = 1;
      delaytime = 2;
      for(int i = 0 ; i <= 255; i+=increment){
        logWrite(red_out, i);
        delay(delaytime);
       }
       for(int i = 255 ; i >= 0; i-=increment){
        logWrite(red_out, i);
        delay(delaytime);
       }
       for(int i = 0 ; i <= 255; i+=increment){
        logWrite(green_out, i);
        delay(delaytime);
       }
       for(int i = 255 ; i >= 0; i-=increment){
        logWrite(green_out, i);
        delay(delaytime);
       }
      for(int i = 0 ; i <= 255; i+=increment){
        logWrite(blue_out, i);
        delay(delaytime);
       }
       for(int i = 255 ; i >= 0; i-=increment){
        logWrite(blue_out, i);
        delay(delaytime);
       }
}

void effect_5(){
    increment = 1;
    delaytime = 2000;
    delaytime2 = 100;
    for (int i = 1; i <= 3; i+=increment){
     switch (i) {
      case 1:
      set_rgb(255,0,0);
      break;
      case 2:
      set_rgb(0,255,0);
      break;
      case 3:
      set_rgb(0,0,255);
      break;
     }
     delay (delaytime2);
     set_rgb(0,0,0);
     delay (delaytime);
    }
}

void kill_effects(){
      delaytime = 0;
      delaytime2 = 0;
      increment = 1000;

}

void fade_pot(byte updown, int minimum){
  increment = 1;
  delaytime = 8;
  if (updown == 1){
    for (int i = minimum ; i < 1001 ; i++){
      if (i>250) i+=increment;
      if (i>500) i+=increment;
      if (i>750) i+=increment;
      fade_pot_helper(i);
    }
  } else {
    for (int i = 1001 ; i > minimum ; i-=increment){
      if (i>250) i-=increment;
      if (i>500) i-=increment;
      if (i>750) i-=increment;
      fade_pot_helper(i);
    }
  }
}

void fade_pot_helper(int i){
    floattemp = analogRead(red_in);
    redinc = floattemp/4000;
    floattemp = analogRead(green_in);
    greeninc = floattemp/4000;
    floattemp = analogRead(blue_in);
    blueinc = floattemp/4000;
    set_rgb(redinc * i, greeninc * i, blueinc * i);
    delay(delaytime);
}

void setup()
{
  ir.begin();

  //Reset PWM outputs
  analogWrite(red_out, 255);
  analogWrite(green_out, 255);
  analogWrite(blue_out, 255);

  //Set Pinmodes
  pinMode(red_out, OUTPUT);
  pinMode(green_out, OUTPUT);
  pinMode(blue_out, OUTPUT);
  pinMode(speakerpin, OUTPUT);
  pinMode(btn1, INPUT);

  //Setup IR, Serial, Interrupts, Timers
  Serial.begin(19200);
  attachInterrupt(0, btn1_press, RISING);

  //Startup Sequence
  play_tone(1915, 70);
  play_tone(1136, 70);
  play_tone(800, 70);
  play_tone(400, 100);
  fade_pot(1, 0);
  state = 0;
  toggle_pin(led);
}

void loop()
{
  Serial.println(state);
  ir_check();

  switch (state){
    case -2:
      set_rgb(0,0,0);
      break;
    case -1:
     if ((analogRead(red_out) > 0)
     || (analogRead(green_out) > 0)
     || (analogRead(blue_out) > 0))
       fade_pot(-1,0);
     break;
    case 0:
      manual_mix();
      break;
    case 1:
      effect_1();
      break;
    case 2:
      effect_2();
      break;
     case 3:
      effect_3();
      break;
     case 4:
      effect_4();
      break;
     case 5:
      effect_5();
      break;
  }
}

void logWrite(int colour,int pwmval)   //adapted version of analogWrite
{
  if (pwmval < 0)
    analogWrite(colour, 255);
  else if (pwmval > 255)
    analogWrite(colour, 0);
  else
    analogWrite(colour, 255-pwmval);
}

void set_rgb(int red, int green, int blue){
    logWrite(red_out, red);
    logWrite(green_out, green);
    logWrite(blue_out, blue);
}

void toggle_pin(int pin){
    digitalWrite(pin, !digitalRead(pin));
}

void play_tone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerpin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerpin, LOW);
    delayMicroseconds(tone);
  }
}

void btn1_press(){
  state_inc(true);
  kill_effects();
}

void state_inc(boolean up){
  if (up == true){
    if (state != max_state){
      beep_std();
      state++;
    }else{
      beep_high();
    }
  } else {
    if (state != 0){
      beep_std();
      state--;
    }else{
      beep_low();
    }
  }
}

void beep_std(){
  play_tone(1000, 20);
}

void beep_low(){
  play_tone(1800, 20);
}

void beep_high(){
  play_tone(700, 20);
}

void ir_check(){
  if (ir.available()){
    ir_code = ir.read();
    switch (ir_code){
     case Power:
        if (state == -1){
          beep_low();
          state = -2;
        }else{
          beep_high();
          beep_low();
          state = -1;
        }
        break;
      case Volume_up:
        state_inc(true);
        break;
      case Volume_down:
        state_inc(false);
        break;
    }
    kill_effects();
  }
}