Here's a sketch of an adjustable timer which I plan to use in a publication of a DIY project. For various reasons it must run on a Trinket, the smallest, least expensive Arduino in the world. My goal is the most compact sketch possible, even at the expense of no comments, abbreviated names for variables, etc. That's why all my variables and I/O are single characters "a" to "j". Anything to minimize the number of key strokes to enter the sketch into the IDE. For example, is there any way to put several instructions on one line, perhaps separated by semicolons?
It's not pretty and it's probably not good programming. The only way I got it to work was with a hundred uploads and trial-and-error changes to correct compile errors. It works, as you can easily prove. But this is a plea for someone who knows what the heck they're doing to suggest ways to simplify it or improve it. I'm looking for minor tune-ups, not a major redesign.
To run the sketch connect a push button switch S1 from Trinket pin #0 to GND, and a pair of 470 ohm resistors from V+ to the anodes of a pair of LEDs. Connect the cathode of LED1 to Trinket pin #1 and the cathode of LED2 to Trinket pin #2.
Think of LED2 as the START/STOP indicator and LED1 as the TIMER RUNNING indicator.
A short press of S1 gives a brief blink of LED2 and turns on LED1. When the time delay expires, LED2 blinks again and LED1 turns off. If S1 is pressed while the timer is running, the time delay is terminated immediately, just as if the time delay expired. To re-calibrate the timer for a different delay, hold S1 for a long press (over 2 seconds). When you release S1, LED1 starts blinking about once per second. Count the blinks. Each blink represents a delay of 5 seconds. As soon as you see the blink for the delay you want, quickly press S1 before the blink goes out, to lock in the value. For example, if you want a time delay of 15 seconds, press S1 on the 3rd blink. If you make a mistake, just do it over. The delay value is saved in EEPROM so the system is immune to power interruption. All that for just 58 lines of code. Not bad for an 89 year old fart who doesn't know his Arduino from a hole in the ground, huh?
OLDokasional:
My goal is the most compact sketch possible, even at the expense of no comments, abbreviated names for variables, etc. That's why all my variables and I/O are single characters "a" to "j".
The length of variable names and presence of comments have ABSOLUTELY ZERO effect on the size of the final compiled code.
Anything to minimize the number of key strokes to enter the sketch into the IDE.
That's a very odd metric. Why?
For example, is there any way to put several instructions on one line, perhaps separated by semicolons?
Only a good idea if your goal is to make it unreadable. It has no effect on compiled code size.
Like the others have said, designing a program in such a way that the number of keystrokes required to enter it in the IDE is minimised, is rather odd. Much more usual is to publish the .ino file which people download directly or, in the case of a very small program, cut and paste the text of it from the web browser directly into the IDE.
The effect of this has been that the program is rather obscure. Also your use of goto statements, and that it is truncated, has not made it any easier to read. You could possibly save a few bytes of both RAM and flash memory by reviewing your choice of datatypes. For example, pins can be declared as byte instead of the larger integer. Long variable names, text spacing, comments etc. are all thrown away by the compiler. This is different to interpreted languages such as Basic which can, in small implementations, enforce a programming style similar to the one you have used here.
On the subject of the target hardware, the Trinket, Adafruit themselves are not very positive about it and carry a warning : Link . Personally, I'd prefer to use the raw ATTiny85, dispensing with the boot loader (and the pin restrictions). You'd then need a programmer (or an Arduino Uno etc. configured as a programmer).