Go Down

Topic: Noob Q about Infrared and LED (Read 20 times) previous topic - next topic

UltraNoob

Today at 03:53 pm Last Edit: Today at 04:07 pm by UltraNoob
Hello good people,

I'm a beginner at Arduino, we have to start somewhere.
I found Nick Gammons code very helpful to understand how to work with push buttons and blinking leds.
http://www.gammon.com.au/switches in particular any of the turn indicator samples.
My next quest would be to turn the same code in to InfraRed + Remote.

I've been messing with it as far as my peanut allows me to at the moment.
But I haven't succeded and have reached my patience limit. Better ask than stare.

The existing code works fine with push buttons.
But when adding the IRremote.h, etc.. I run into issues.

I get no errors when compiling.
But when using the IR. All buttons do the same.
They light up 2 leds constantly untill random remote button is pushed again.
I have defined back pause and next buttons.

I've made another code and I can turn on each of the leds independetly, via digitalWrite, so I presume the IR functions.

Can someone lift me over my current limitation so I can understand it?

So I didn't like the transition from left <> right
from Motorcycle turn indicator with warning function
and
Replaced part with part from
Motorcycle turn indicator with brake light.
Separated each function for each push button.

Then I wanted to add Infrared remote and got stuck.
The code has junk in it from trying whatever came to mind.
So exuse me :)


This is what I have so far:

Thank you, enjoy the lovely summer sunday.

FAILED IR code (turns only channel 8 amd 9 on with any button on the remote:
Code: [Select]
#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>


#include <SwitchManager.h>

typedef enum {
    NONE,
    LH_DOWN,
    RH_DOWN,
    ALARM_DOWN,
    LH_LIGHT_ON,
    RH_LIGHT_ON,
    ALARM_LIGHT_ON,
    BOTH
};

const unsigned long BLINK_INTERVAL = 666; // ms

// pin assignments
const byte RECV_PIN = 3;
//const byte LH_SWITCH_PIN = 5;
//const byte RH_SWITCH_PIN = 3;
//const byte ALARM_SWITCH_PIN = 6;
const byte LH1_LIGHT = 9;
const byte LH2_LIGHT = 10;
const byte RH1_LIGHT = 12;
const byte RH2_LIGHT = 11;
//const byte itsONled[] = {0,0,0,0,0};
//IRsend irsend;

#define LH_SWITCH_PIN 16712445 // code received from button no. 1 / back
#define RH_SWITCH_PIN 16720605 // code received from button no. 2 / pause
#define ALARM_SWITCH_PIN 16761405 // code received from button no. 3 / next
//#define code4 16754775 // code received from button no. 4 / minus

IRrecv irrecv(RECV_PIN);

decode_results results;

SwitchManager LHswitch;
SwitchManager RHswitch;
SwitchManager ALARMswitch;
 
byte state = NONE;
 
void handleLHPress (const byte newState, const unsigned long interval, const byte whichPin)
  {
  // switch down?
  if (newState == LOW)
     {
     switch (state)
       {
       // if already on or warning signal, turn all off
       case LH_LIGHT_ON:
         state = NONE;
         break;
        
       // otherwise switch is now down, but not yet released
       default:
         state = LH_DOWN;
         break;
       }  // end of switch
     return;
     }  // end of LH switch down
  
  // switch must be up

  if (state == LH_DOWN)  // if down, switch to down-and-released mode
    state = LH_LIGHT_ON;  
  }  // end of handleLHPress
 
void handleRHPress (const byte newState, const unsigned long interval, const byte whichPin)
  {
  // switch down?
  if (newState == LOW)
     {
     switch (state)
       {
        
       // if already on or warning signal, turn all off
       case RH_LIGHT_ON:
         state = NONE;
         break;
        
       // otherwise switch is now down, but not yet released
       default:
         state = RH_DOWN;
         break;
       }  // end of switch
     return;
     }  // end of RH switch down
  
  // switch must be up

  if (state == RH_DOWN)  // if down, switch to down-and-released mode
    state = RH_LIGHT_ON;  
  }  // end of handleRHPress

void handleALARMPress (const byte newState, const unsigned long interval, const byte whichPin)
  {
  // switch down?
  if (newState == LOW)
     {
     switch (state)
       {
        
       // if already on or warning signal, turn all off
       case ALARM_LIGHT_ON:
         state = NONE;
         break;
        
       // otherwise switch is now down, but not yet released
       default:
         state = ALARM_DOWN;
         break;
       }  // end of switch
     return;
     }  // end of RH switch down
  
  // switch must be up

  if (state == ALARM_DOWN)  // if down, switch to down-and-released mode
    state = ALARM_LIGHT_ON;  

  }  // end of handleRHPress
  
void setup ()
  {
  Serial.begin(9600); // you can omit this line
  irrecv.enableIRIn(); // Start the receiver
  LHswitch.begin (LH_SWITCH_PIN, handleLHPress);
  RHswitch.begin (RH_SWITCH_PIN, handleRHPress);
  ALARMswitch.begin (ALARM_SWITCH_PIN, handleALARMPress);
  pinMode (LH1_LIGHT, OUTPUT);
  pinMode (LH2_LIGHT, OUTPUT);
  pinMode (RH1_LIGHT, OUTPUT);
  pinMode (RH2_LIGHT, OUTPUT);
 // pinMode (LH_SWITCH_PIN, INPUT);
  pinMode (RECV_PIN, INPUT);
  }  // end of setup

unsigned long lastBlink;
bool onCycle;
 
void blinkLights ()
  {
  lastBlink = millis ();
  onCycle = !onCycle;
  
  // default to off
  digitalWrite (LH1_LIGHT, LOW);
  digitalWrite (RH1_LIGHT, LOW);
  digitalWrite (LH2_LIGHT, LOW);
  digitalWrite (RH2_LIGHT, LOW);

  // every second time, turn them all off
  if (!onCycle)
  return;
    
  // blink light
  switch (state)
      {
    case NONE:
      break;
    
    case LH_DOWN:
    case LH_LIGHT_ON:
      digitalWrite (LH1_LIGHT, HIGH);
      digitalWrite (LH2_LIGHT, HIGH);
      break;
      
    case RH_DOWN:
    case RH_LIGHT_ON:
      digitalWrite (RH1_LIGHT, HIGH);
      digitalWrite (RH2_LIGHT, HIGH);
      break;
      
    case ALARM_DOWN:
      digitalWrite (LH1_LIGHT, HIGH);
      digitalWrite (RH1_LIGHT, HIGH);
      digitalWrite (LH2_LIGHT, HIGH);
      digitalWrite (RH2_LIGHT, HIGH);
      break;
    
    }  // end of switch on state
    
  }  // end of blinkLights
  

void loop() {
    if (irrecv.decode(&results)) {
    unsigned int value = results.value; {

     LHswitch.check ();  // check for presses
     RHswitch.check ();  // check for presses
     ALARMswitch.check ();  // check for presses          
 
   if (millis () - lastBlink >= BLINK_INTERVAL)
   blinkLights ();

    }
      Serial.println(value);
      irrecv.resume();

    } }
  
  


IR Serial Monitor Codes:
Hi-Fi Remote button codes

- on/off
16753245
4294967295

-M - memory
16736925
4294967295

- EQ
16769055
4294967295

-Plus
16748655
4294967295

-Min
16754775
4294967295

-back
16712445
4294967295

-play/pause
16720605
4294967295

-forward/next
16761405
4294967295

Thank you in advanced.

UltraNoob

Working code for 3 push buttons (noIR)

Code: [Select]
#include <SwitchManager.h>

typedef enum {
    NONE,
    LH_DOWN,
    RH_DOWN,
    ALARM_DOWN,
    LH_LIGHT_ON,
    RH_LIGHT_ON,
    ALARM_LIGHT_ON,
    BOTH
};

const unsigned long BLINK_INTERVAL = 666; // ms

// pin assignments
const byte LH_SWITCH_PIN = 5;
const byte RH_SWITCH_PIN = 3;
const byte ALARM_SWITCH_PIN = 6;
const byte LH1_LIGHT = 9;
const byte LH2_LIGHT = 10;
const byte RH1_LIGHT = 12;
const byte RH2_LIGHT = 11;

SwitchManager LHswitch;
SwitchManager RHswitch;
SwitchManager ALARMswitch;
 
byte state = NONE;
 
void handleLHPress (const byte newState, const unsigned long interval, const byte whichPin)
  {
  // switch down?
  if (newState == LOW)
     {
     switch (state)
       {
       // if already on or warning signal, turn all off
       case LH_LIGHT_ON:
         state = NONE;
         break;
         
       // otherwise switch is now down, but not yet released
       default:
         state = LH_DOWN;
         break;
       }  // end of switch
     return;
     }  // end of LH switch down
 
  // switch must be up

  if (state == LH_DOWN)  // if down, switch to down-and-released mode
    state = LH_LIGHT_ON; 
  }  // end of handleLHPress
 
void handleRHPress (const byte newState, const unsigned long interval, const byte whichPin)
  {
  // switch down?
  if (newState == LOW)
     {
     switch (state)
       {
         
       // if already on or warning signal, turn all off
       case RH_LIGHT_ON:
         state = NONE;
         break;
         
       // otherwise switch is now down, but not yet released
       default:
         state = RH_DOWN;
         break;
       }  // end of switch
     return;
     }  // end of RH switch down
 
  // switch must be up

  if (state == RH_DOWN)  // if down, switch to down-and-released mode
    state = RH_LIGHT_ON; 
  }  // end of handleRHPress

void handleALARMPress (const byte newState, const unsigned long interval, const byte whichPin)
  {
  // switch down?
  if (newState == LOW)
     {
     switch (state)
       {
         
       // if already on or warning signal, turn all off
       case ALARM_LIGHT_ON:
         state = NONE;
         break;
         
       // otherwise switch is now down, but not yet released
       default:
         state = ALARM_DOWN;
         break;
       }  // end of switch
     return;
     }  // end of RH switch down
 
  // switch must be up

  if (state == ALARM_DOWN)  // if down, switch to down-and-released mode
    state = ALARM_LIGHT_ON; 

  }  // end of handleRHPress
 
void setup ()
  {
  LHswitch.begin (LH_SWITCH_PIN, handleLHPress);
  RHswitch.begin (RH_SWITCH_PIN, handleRHPress);
  ALARMswitch.begin (ALARM_SWITCH_PIN, handleALARMPress);
  pinMode (LH1_LIGHT, OUTPUT);
  pinMode (LH2_LIGHT, OUTPUT);
  pinMode (RH1_LIGHT, OUTPUT);
  pinMode (RH2_LIGHT, OUTPUT);
  }  // end of setup

unsigned long lastBlink;
bool onCycle;
 
void blinkLights ()
  {
  lastBlink = millis ();
  onCycle = !onCycle;
 
  // default to off
  digitalWrite (LH1_LIGHT, LOW);
  digitalWrite (RH1_LIGHT, LOW);
  digitalWrite (LH2_LIGHT, LOW);
  digitalWrite (RH2_LIGHT, LOW);

  // every second time, turn them all off
  if (!onCycle)
    return;
   
  // blink light
  switch (state)
    {
    case NONE:
      break;
   
    case LH_DOWN:
    case LH_LIGHT_ON:
      digitalWrite (LH1_LIGHT, HIGH);
      digitalWrite (LH2_LIGHT, HIGH);
      break;
     
    case RH_DOWN:
    case RH_LIGHT_ON:
      digitalWrite (RH1_LIGHT, HIGH);
      digitalWrite (RH2_LIGHT, HIGH);
      break;
     
    case ALARM_DOWN:
      digitalWrite (LH1_LIGHT, HIGH);
      digitalWrite (RH1_LIGHT, HIGH);
      digitalWrite (LH2_LIGHT, HIGH);
      digitalWrite (RH2_LIGHT, HIGH);
      break;
   
    }  // end of switch on state
   
  }  // end of blinkLights
 
void loop ()
  {
  LHswitch.check ();  // check for presses
  RHswitch.check ();  // check for presses
  ALARMswitch.check ();  // check for presses

  if (millis () - lastBlink >= BLINK_INTERVAL)
    blinkLights ();
   
  // other stuff
  }  // end of loop

Go Up