What is "uint16_t" and what is it's largest value ?

Am starting to encounter sample code/language that uses "non-native Arduino" and am wondering what it's characteristics are, i bravely :stuck_out_tongue: ventured to http://www.cplusplus.com/doc/tutorial/variables/ but couldn't really get much of it.

Why i ask is because i'm guessing it's to do with that where my code encounters a problem.

i'll probably have to post the whole code (it's quite a lot*) but for now i'll focus on this first;

  • = it's actually the IR-commander code from the Adafruit IR-tutorial website
#define NUMPULSES 50
...
...
uint16_t pulses[NUMPULSES][2];
...

void setup() {
  Serial.begin(9600);
  Serial.println("Ready to begin!");

}

the problem occurs when i just increase the NUMPULSES from 50 to 70, the program just hangs - it doesn't even get past the setup() - which is why i'm assuming it's to do with the lines "up here".

the code compiles without any problem so what am i missing here ?

uint16_t is a datatype that's unsigned and is 16 bits wide. So, the maximum value is 2^16, or 65535.

pulses is a 2 dimensional array, so it's got NUMPULSES arrays of two uint16_t's. Each uint16_t is two bytes, so at NUMPULSES=50 you're using 5022 = 200 bytes, at 70 it's 280 bytes. Depending on what else is in the program, that might be pushing past the available memory for your board. Posting all the code and what board you're using would help.

Yes:
uint16_t is a datatype that's unsigned and is 16 bits wide. So, the maximum value is 2^16, or 65535.

pulses is a 2 dimensional array, so it's got NUMPULSES arrays of two uint16_t's. Each uint16_t is two bytes, so at NUMPULSES=50 you're using 5022 = 200 bytes, at 70 it's 280 bytes. Depending on what else is in the program, that might be pushing past the available memory for your board. Posting all the code and what board you're using would help.

hmm, so well within the range - i tried to compile the code twice, once each with the 2 values, and they both resulted in

Binary sketch size: 4,908 bytes (of a 32,256 byte maximum)

okay then, here's the code !

/* Raw IR commander
 
 This sketch/program uses the Arduno and a PNA4602 to decode IR received. 
 It then attempts to match it to a previously recorded IR signal
 Code is public domain, check out www.ladyada.net and adafruit.com for more tutorials! 
*/

// We need to use the 'raw' pin reading methods
// because timing is very important here
// and the digitalRead() procedure is slower!
//uint8_t IRpin = 2;
// Digital pin #2 is the same as Pin D2 see
// http://arduino.cc/en/Hacking/PinMapping168 for the 'raw' pin mapping
#define IRpin_PIN      PIND
#define IRpin          2

// the maximum pulse we'll listen for - 65 milliseconds is a long time
#define MAXPULSE 65000
//#define NUMPULSES 50 (need to increase for Pioneer remote)
//                     BUT WHY IT FAILS TO WORK THEN !! ??
#define NUMPULSES 70

// 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[NUMPULSES][2];  // pair is high and low pulse 
uint8_t currentpulse = 0; // index for pulses we're storing

#include "ircodes.h"

// to switch DEBUG mode on(1) or off (0)
//#define DEBUG // it's not boolean, it's whether defined AT ALL !

void setup() {
  Serial.begin(9600);
  Serial.println("Ready to decode IR!");
}

void loop() {
//  int numberpulses;  
  //numberpulses = listenForIR();
    int numberpulses = listenForIR();

  Serial.print("Heard ");
  Serial.print(numberpulses);
  Serial.println("-pulse long IR signal");
  
  if (IRcompare(numberpulses, pioneerUP,sizeof(pioneerUP))) {
    Serial.println("UP");
  }
  else if (IRcompare(numberpulses, pioneerDN,sizeof(pioneerDN))) {
    Serial.println("DOWN");
  }
  else if (IRcompare(numberpulses, pioneerLF,sizeof(pioneerLF))) {
    Serial.println("LEFT");
  }
  else if (IRcompare(numberpulses, pioneerRT,sizeof(pioneerRT))) {
    Serial.println("RIGHT");
  }
  else if (IRcompare(numberpulses, pioneerSTOP,sizeof(pioneerSTOP))) {
    Serial.println("STOP");
  }
  else  {
    Serial.println("Code NOT recognised");
  }
  delay(500);
}

//KGO: added size of compare sample. Only compare the minimum of the two
  
boolean IRcompare(int numpulses, int Signal[], int refsize) {
  int count = min(numpulses,refsize);
  Serial.println("");  // should just use ESC code in the next line ! forgot what tho : \n ??
  Serial.print("ENTERingIRcompareFUNC=> count set to: ");
  Serial.println(count);
  for (int i=0; i< count-1; i++) {
    int oncode = pulses[i][1] * RESOLUTION / 10;
    int offcode = pulses[i+1][0] * RESOLUTION / 10;
    
#ifdef DEBUG    
    Serial.print(oncode); // the ON signal we heard
    Serial.print(" - ");
    Serial.print(Signal[i*2 + 0]); // the ON signal we want 
#endif   
    
    // check to make sure the error is less than FUZZINESS percent
    if ( abs(oncode - Signal[i*2 + 0]) <= (Signal[i*2 + 0] * FUZZINESS / 100)) {
#ifdef DEBUG
      Serial.print(" (ok)");
#endif
    } else {
#ifdef DEBUG
      Serial.print(" (x)");
#endif
      Serial.print("Variance in ON");
      // we didn't match perfectly, return a false match
      return false;
    }
        
#ifdef DEBUG
    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 
#endif    
    
    if ( abs(offcode - Signal[i*2 + 1]) <= (Signal[i*2 + 1] * FUZZINESS / 100)) {
#ifdef DEBUG
      Serial.print(" (ok)");
#endif
    } else {
#ifdef DEBUG
      Serial.print(" (x)");
#endif
      Serial.print("Variance in OFF");
      // we didn't match perfectly, return a false match
      return false;
    }
    
#ifdef DEBUG
    Serial.println();
#endif
  }
  // 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
       
       // KGO: Added check for end of receive buffer    ME: REMOVED - revert to Original Tutorial code - AND THEN REVERTED coz DIDN'T WORK !!
       if (((highpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES)  {
       //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);
        // KGO: Added check for end of receive buffer  ME: same as above for 'highpulse'
        if (((lowpulse >= MAXPULSE)  && (currentpulse != 0))|| currentpulse == NUMPULSES) {
        //if ((lowpulse >= MAXPULSE) && (currentpulse != 0))  {
         return currentpulse;
       }
    }
    pulses[currentpulse][1] = lowpulse;

    // we read one high-low pulse successfully, continue!
    currentpulse++;
  }
}

and a sample array which is in ircodes.h

/*********** THE codes ***********/

 
int pioneerUP[] = {	   
// ON, OFF (in 10's of microseconds) 
838,416,	   
50,158,	   
56,152,	   
52,52,	   
52,52,	   
50,52,	   
54,156,	   
50,54,	   
52,156,	   
52,50,	   
54,52,	   
52,154,	   
54,156,	   
52,156,	   
54,52,	   
52,154,	   
54,52,	   
54,152,	   
54,52,	   
50,52,	   
52,158,	   
50,158,	   
52,52,	   
50,52,	   
54,156,	   
50,52,	   
54,156,	   
52,156,	   
54,50,	   
52,50,	   
54,156,	   
52,156,	   
54,52,	   
50,2496,	   
832,420,	   
50,156,	   
54,154,	   
54,156,	   
52,156,	   
52,52,	   
52,156,	   
54,52,	   
50,156,	   
54,52,	   
50,52,	   
52,52,	   
54,50,	   
52,156,	   
54,50,	   
52,156,	   
54,52,	   
50,52,	   
52,158,	   
50,52,	   
52,52,	   
54,154,	   
54,156,	   
50,158,	   
52,156,	   
52,156,	   
52,52,	   
52,158,	   
50,156,	   
54,52,	   
50,52,	   
52,52,	   
54,50,	   
52,0};

okay, this time the code managed to get into setup() but it got as far as the display of ;

H³ªV?åto decode IR!

could a faulty IR-receiver be a possible cause ?
i did already try the shiriff IRremote-library but was getting inconsistent codes, which is why i ended up trying the Adafruit way, a bit more complex but a lot more learned !

for what it's worth, with the shiriff Library "IRrecvDemo" i got extraneous data like;

FD8877
FFFFFFFF
FFFFFFFF
FFFFFFFF
6F5974BD
FFFFFFFF
FFFFFFFF
FFFFFFFF
FFFFFFFF
FFFFFFFF
6F5974BD
FFFFFFFF

and

DEE522C1
FFFFFFFF
DEE522C1
FD08F7
DEE522C1
FD08F7
FFFFFFFF
FD
FFFFFFFF
FD
FFFFFFFF

the HEX codes in Blue are the relevant ones.

i had already got (more) consistent readings with a cheap remote (with the IR kit) and decided to play with a TV remote !
that's why i need to read more pulses.