Go Down

Topic: Ignoring repeated IR codes in switch case (Read 668 times) previous topic - next topic

UKHeliBob

Looking at the RiCino GitHub page it looks like it simply uses multiple IR detectors connected to the same Arduino pin as suggested earlier in the thread.  If so then getting it to work with one pin would be an imprtant first step.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

xxxWALDOxxx

https://github.com/cdemetriadis/RiCino-Lap-Counter/commit/e3c0adaf67c1b8dda2ba94aa57ea285e851d1e8e



above is a link that directs you to a github member who wanted to do the same thing with the zones.  He helped me get the zones part of the code working with a change in library.  Worked slick and continues to work.  I had to change my transponders to 8bit HEX which also helped out with the speed at which it transmitted the IR codes to the receiver.  My idea is to take the receiver bridge and use it to work this counter you are helping me with as well.  I figured I would start slow and get one working now and then add the other zones in later.  Obviously this was a BAAAAD idea to think that without bringing it up.  It was pretty simple to make the zone change with the Ricino receiver so I thought it would be fairly simple to change this code to make it work as well.  that's what I get for thinking.  When I get the chance to sit in front of my breadboard with the proto-typed counter, I will send something to get me started to see what your thoughts are on what I think might be a possible start.  I got the last code you sent to work with my segment display.  Have not done extensive testing, but seems to work.  Thanks for not giving up on me.

UKHeliBob

Quote
He helped me get the zones part of the code working with a change in library. 
Where can this revised library be downloaded from ?

There seems to be no mention of it on the RiCino GitHub page that you linked to
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

xxxWALDOxxx

I will try and attach the 2 libraries later this evening.  I am sure I am handling the libraries wrong, but there is one for sending and one for receiving.  I believe that the transponder has to be setup for 8bit HEX as well, so a standard remote control won't work, at least the way the library is setup right now.  But I will try and attach them to my next post.

xxxWALDOxxx

I just quickly went back to that Github page and grabbed the zip folder for the libraries that were used for the multi receiver setup.  When I was building and programming the receivers I copied and pasted the library into my IRremote folder, then removed it and replaced it with the transponder library the same way when building the transponder.  All worked well. and continues to impress.  Just hoping to adapt the same setup into the counter display.  Thanks again for your interest in helping me out.  The library folder is attached to this post.

xxxWALDOxxx

I took the latest code you wrote and adapted my iteration of 2 receivers using that code.  I will definitely say this is more than likely not the correct way by any means to do this, but it was a quick way to check the library I posted for multi receiver setup.  I just duplicated your original code with another instance of the decode_results2 for the second receiver and changed a few serial prints to show which receiver was receiving.  It worked, but again, I am sure this is so very wrong, but worked as a test.  I changed the library for 32 bits instead of the 8 that we used for our Ricino setup so I can use a standard remote for testing for now.  Here is what I did......okay, okay, hold the laughter. I'm trying.

Code: [Select]
#include "IRremote.h"

const byte receivePin = 12;
const byte receivePin2 = 10;

unsigned long currentCode;
unsigned long previousCode;
unsigned long currentCode2;
unsigned long previousCode2;
unsigned long currentTime;
unsigned long startTime;
unsigned long ignorePeriod = 5000;
boolean ignoreSameCode = false;

IRrecv irrecv(receivePin);
IRrecv irrecv2(receivePin2);

decode_results results;
decode_results results2;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn();
  irrecv2.enableIRIn();
}

void loop()
{
  currentTime = millis();
  if (irrecv.decode(&results))  //signal received
  {
    irrecv.resume();
    currentCode = results.value;  //get current code
    if (currentCode == 0xFFFFFFFF)
    {
      return;
    }
    debugReport();
    if (currentCode != previousCode || ignoreSameCode == false) //we have a new code
    {
      Serial.print("\t\tNew code : ");
      Serial.println(currentCode, HEX);
      startTime = currentTime;
      ignoreSameCode = true;
      Serial.println("Wait period started");
    }
    previousCode = currentCode; //save the previous code
  }
  if ((ignoreSameCode == true) && currentTime - startTime >= ignorePeriod)
  {
    ignoreSameCode = false;
    Serial.println("Wait period ended");
  }
  if (irrecv2.decode(&results2))  //signal received
  {
    irrecv2.resume();
    currentCode2 = results2.value;  //get current code
    if (currentCode2 == 0xFFFFFFFF)
    {
      return;
    }
    debugReport();
    if (currentCode2 != previousCode2 || ignoreSameCode == false) //we have a new code
    {
      Serial.print("\t\tNew code2 : ");
      Serial.println(currentCode2, HEX);
      startTime = currentTime;
      ignoreSameCode = true;
      Serial.println("Wait period started 2");
    }
    previousCode2 = currentCode2; //save the previous code
  }
  if ((ignoreSameCode == true) && currentTime - startTime >= ignorePeriod)
  {
    ignoreSameCode = false;
    Serial.println("Wait period ended 2");
  }
}
void debugReport()
{
  Serial.print("currentCode : ");
  Serial.println(currentCode, HEX);
  Serial.print("previousCode : ");
  Serial.println(previousCode, HEX);
  Serial.print("ignoreSameCode : ");
  Serial.println(ignoreSameCode == false ? "false" : "true");
  Serial.println();
}




again, I am sure you have better methods, especially for debugging this.  I had to bring the baud rate down from what you originally had, it just couldn't keep up with displaying all the received signals with as many as I was sending with my 8bit transponders.   Your thoughts?

I was doing some digging as always trying to research on my own and saw your post to other members in regards to using an array......waaaaay outta my league, I'm glad I was able to spark a little interest here though.  Thanks again for all the help on this UKHeliBob. MUCH appreciated.

xxxWALDOxxx

using the last code I posted and playing with ideas I came to a few new problems I am sure someone else can see long before it happened.  Using the multi receiver library I can get the 2 receivers to work, figured I would start with 2 and add more as I can refine the code.  But a couple problems I am unsure how to fix.  Considering I have 2 instances of the receivers, I am unsure of how to use the translate() function.  Now if both the receivers are receiving the same code it will translate twice which will initially add 2 laps at a time.  If I had the 4 receivers going it would add 4 laps.  Also noticing that when it receives a different code after the first viable saved code it will not go into ignore because it reads the different code.  With multiple receivers there is a chance of 4 different codes within milliseconds of each other and will continue getting lap counts until they are out of the IR sensor range. Any ideas?

Code: [Select]
#include "IRremote.h"

int receiver = 12; // Signal Pin of IR receiver to Arduino Digital Pin 12
int receiver2 = 10;
int receiver3 = 9;
int Blue = 11;
int Red = 7;

int lap_count1;
int lap_count2;

int latchPin = 4;
int clockPin = 5;
int dataPin = 3;

unsigned long StartMS;
unsigned long CurrentMS;
unsigned long LastIR;
unsigned long LastIR2;
unsigned long LastIR3;
unsigned long CurrentIR;
unsigned long CurrentIR2;
unsigned long CurrentIR3;
unsigned long ignorePeriod = 10000;
bool ignoreSameCode = false;
bool ignoreSameCode2 = false;



byte segdisp[10] = {          B11111100,
                              B01100000,
                              B11011010,
                              B11110010,
                              B01100110,
                              B10110110,
                              B10111110,
                              B11100000,
                              B11111110,
                              B11100110,
 
   };


/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);     // create instance of 'irrecv'
IRrecv irrecv2(receiver2);
IRrecv irrecv3(receiver3);
decode_results results;      // create instance of 'decode_results'
decode_results results2;
decode_results results3;

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);
  Serial.println("Ready to Detect");
  irrecv.enableIRIn(); // Start the receiver
  irrecv2.enableIRIn();
  irrecv3.enableIRIn();
  pinMode(Blue, OUTPUT);
  pinMode(Red, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  digitalWrite(latchPin, LOW);                        // send to 7 segment displays
  shiftOut(dataPin, clockPin, LSBFIRST, B11111100);
  shiftOut(dataPin, clockPin, LSBFIRST, B11111100);                     
  digitalWrite(latchPin, HIGH);
   


}/*--(end setup )---*/



/*-----( Function )-----*/
void translateIR() {// takes action based on IR code received
  int a;
  int b;
  int c;
  int d;
  a = lap_count1%10;
  b = lap_count1/10;
  c = lap_count2%10;
  d = lap_count2/10;
 
 


{

  switch(results.value)  // describe IR codes and what they will do
 
  {
     
   
  case 0x13:
  // BLUE button on tape light remote
  Serial.println("Driver 1");
  digitalWrite(Blue, HIGH);
  lap_count1 ++;
  Serial.println("Lap");
  Serial.println(lap_count1);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, segdisp[a]);
  shiftOut(dataPin, clockPin, LSBFIRST, segdisp[b]);
  digitalWrite(latchPin, HIGH);
  digitalWrite(Blue, LOW);
  break;
     

  case 0x14:   
  // RED button on tape light remote
  Serial.println("Driver 2");
  digitalWrite(Blue, HIGH);
  lap_count2 ++;
  Serial.println("Lap");
  Serial.println(lap_count2);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, segdisp[c]);
  shiftOut(dataPin, clockPin, LSBFIRST, segdisp[d]);
  digitalWrite(latchPin, HIGH);
  digitalWrite(Blue, LOW);
  break;

  case 0xFFA25: Serial.println("Reset");   // Play/Pause button on tape light remote
  digitalWrite(Red, HIGH);
  lap_count1 = 0;
  lap_count2 = 0;
  Serial.println(lap_count1, lap_count2);
   digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, B11111100);
  shiftOut(dataPin, clockPin, LSBFIRST, B11111100);
  digitalWrite(latchPin, HIGH);
   delay(1000);
  digitalWrite(Red, LOW);
  break;
   

  default:
    digitalWrite(Red, HIGH);
    delay(5);
    digitalWrite(Red, LOW);

  }// End Case
}
}

void loop() {  /*----( LOOP: RUNS CONSTANTLY )----*/
 
  CurrentMS = millis();
  if (irrecv.decode(&results))  //signal received
  {
    irrecv.resume();
    CurrentIR = results.value;
    if (CurrentIR == 0xFFFFFFFF)
    {
      return;
    }
    debugReport();
    if (CurrentIR != LastIR || ignoreSameCode == false)   //we have a new code
    {
    Serial.print("\t\tNew code : ");
    Serial.println(CurrentIR, HEX);   
    StartMS = CurrentMS;
    ignoreSameCode = true;
    Serial.println("wait period started");
   
    }
    LastIR = CurrentIR;
  }
    if ((ignoreSameCode == true) && CurrentMS - StartMS >= ignorePeriod)
    {
      ignoreSameCode = false;
      Serial.println("wait period over");
  }
   if (irrecv2.decode(&results2))  //signal received
  {
    irrecv2.resume();
    CurrentIR2 = results2.value;
    if (CurrentIR2 == 0xFFFFFFFF)
    {
      return;
    }
    debugReport();
    if (CurrentIR2 != LastIR2 || ignoreSameCode2 == false)   //we have a new code
    {
    Serial.print("\t\tNew code : ");
    Serial.println(CurrentIR2, HEX);   
    StartMS = CurrentMS;
    ignoreSameCode2 = true;
    Serial.println("wait period started");
    translateIR();
    }
    LastIR2 = CurrentIR2;
  }
    if ((ignoreSameCode2 == true) && CurrentMS - StartMS >= ignorePeriod)
    {
      ignoreSameCode2 = false;
      Serial.println("wait period over");
  }
}
void debugReport()
{
Serial.print("currentCode : ");
  Serial.println(CurrentIR, HEX);
  Serial.print("previousCode : ");
  Serial.println(LastIR, HEX);
  Serial.print("ignoreSameCode : ");
  Serial.println(ignoreSameCode == false ? "false" : "true");
  Serial.println(); 
}

xxxWALDOxxx

After a little more playing around with the code I ended with the following which resulted in better translation yet still have the problem with the counting portion of the code.  Again, please suggest better options as I am sure I am handling the multi receiver part of this all wrong.  The program seems to work pretty well, but if this is indeed the right way to handle the multi receiver part of the code, by the time I type in for all 4 receivers it seems a little much.  Anyways, here is what I have to date.

Code: [Select]
#include "IRremote.h"

int receiver = 12; // Signal Pin of IR receiver to Arduino Digital Pin 12
int receiver2 = 10;
int receiver3 = 9;
int Blue = 11;
int Red = 7;

int lap_count1;
int lap_count2;

int latchPin = 4;
int clockPin = 5;
int dataPin = 3;

unsigned long StartMS;
unsigned long CurrentMS;
unsigned long LastIR;
unsigned long LastIR2;
unsigned long LastIR3;
unsigned long CurrentIR;
unsigned long CurrentIR2;
unsigned long CurrentIR3;
unsigned long ignorePeriod = 10000;
bool ignoreSameCode = false;
bool ignoreSameCode2 = false;



byte segdisp[10] = {          B11111100,
                              B01100000,
                              B11011010,
                              B11110010,
                              B01100110,
                              B10110110,
                              B10111110,
                              B11100000,
                              B11111110,
                              B11100110,
 
   };


/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);     // create instance of 'irrecv'
IRrecv irrecv2(receiver2);
IRrecv irrecv3(receiver3);
decode_results results;      // create instance of 'decode_results'
decode_results results2;
decode_results results3;

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);
  Serial.println("Ready to Detect");
  irrecv.enableIRIn(); // Start the receiver
  irrecv2.enableIRIn();
  irrecv3.enableIRIn();
  pinMode(Blue, OUTPUT);
  pinMode(Red, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  digitalWrite(latchPin, LOW);                        // send to 7 segment displays
  shiftOut(dataPin, clockPin, LSBFIRST, B11111100);
  shiftOut(dataPin, clockPin, LSBFIRST, B11111100);                     
  digitalWrite(latchPin, HIGH);
   


}/*--(end setup )---*/



/*-----( Function )-----*/
void translateIR() {// takes action based on IR code received
  int a;
  int b;
  int c;
  int d;
  a = lap_count1%10;
  b = lap_count1/10;
  c = lap_count2%10;
  d = lap_count2/10;
 
 


{

  switch(results.value)  // describe IR codes and what they will do
 
  {
     
   
  case 0x13:
  // BLUE button on tape light remote
  Serial.println("Driver 1");
  digitalWrite(Blue, HIGH);
  lap_count1 ++;
  Serial.println("Lap");
  Serial.println(lap_count1);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, segdisp[a]);
  shiftOut(dataPin, clockPin, LSBFIRST, segdisp[b]);
  digitalWrite(latchPin, HIGH);
  digitalWrite(Blue, LOW);
  break;
     

  case 0x14:   
  // RED button on tape light remote
  Serial.println("Driver 2");
  digitalWrite(Blue, HIGH);
  lap_count2 ++;
  Serial.println("Lap");
  Serial.println(lap_count2);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, segdisp[c]);
  shiftOut(dataPin, clockPin, LSBFIRST, segdisp[d]);
  digitalWrite(latchPin, HIGH);
  digitalWrite(Blue, LOW);
  break;

  case 0xFFA25: Serial.println("Reset");   // Play/Pause button on tape light remote
  digitalWrite(Red, HIGH);
  lap_count1 = 0;
  lap_count2 = 0;
  Serial.println(lap_count1, lap_count2);
   digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, B11111100);
  shiftOut(dataPin, clockPin, LSBFIRST, B11111100);
  digitalWrite(latchPin, HIGH);
   delay(1000);
  digitalWrite(Red, LOW);
  break;
   

  default:
    digitalWrite(Red, HIGH);
    delay(5);
    digitalWrite(Red, LOW);

  }// End Case
}
}

void loop() {  /*----( LOOP: RUNS CONSTANTLY )----*/
 
  CurrentMS = millis();
  if (irrecv.decode(&results) || irrecv2.decode(&results2))  //signal received
  {
    irrecv.resume();
    irrecv2.resume();
    CurrentIR = results.value;
    CurrentIR2 = results2.value;
    if (CurrentIR == 0xFFFFFFFF || CurrentIR2 == 0xFFFFFFFF)
    {
      return;
    }
    debugReport();
    if ((CurrentIR != LastIR || ignoreSameCode == false) || (CurrentIR2 != LastIR2 || ignoreSameCode2 == false))   //we have a new code
    {
    Serial.print("\t\tNew code : ");
    Serial.println(CurrentIR, HEX);
    Serial.println(CurrentIR2, HEX);   
    StartMS = CurrentMS;
    ignoreSameCode = true;
    ignoreSameCode2 = true;
    Serial.println("wait period started");
    translateIR();
    }
    LastIR = CurrentIR;
    LastIR2 = CurrentIR2;
  }
    if (((ignoreSameCode == true) && CurrentMS - StartMS >= ignorePeriod) || ((ignoreSameCode2 == true) && CurrentMS - StartMS >= ignorePeriod))
    {
      ignoreSameCode = false;
      ignoreSameCode2 = false;
      Serial.println("wait period over");
     
  }
   
}
void debugReport()
{
Serial.print("currentCode : ");
  Serial.println(CurrentIR, HEX);
  Serial.print("currentCode2 :");
  Serial.println(CurrentIR2, HEX);
  Serial.print("previousCode : ");
  Serial.println(LastIR, HEX);
  Serial.print("previousCode2 :");
  Serial.println(LastIR2, HEX);
  Serial.print("ignoreSameCode : ");
  Serial.println(ignoreSameCode == false ? "false" : "true");
  Serial.println(); 
}

Go Up