title: correct way to include .cpp and .h files in an Arduino sketch
body: <p>First, the problem:</p>
  
  <p>main sketch file:</p>
  
  <pre><code>char foo;            // required to clean up some other problems
  #include &lt;Arduino.h&gt; // tried it in desperation, no help
  #include "a.h"
  
  void setup(){
    Serial.begin(9600);
    Serial.println("\nTest begins");
    for (int num = -1; num &lt; 1; num++){
      Serial.print(num);
      if (isNegative(num)){
        Serial.println(" is negative");
      } else {
        Serial.println(" is NOT negative");
      }
    }
  }
  
  void loop(){}
  </code></pre>
  
  <p>// a.h</p>
  
  <pre><code>#ifndef H_A
  #define H_A
  
  boolean isNegative(int x);                  // Err#1
  int anotherOdity();
  
  #endif // H_A
  </code></pre>
  
  <p>// a.cpp</p>
  
  <pre><code>#include "a.h"
  
  int isNegative(int x){
    Serial.println("I can't print this from inside my INCLUDE FILE"); //Err#2
    if (x&lt;0) return true;
    return false;
  }
  
  int anotherOdity(){
    char ch[5];
    memcpy(ch,"1",1);  //doesn't work, memcpy not declared      // Err#3
  }
  </code></pre>
  
  <p>The above, as is, doesn't compile and these are the errors I get:</p>
  
  <pre><code>In file included from a.cpp:1:
  a.h:4: error: 'boolean' does not name a type
  a.cpp: In function 'int isNegative(int)':
  a.cpp:4: error: 'Serial' was not declared in this scope
  a.cpp: In function 'int anotherOdity()':
  a.cpp:11: error: 'memcpy' was not declared in this scope
  </code></pre>
  
  <p>The first problem is the boolean type, seems to suffer from some name mangling that the Arduino environment does, but that is generally fixed by the <code>char foo;</code> in the main file. And in certain situations, it is. But to use that type in the <code>.cpp</code> file generates this error. </p>
  
  <p>I can see that Errors 2 and 3 are related, but how do I get these in scope? I realise that part of the problem is probably the <code>#include</code> itself (maybe) because <code>Serial</code> and <code>memcpy</code> aren't yet defined/declared? I tried including the the <code>Arduino.h</code> library, but that didn't help. Actually, it did help the boolean problem but only in the case of putting everything in the <code>.h</code> file (as I discuss further below), it doesn't help the above example.</p>
  
  <p>If I put the three files together and have everything in the main sketch (<code>.ino</code>) file, it works as it should. But the idea here is that I want to break out some code and make my sketch more readable. </p>
  
  <p>The closest I got to a solution was found here: <a href="http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/" rel="noreferrer">http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/</a> where, after running my own tests, I determined that if I put EVERYTHING in a <code>.h</code> file, it works! </p>
  
  <p>For example, leaving the main sketch file unchanged, if I delete <code>a.cpp</code> and create just <code>a.h</code> (as follows) it works! </p>
  
  <pre><code>#ifndef H_A
  #define H_A
  
  boolean isNegative(int x){
    Serial.println("I can't print this from inside my INCLUDE FILE");
    if (x&lt;0) return true;
    return false;
  }
  
  int anotherOdity(){
    char ch[5];
    memcpy(ch,"1",1);  //doesn't work, memcpy not declared
  }
  
  #endif // H_A
  </code></pre>
  
  <p>This fixes the boolean problem (well.... I still need <code>Arduino.h</code> or <code>char foo;</code>), and it fixes the scope issues. </p>
  
  <p>But it just feels wrong. </p>
  
  <p>This isn't about creating a library of standard functions I could use across various sketches, it's all about breaking my code into smaller (readable) chunks, and keeping them all together in the project folder. I want to do this in the most correct way possible, it just seems to be I'm limited by the IDE. I'm sure I have a suitable understanding of how to put a header and associated <code>.cpp</code> file together (I hope that I have't got that part wrong). </p>
  
  <p>I am totally self taught with everything C/C++ and have only really got into programming micros very recently.</p>
  
  <p>I have researched this through the depths of google and am just continually coming up short. </p>
  
  <p>Without resorting to <code>hacks</code> and keeping it simple for folk like me, how can I best put together the above examples so that the Arduino IDE/gcc will compile it?</p>
  
  <p>edit: I thought I would include just SOME of the tabs I have open here to show that I really have done some research on this!</p>
  
  <p><a href="http://arduino.cc/en/Reference/Include" rel="noreferrer">http://arduino.cc/en/Reference/Include</a></p>
  
  <p><a href="http://arduino.cc/en/Hacking/LibraryTutorial" rel="noreferrer">http://arduino.cc/en/Hacking/LibraryTutorial</a></p>
  
  <p><a href="http://forum.arduino.cc/index.php/topic,124904.msg938861.html#msg938861" rel="noreferrer">http://forum.arduino.cc/index.php/topic,124904.msg938861.html#msg938861</a></p>
  
  <p><a href="http://forum.arduino.cc/index.php?topic=84412.0" rel="noreferrer">http://forum.arduino.cc/index.php?topic=84412.0</a> (this is where I found the <code>char foo;</code> solution)</p>
  
  <p><a href="http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/" rel="noreferrer">http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/</a></p>
  
  <p><a href="https://stackoverflow.com/questions/19547091/including-cpp-files">Including .cpp files</a></p>
  
  <p><a href="https://stackoverflow.com/questions/4705790/keeping-all-libraries-in-the-arduino-sketch-directory?rq=1">Keeping all libraries in the Arduino sketch directory</a></p>
  
  <p><a href="https://stackoverflow.com/questions/8893421/c-header-and-cpp-includes?lq=1">C++ Header and CPP includes</a></p>