Help with shift bits...

Hello.How work this?

#define IRpin_PIN      PIND
#define IRpin             2
 
//  while (digitalRead(IRpin)) { // this is too slow!
    while (IRpin_PIN & (1 << IRpin)) {
     // pin is still HIGH
 
     // count off another few microseconds
     highpulse++;

This is from http://www.ladyada.net/learn/sensors/ir.html

1 is shifted left IRpin places. Since IRpin is defined to have a value of 2, the shifted value will be 4. That 4 will be ANDed with the value of IRpin_PIN and the result will be 4 if the corresponding bit of IRpin_PIN is also a 1, and otherwise will be zero.

I can't understand how pin is still high ....the pulse is 01010101....38 KHz....?

The counter work when pin2 something there is ....the pulse?

post all you code not just a tiny bit of it.

/* Raw IR decoder sketch!
 
 This sketch/program uses the Arduno and a PNA4602 to 
 decode IR received. This can be used to make a IR receiver
 (by looking for a particular code)
 or transmitter (by pulsing an IR LED at ~38KHz for the
 durations detected 
 
 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
 
// 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 
 
// we will store up to 100 pulse pairs (this is -a lot-)
uint16_t pulses[100][2];  // pair is high and low pulse 
uint8_t currentpulse = 0; // index for pulses we're storing
 
void setup(void) {
  Serial.begin(9600);
  Serial.println("Ready to decode IR!");
}
 
void loop(void) {
  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
     if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
       printpulses();
       currentpulse=0;
       return;
     }
  }
  // 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);
     if ((lowpulse >= MAXPULSE)  && (currentpulse != 0)) {
       printpulses();
       currentpulse=0;
       return;
     }
  }
  pulses[currentpulse][1] = lowpulse;
 
  // we read one high-low pulse successfully, continue!
  currentpulse++;
}
 
void printpulses(void) {
  Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
  for (uint8_t i = 0; i < currentpulse; i++) {
    Serial.print(pulses[i][0] * RESOLUTION, DEC);
    Serial.print(" usec, ");
    Serial.print(pulses[i][1] * RESOLUTION, DEC);
    Serial.println(" usec");
  }
}

So what sort of arduino do you have?

Arduino Duemilanove Mega328

If i remove delay from the code....i measurement the high pulse general or only the total time when the bit is 1 high for 13usecs (38KHz)?

Arduino work with 16MHz....every cycle is 62.5 nanoseconds....i could use this for more precision?

Arduino Duemilanove Mega328

OK so the pin mapping is still valid.

If i remove delay from the code

You will measure the high period but it will not be easy to say what units they are in. At the moment the delay dominates the rest of the code and so you can just ignore what time it takes to do that and simply use the count times the delay to get your pulse width.

I am not sure what you are receiving, have you got a print out of the results this program gives. What are you expecting it to give?

toyotomi a/c on pulse with resolution 20usec

Ready to decode IR!

Received:

OFF ON 45204 usec, 8940 usec 4420 usec, 760 usec 400 usec, 680 usec 420 usec, 680 usec 1500 usec, 680 usec 1520 usec, 700 usec 1500 usec, 680 usec 400 usec, 700 usec 400 usec, 680 usec 420 usec, 680 usec 400 usec, 700 usec 400 usec, 680 usec 1500 usec, 700 usec 1500 usec, 680 usec 1500 usec, 680 usec 420 usec, 680 usec 400 usec, 700 usec 400 usec, 680 usec 1500 usec, 700 usec 420 usec, 660 usec 440 usec, 680 usec 1500 usec, 680 usec 1500 usec, 700 usec 400 usec, 680 usec 1500 usec, 700 usec 400 usec, 680 usec 420 usec, 680 usec 400 usec, 700 usec 1500 usec, 680 usec 400 usec, 700 usec 400 usec, 680 usec 420 usec, 680 usec 400 usec, 700 usec 400 usec, 680 usec 420 usec, 680 usec 400 usec, 700 usec 1500 usec, 680 usec 1520 usec, 660 usec 1500 usec, 720 usec 1500 usec, 680 usec 1500 usec, 680 usec 1500 usec, 700 usec 400 usec, 680 usec 440 usec, 660 usec 400 usec, 700 usec 400 usec, 680 usec 420 usec, 680 usec 400 usec, 700 usec 1500 usec, 680 usec 1520 usec, 660 usec 1520 usec, 700 usec 1500 usec, 680 usec 1500 usec, 680 usec 1500 usec, 700 usec 400 usec, 680 usec 440 usec, 660 usec 400 usec, 700 usec 400 usec, 680 usec 440 usec, 660 usec 400 usec, 700 usec 420 usec, 660 usec 420 usec, 680 usec 400 usec, 700 usec 1520 usec, 660 usec 400 usec, 700 usec 1520 usec, 660 usec 1520 usec, 680 usec 420 usec, 680 usec 1500 usec, 680 usec 420 usec, 500 usec

when i decrease resolution to 1 usec the time 400usec became 250usecs...

the toyotomi ok on but i have another a/c united no work...

Ok so what is wrong with that information, it looks good to me. What are you expecting it to show you?

I used the same stuff on Sunday, didn’t quite work out for me… Here’s what I ended up with:

It’s not quite correct, but close enough (can resend IR signals from remote 95% of the time…)

/* Raw IR decoder sketch!
 
 This sketch/program uses the Arduno and a PNA4602 to 
 decode IR received. This can be used to make a IR receiver
 (by looking for a particular code)
 or transmitter (by pulsing an IR LED at ~38KHz for the
 durations detected 
 
 */
 
#define IRpin          2

#define irLed          9
 
// the maximum pulse we'll listen for - 65 milliseconds is a long time
#define MAXPULSE 65000
 
// we will store up to 100 pulse pairs (this is -a lot-)
unsigned long pulses[100][2];  // pair is high and low pulse 
int currentpulse = 0; // index for pulses we're storing

unsigned long time;
unsigned long timeCmp;

void setup(void) {
  Serial.begin(9600);
  
  pinMode(irLed, OUTPUT);
  delay(200);
  digitalWrite(irLed, LOW);
}

 
void loop() {
  
 if (currentpulse >= 100) {
       stopReading();
  }
 //Serial.println("Waiting for data!");
 // wait for data..
  while (currentpulse == 0 && digitalRead(IRpin) ) {
  
  }
 
  time = micros();
  while (!digitalRead(IRpin)) { 
    // 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
     timeCmp = micros() - time;
     if (timeCmp > MAXPULSE) {
       stopReading();
       return;
     }
  }
  // we didn't time out so lets stash the reading
  pulses[currentpulse][0] = timeCmp;
 
  // same as above
  time = micros();
  while (digitalRead(IRpin)) {
     timeCmp = micros() - time; 
     if (timeCmp > MAXPULSE) {
       stopReading();
       return;
     }
  }
  pulses[currentpulse][1] = timeCmp;
 
  // we read one high-low pulse successfully, continue!
  currentpulse++;
}

void stopReading() {
  if (currentpulse != 0) {
    printpulses();
    currentpulse=0;
  }
}

 
void printpulses(void) {

  
  // print it in a 'array' format
  Serial.print("int IRsignal[] = {");
  for (int i = 0; i <= currentpulse; i++) {

    Serial.print(pulses[i][0]);
    Serial.print(", ");
    Serial.print(pulses[i][1]);
    Serial.print(", ");
  }
 
  Serial.println("-1};");
  delay(1000);
  unsigned long now = micros();
 
  for (int i = 0; i < currentpulse; i++) {
    pulseIR(pulses[i][0]);
     delayMicroseconds(pulses[i][1] - 10);
  }
   now = (micros() - now);

 
  Serial.print("Sent stuff, took in ms ");
  Serial.println(now / 1000);
  
  unsigned long shouldTake = 0;
   for (int i = 0; i < currentpulse; i++) {
    shouldTake += (pulses[i][0] / 10);
    shouldTake += (pulses[i][1] / 10);
  }
   Serial.print("Should take in ms ");
  Serial.println(shouldTake / 100);
    Serial.println("");
}

void pulseIR(unsigned long microsecs) {
   if (!microsecs) { return; }
   unsigned long start = micros();
   digitalWrite(irLed, HIGH);  
   
      // 38 kHz is about 13 microseconds high and 13 microseconds low
   unsigned long cdiff = 0;
  while (micros() - start < microsecs) {
     digitalWrite(irLed, HIGH);
     cdiff += 13;
     while (micros() - start < cdiff);
     
     digitalWrite(irLed, LOW);  
      cdiff += 13;
     while (micros() - start < cdiff);
   
  }
}

void sendPulses(const int pulses[]) {
   int i = 0;
   while (pulses[i] > 0) {
   
    pulseIR(pulses[i++]);
    delayMicroseconds(pulses[i++] - 50);
  }
}

My basic question is why pin 2 still high?The Pulse Ir is high ,low every 13usec when fall to zero what happen ?How read this?The pin arduino or ir receiver has internal small capacitor?

My basic question is why pin 2 still high

I do not understand what you mean by this? What is the problem you have?

When send a pulse IR is a pulse 13usec High,13usecs Low,13usec High,13usecs Low,13usec High,13usecs Low,13usec High,13usecs Low....for 700usecs the loop work counter when pulse is high only for 13usecs or for total time 700usecs?

the looop start and stop again or still countdown ?

When send a pulse IR is a pulse 13usec High,13usecs Low,13usec High,13usecs Low,13usec High,13usecs Low,13usec

No that is not what the IR is sending.

The IR is a modulated signal, that is when it wants to send a logic one it sends a 13 uS high and 13uS low. If it is modulated at 38KHz. When it sends a zero it sends nothing. The IR remote will send a stream of zeros and ones. You will see this in the program as a low determined by the number of zeros followed by a 13uS high. You are seeing a 680uS high. This suggests that either:- 1) your modulation frequency is 735Hz 2) the code is not accurate at timing the pulses