Let's Make Robots!

Boolean with arduino

Hi i have a question about boolean for arduino. Where does the whole boolean statement go? in void loop? I am trying to use this code http://www.ladyada.net/learn/sensors/ir.html - the second to last part to be specific. However, it does not say where to put it.

i just put it at its own thing but this error comes up "a function definintion is not allowed here before '{' token" with this-  boolean IRcompare(int numpulses, int Signal[]) { - highlighted

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

If you are talking about the function that starts with
boolean IRcompare(int numpulses, int Signal[]) {

You can put the whole function after the main loop as the main program call IRcompare and returns a Boolean.

You could look here. It's running code. Used for this IR control.

i did that but i get this eror  "a function definintion is not allowed here before '{' token"  and with this-  boolean IRcompare(int numpulses, int Signal[]) { - highlighted

There is a distinction between a function definition and usage. Define the function after the loop function as Geir stated. Use it wherever you want as shown in the sample code I mentioned.

 

void loop(void) {
  int numberpulses = listenForIR();
  ...  
  if (IRcompare(numberpulses, IRsignal_LEFT_ChannelA)) {
    Serial.println("LEFT");
    digitalWrite(LEFT_PIN, HIGH); // set the LED on
  }
...
}// end of loop function

boolean IRcompare(int numpulses, int Signal[]) {
... 
}

Seems like you are pasting the function in on the wrong place.
You probably should select your complete code and paste it into this thread (try copy as HTML)

int Volumedown[] = {

// ON, OFF (in 10's of microseconds)

884, 434,

58, 52,

58, 52,

58, 160,

60, 50,

60, 50,

58, 52,

58, 52,

58, 50,

58, 162,

58, 162,

58, 50,

58, 162,

58, 162,

58, 162,

58, 160,

60, 160,

58, 162,

58, 162,

58, 50,

58, 52,

58, 52,

58, 52,

58, 50,

58, 52,

58, 52,

58, 50,

60, 160,

58, 162,

58, 162,

58, 160,

60, 160,

58, 162,

58, 3954,

884, 218,

58, 2892,

884, 218,

56, 0};

 

int Volumeup[] = {

// ON, OFF (in 10's of microseconds)

884, 434,

58, 52,

58, 52,

58, 162,

58, 50,

58, 52,

58, 52,

58, 50,

60, 50,

58, 162,

58, 162,

58, 50,

58, 162,

58, 162,

58, 162,

58, 160,

58, 162,

58, 52,

58, 162,

58, 50,

58, 52,

58, 52,

58, 50,

60, 50,

58, 52,

58, 162,

58, 52,

58, 160,

58, 162,

58, 162,

58, 160,

58, 162,

58, 162,

58, 3954,

884, 218,

58, 2892,

884, 216,

60, 2892,

884, 218,

56, 2894,

884, 216,

58, 0};

 

int Channelup[] = {

// ON, OFF (in 10's of microseconds)

884, 434,

58, 52,

58, 52,

56, 162,

58, 52,

58, 52,

58, 50,

60, 50,

58, 52,

58, 162,

58, 162,

56, 52,

58, 162,

58, 162,

58, 160,

60, 160,

58, 162,

58, 52,

58, 52,

56, 52,

58, 52,

58, 52,

58, 50,

58, 52,

58, 52,

58, 160,

58, 162,

58, 162,

58, 162,

58, 160,

60, 160,

58, 162,

58, 162,

58, 3954,

884, 218,

58, 2892,

884, 216,

58, 0};

int Channeldown[] = {

// ON, OFF (in 10's of microseconds)

884, 436,

58, 50,

60, 50,

58, 162,

58, 52,

58, 50,

58, 52,

58, 52,

58, 52,

58, 160,

58, 162,

58, 52,

58, 160,

58, 162,

58, 162,

58, 162,

58, 160,

60, 160,

60, 50,

58, 52,

58, 52,

58, 50,

58, 52,

58, 52,

58, 50,

60, 50,

58, 162,

58, 162,

58, 160,

58, 162,

58, 162,

58, 162,

58, 160,

58, 3954,

884, 218,

58, 2894,

884, 216,

58, 0};

#define IRpin_PIN      PIND

#define IRpin          2

 #define FUZZINESS 20

// 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) {

  int numberpulses;

 

  numberpulses = listenForIR();

 

  Serial.print("Heard ");

  Serial.print(numberpulses);

  Serial.println("-pulse long IR signal");

  if (IRcompare(numberpulses, Channelup )) {

    Serial.println("Forward");

  }

    if (IRcompare(numberpulses, Channeldown)) {

    Serial.println("Backward");

  }

    if (IRcompare(numberpulses, Volumeup)) {

    Serial.println("Left");

  }

 

  if (IRcompare (numberpulses, Volumedown)) {

    Serial.println ("Right");

}

 

boolean IRcompare(int numpulses, int Signal[]) {

 

 for (int i=0; i< numpulses-1; i++) {

 

    int oncode = pulses[i][1] * RESOLUTION / 10;

 

    int offcode = pulses[i+1][0] * RESOLUTION / 10;

 

     if ( abs(oncode - Signal[i*2 + 0]) <= (Signal[i*2 + 0] * FUZZINESS / 100)) {

    } else {

    return false;

 

    if ( abs(offcode - Signal[i*2 + 1]) <= (Signal[i*2 + 1] * FUZZINESS / 100)) {

 

   } else {

 

     return false;

 

 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

       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);

       if ((lowpulse >= MAXPULSE)  && (currentpulse != 0)) {

         return currentpulse;

       }

    }

    pulses[currentpulse][1] = lowpulse;

 

    // we read one high-low pulse successfully, continue!

    currentpulse++;

}}

There is a missing } before the boolean IRcompare(int numpulses, int Signal[]). This is why the compiler thinks you try to define a function inside the loop function. Add this bracket and remove the last bracket } of the file... and then you are fine.

Thanks u are a genius

would you know why i am getting completly diffirent readings from my reciever?