user8: I want to make a sketch that is as small as possible, for test purposes. The problem is, when i compile the BareMinimum sketch (with an empty setup and loop), i get 466 bytes for the Uno and a whopping 4,242 for the Leonardo. Is there any way to write your own code that has no extra functions (namely Timer0 for millis() and delay()). I would also like to be able to disable the keyboard/mouse functions for the Leonardo.

user220: Although it depends on your sketch, you can somewhat decrease the size by reusing code with methods.
Take this code:
int led = 13;
int val;

void setup() {                
  pinMode(led, OUTPUT);     
}

void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second

  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second

  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second

  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second

  val = digitalRead(10);
}

1,322 bytes on Arduino Uno. Now let's shrink it a bit:
int led = 13;
int val;

void setup() {                
  pinMode(led, OUTPUT);     
}

void loop() {
  for(uint8_t i = 0; i < 8; i++) {
    blink(HIGH);
    blink(LOW);
  }    
  val = digitalRead(10);
}

void blink(uint8_t state) {
  digitalWrite(led, state);   // turn the LED to the right state
  delay(1000);                // wait for a second
}

1,194 bytes. That's about a 10% decrease!
Anyway, although it doesn't greatly shrink a sketch, it sometimes can be the easiest route when you are two bytes over the limit, or just want to make a more compact sketch to begin with without loosing any functionality. It's not for every situation, but I find it useful sometimes.

user60: You should be able to create your own board definition with a custom boards.txt file as per https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5---3rd-party-Hardware-specification. As I see there are several usb features in the leonardo's definition. I would hope that the compiles inclusion of the 4K is based off of these flags and not the processor type.
Where the boards.txt would use the upload, bootloader sections from the Leonardo and the build from the uno.
This is all assuming the core library build does not use processor specific flags to include USB functions.
If you get such working. post back, I am sure others would be interested in such.

I recently ran into this 4K used limitation on a library demo that actually maxed out the UNO and had to put in a
#if !defined(__AVR_ATmega32U4__)
...

around a large chunk of extra features in the sketch to fit on the Leonardo.
I had assume (wrong) that this 4K was because I still included Serial.print which when over the USB's CDC on the Leo. But I see after a memory dump of an empty sketch they are still there.
C:\Users\mflaga\AppData\Local\Temp\build8958339595868119500.tmp>avr-objdump -d sketch_feb13a.cpp.elf > sketch_feb13a.cpp.elf.lst

Which makes sense. As the Leonardo still requires the USB-CDC client (the 4K) as to detect the 1200 Baud connect from AVR-DUDE as to stroke the remote reboot.

Hence making a custom boards.txt without USB in the build, also needs to have
leonardo.upload.use_1200bps_touch=true

removed.
Once loaded on the target, this would require the upload to be synchronized with a manual reset of the target. As the ability to remote reboot is lost.

user86: I recently wanted to do exactly this. Since there is no nice way to do so, I wound up writing a patch for the Stino sublime-text arduino plugin to do exactly this. It's subsequently been accepted, so it should be in any up-to-date Stino installs.
This adds a new option to Stino:  

Using this mode produces compilation results like the following:  
For a Uno:

Binary sketch size: 172 bytes (of a 32256 byte maximum, 0.53 percent).
  Estimated memory use: 0 bytes (of a 1024 byte maximum, 0.00 percent).

For a leonardo

Binary sketch size: 240 bytes (of a 28672 byte maximum, 0.84 percent).
  Estimated memory use: 0 bytes (of a 2560 byte maximum, 0.00 percent).

Actually programming the leonardo with the above compiled output is probably a bad idea, as it might break the auto-reset functionality, but you could, if you wanted. Hat-tip to mpflaga for noting this in his answer.
Note that the memory reports are actually incorrect, but that's a separate issue.
The code used for the above is:  
int main()
{
    while (1)
    {

    }
}

Some notes:

You ain't writing a "Sketch" anymore, not that you ever actually do write a sketch. You write programs. Period. I don't care what the Arduino wackos want to say, they don't get to redefine terms.
All interrupt management is manual. This means no milis() or similar.
You can still use the arduino serial libraries and so forth, if you want. You do have to #include <Arduino.h>.
You define main. You don't ever return from main. If you want setup stuff, it goes before the while (1).


8: <p>I want to make a sketch that is as small as possible, for test purposes. The problem is, when i compile the BareMinimum sketch (with an empty setup and loop), i get 466 bytes for the Uno and a whopping 4,242 for the Leonardo. Is there any way to write your own code that has no extra functions (namely Timer0 for <code>millis()</code> and <code>delay()</code>). I would also like to be able to disable the keyboard/mouse functions for the Leonardo.</p>
 220: <p>Although it depends on your sketch, you can somewhat decrease the size by <strong>reusing code with methods.</strong></p>

<p>Take this code:</p>

<pre><code>int led = 13;
int val;

void setup() {                
  pinMode(led, OUTPUT);     
}

void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second

  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second

  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second

  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second

  val = digitalRead(10);
}
</code></pre>

<p>1,322 bytes on Arduino Uno. Now let's shrink it a bit:</p>

<pre><code>int led = 13;
int val;

void setup() {                
  pinMode(led, OUTPUT);     
}

void loop() {
  for(uint8_t i = 0; i &lt; 8; i++) {
    blink(HIGH);
    blink(LOW);
  }    
  val = digitalRead(10);
}

void blink(uint8_t state) {
  digitalWrite(led, state);   // turn the LED to the right state
  delay(1000);                // wait for a second
}
</code></pre>

<p>1,194 bytes. That's about a 10% decrease!</p>

<p>Anyway, although it doesn't <em>greatly</em> shrink a sketch, it sometimes can be the easiest route when you are two bytes over the limit, or just want to make a more compact sketch to begin with without loosing any functionality. It's not for every situation, but I find it useful sometimes.</p>
 60: <p>You should be able to create your own board definition with a custom boards.txt file as per <a href="https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5---3rd-party-Hardware-specification" rel="nofollow">https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5---3rd-party-Hardware-specification</a>. As I see there are several usb features in the leonardo's definition. I would hope that the compiles inclusion of the 4K is based off of these flags and not the processor type.</p>

<p>Where the boards.txt would use the upload, bootloader sections from the Leonardo and the build from the uno.</p>

<p>This is all assuming the core library build does not use processor specific flags to include USB functions.</p>

<p><em>If you get such working. post back, I am sure others would be interested in such.</em></p>

<hr>

<p>I recently ran into this 4K used limitation on a library demo that actually maxed out the UNO and had to put in a</p>

<pre><code>#if !defined(__AVR_ATmega32U4__)
...
</code></pre>

<p>around a large chunk of extra features in the sketch to fit on the Leonardo.</p>

<p>I had assume (wrong) that this 4K was because I still included Serial.print which when over the USB's CDC on the Leo. But I see after a memory dump of an empty sketch they are still there.</p>

<pre><code>C:\Users\mflaga\AppData\Local\Temp\build8958339595868119500.tmp&gt;avr-objdump -d sketch_feb13a.cpp.elf &gt; sketch_feb13a.cpp.elf.lst
</code></pre>

<p>Which makes sense. As the Leonardo still requires the USB-CDC client (the 4K) as to detect the 1200 Baud connect from AVR-DUDE as to stroke the remote reboot.</p>

<hr>

<p>Hence making a custom boards.txt without USB in the build, also needs to have</p>

<pre><code>leonardo.upload.use_1200bps_touch=true
</code></pre>

<p>removed.</p>

<p>Once loaded on the target, this would require the upload to be synchronized with a manual reset of the target. As the ability to remote reboot is lost.</p>
 86: <p>I recently wanted to do exactly this. Since there is no nice way to do so, I wound up <a href="https://github.com/Robot-Will/Stino/pull/123" rel="nofollow noreferrer">writing a patch</a> for the <a href="https://github.com/Robot-Will/Stino" rel="nofollow noreferrer">Stino</a> sublime-text arduino plugin to do exactly this. It's subsequently been accepted, so it should be in any up-to-date Stino installs.</p>

<p>This adds a new option to Stino:  </p>

<p><img src="https://i.stack.imgur.com/9p7tO.png" alt="enter image description here"></p>

<p>Using this mode produces compilation results like the following:  </p>

<p>For a Uno:</p>

<blockquote>
  <p>Binary sketch size: 172 bytes (of a 32256 byte maximum, 0.53 percent).<br>
  Estimated memory use: 0 bytes (of a 1024 byte maximum, 0.00 percent).</p>
</blockquote>

<p>For a leonardo</p>

<blockquote>
  <p>Binary sketch size: 240 bytes (of a 28672 byte maximum, 0.84 percent).<br>
  Estimated memory use: 0 bytes (of a 2560 byte maximum, 0.00 percent).</p>
</blockquote>

<p><sub>Actually <em>programming</em> the leonardo with the above compiled output is <em>probably</em> a bad idea, as it <em>might</em> break the auto-reset functionality, but you <em>could</em>, if you wanted. Hat-tip to mpflaga for noting this in his answer.</sub></p>

<p>Note that the memory reports are actually incorrect, but that's a <a href="https://github.com/Robot-Will/Stino/pull/127" rel="nofollow noreferrer">separate issue</a>.</p>

<p>The code used for the above is:  </p>

<pre><code>int main()
{
    while (1)
    {

    }
}
</code></pre>

<p>Some notes:</p>

<ul>
<li>You ain't writing a "Sketch" anymore, not that you ever actually <em>do</em> write a sketch. You write <strong>programs</strong>. Period. I don't care what the Arduino wackos want to say, they don't get to redefine terms.</li>
<li>All interrupt management is manual. This means no <code>milis()</code> or similar.</li>
<li>You <em>can</em> still use the arduino serial libraries and so forth, if you want. You do have to <code>#include &lt;Arduino.h&gt;</code>.</li>
<li>You define <code>main</code>. You don't ever return from <code>main</code>. If you want setup stuff, it goes before the <code>while (1)</code>.</li>
</ul>
