William Toth
Navigation
Home
Tutorials
Links

Portfolio
Programming
FIRST Robotics
Hockey
3d Art and Designs
Company of Heroes Mod
Hardware Creations
Music
Other Projects

Contact
Email
Instant Messaging
Phone

Infrared Sensor

IR_Routines.h


/*
  IR board command routines
  Author: William Toth
  Date: 01/15/08
  Description: This maps out some routines that will happen each
  time that you click one of the ir buttons. This is an easy way
  and an organized way to include the IR stuff. Feel free to use
  this code, please dont make any copys on the internet without
  permission, thank you.
*/

   
#ifndef __IR_Header__

#define __IR_Header__



//define which digital I/O port you are using:

//Make sure they are set as inputs in User_routines.h

#define IR_PORT1 rc_dig_in04   //IR button 1

#define IR_PORT2 rc_dig_in05   //IR button 2

#define IR_PORT3 rc_dig_in06   //IR button 3

#define IR_PORT4 rc_dig_in07   //IR button 4



//Use all IR Routines

void IR_All_Routines(void);

//If you only plan on using a couple of buttons you can

//call a couple of these functions, otherwise just stick

//to IR_All_Routines()

//Function for when the first button is pressed

void IR_1_Routine(void);

//Function for when the second button is pressed

void IR_2_Routine(void);

//Function for when the third button is pressed

void IR_3_Routine(void);

//Function for when the fourth button is pressed

void IR_4_Routine(void);

void Disable_IR(void);

//IR enabled by default. Only call this if

//you already disabled it.

void Enable_IR(void);

#ifdef IR_INTERRUPTS

//Enable works best in the User_Initialization in user_routines

void Enable_IR_Interrupts(void);

void Disable_IR_Interrupts(void);

/*Declare variables that you want cleared each time
a different button is presed in this function. Useful for
things like timers if you want a certain routine preformed
over and over again in each block.*/

void Variable_Definitions(void);

//Used to set all pwms back to 127 so if you do a different

//button the thing doesnt keep driving if you forget

void Reset_pwms(void);
#endif






IR_Routines.c
This code includes examples of what you can put in for each button place


#include "IR_Routines.h"

#include "ifi_default.h"

#include "ifi_aliases.h"


unsigned char IR_Routine_Number;
unsigned char IR_Disabled;

/*Initialize variables That you want to stay no
matter how many times it loops and changes here*/

unsigned int tester=0;


/*Write variables that you want cleared each time
a different button is presed in this function. Useful for
things like timers if you want a certain routine preformed
over and over again in each block. Do it exactly as "tester" is*/

void Variable_Definitions(void)
{
      tester=0;
}


//Write code for the actual routines for each button here.

void IR_1_Routine(void)
{
   
/******Start code for the first IR button here******/

     
      pwm01=pwm02=170;
      if(tester>100)
      {
              pwm01=pwm02=127;
      }
      tester++;
}

void IR_2_Routine(void)
{
   
/******Start code for the second IR button here******/
     
      pwm01=pwm02=80;
}

void IR_3_Routine(void)
{
   
/******Start code for the third IR button here******/

      pwm01=170;
      pwm02=80;      
}


void IR_4_Routine(void)
{
   
/******Start code for the fourth IR button here******/

}


void Reset_pwms(void)
{
  pwm01 = pwm02 = pwm03 = pwm04 = pwm05 = pwm06 = pwm07 = pwm08 = 127;
  pwm09 = pwm10 = pwm11 = pwm12 = pwm13 = pwm14 = pwm15 = pwm16 = 127;
}


//Use all IR Routines

void IR_All_Routines(void)
{
    if(IR_Disabled)
    {
        return;
    }
       
    //I have to use this variable instead of calling the function directly

    //because in your loop you want to keep the function going. You dont need

    //to hold the button for it to keep going. Variable_Definitions()

      //is called to reset all the variables in that function. It also

      //sets all pwms neutral before doing your task so a certain moter wont keep going.

      // If you dont want that just comment that part out.

    if(IR_PORT1)
        {
              if(IR_Routine_Number != 1)
              {
              IR_Routine_Number = 1;
                      Variable_Definitions();
                      Reset_pwms();
              }
        }
    else if(IR_PORT2)
        {
              if(IR_Routine_Number != 2)
              {
              IR_Routine_Number = 2;
                      Variable_Definitions();
                      Reset_pwms();
              }
        }
    else if(IR_PORT3)
        {
              if(IR_Routine_Number != 3)
              {
              IR_Routine_Number = 3;
                      Variable_Definitions();
                      Reset_pwms();
              }
        }
    else if(IR_PORT4)
        {
              if(IR_Routine_Number != 4)
              {
              IR_Routine_Number = 4;
                      Variable_Definitions();
                      Reset_pwms();
              }
        }
                     
       
    //call the actual functions now

    if(IR_Routine_Number == 1)
      {
              IR_1_Routine();      
      }
    else if(IR_Routine_Number == 2)
      {
              IR_2_Routine();
      }
    else if(IR_Routine_Number == 3)
      {
              IR_3_Routine();
      }

    else if(IR_Routine_Number == 4)
      {
              IR_4_Routine();
      }      
}

//If you want to disable any feedback from the IR sensor

void Disable_IR(void)
{
    IR_Disabled=1;
}

//Enable the ir, only needs to be called after you call

//Disable_IR().

void Enable_IR(void)
{
    IR_Disabled=0;
}