Where do all my bytes go? ... an analysis of program size in an S4A 4.0 series application

You heard it here first, but coming this year, we are finally releasing the version 2 compiler, as part of Swift for Arduino release 4.0.

This is a very exciting new development, with our own patched swift frontend and (most importantly) a custom built standard library, fine tuned for the atmega328p and the arduino uno.  There is most definitely nothing else like this anywhere in the world. Another first!

As part of this launch, I'm going to try to do some more blog posts about day to day findings and experiences with making AVR/Arduino programs using our new compiler.

As an example, I just compiled this program...

import AVR

SetupSerial() print("newer API") public let dummyArray = [1,5,9,112,0,1,4] print(dummyArray.count) while true {}
This is a super simple program demonstrating some of the powerful new features of version 4.0, namely our custom made strings and our customised arrays.

Both are super efficient on a microcontroller and we have removed some of the featur…

Sipping power... sleep modes and Swift for Arduino

One feature I have been meaning to add to S4A for a while now is sleep.

It might not sound like an important feature but it could radically change the way projects are made.

A critical distinguishing feature of S4A is that it creates true native programs for the atmega328p microcontroller, meaning you don't need to have an Arduino UNO, plugged into your laptop or desktop via USB for these programs (although of course you can).

This sets it aside from many other attempts to simplify Arduino programming in the past, that effectively run programs on a full PC/Mac then send serial commands to the board to activate it.

The next step... how do you make a project that lives on batteries?

Well, right out of the box you can do that, at least for a while. See my Hallowe'en project from last year on this blog that I made for my daughter. But that lasted a couple of days on batteries before it faded. Anything with lots of lights and motors is going to need a lot of battery, of course. But…

Swift for Arduino newsletter - Saturday November 16th 2019

Hello to the Swift for Arduino community!

We are so excited to get in touch with you and let you know what's going on.
First and foremost... Black Friday/Cyber Monday!
We are giving away huge discounts!

*** The Swift for Arduino Starter Box... HALF PRICE!! *** (Contains a Seeed Studios Lotus board and connectors, you don't need any of your own hardware, get started right away!)
Note: to use this discount, you must enter the discount code BFCM50 in the Shopify store during checkout. This discount is limited in time and number - don't lose out!
Offer available from 00:00 on Friday 29th November, closes 23.59 on Monday 2nd December 2019
(For convenience, you can use this link:

*** The Swift for Arduino IDE - license only... 70% OFF!! *** (suitable for customers who already have an Arduino UNO or similar board) This discount is automatic for customers purchasing the IDE only produc…

Grove PIR sensor

As you can see in this video, I've hooked up a simple program to change the colour of a strip of neopixels in response to detecting people passing by.

See example code here:

But this is an easy project to write.

Grove Temperature and Humidity sensor

Grove are a fun concept, lots of cheap, little sensors, buttons, etc. on little PCBs with a universal 4 wire connector system (e.g. gnd, vcc, rx, tx or gnd, vcc, sda, scl). Making projects becomes more like lego but it's a bit less heavyweight than the "shields" or "hats" ideas, and a lot cheaper!

I got a mixed bag recently, about £12 worth, plus a "seeduino", which is Seeed Studios' *duino clone. The price quite recently dropped from something like $19 to something like $6.90! To be honest, most retailers, like the one I went to, are still selling nearer the old price, but I guess you can't blame them if that's what they paid for the stock. I think I forked out £18 in the end, plus the 12 for bits, making about 30... plus postage, etc.

I got:

An IR distance interrupter - single bit detects if a person (or thing) gets close, obviously great for robotics, although many other applications

PIR (passive IR) motion detector - security lights, e…

Button press daemon

As you've seen from my other posts, I have a couple of lamps at home, controlled by homekit. I also have a daemon on the pi that does (slightly inaccurate) PWM to control LEDs for showing the current weather forecast (look around my various blogs or ask here for links if you want to see how each of these works).

I've added a couple of useful functions to the daemon and recently upgraded it to be a bit more of a packaged service (see the github page). It's also in it's own repo now.

The last feature I added was I wanted some way to capture inputs not just output voltages.

There are already good solutions on the pi, many of them written in python, that will read input pin values, and about 7 years ago, someone patched the kernel to allow interrupt drive, an addition to sysfs to indicate you want to write code that blocks on a device with a poll() or select(). Probably the best solution for most people will be to use this mechanism within their own C program or (even bett…

Neopixel rainbow

Thanks to great work by Mark Swanson, neopixel support is now available for S4A!
We needed a small upgrade to the S4A component library, AVR to give some timing functions, otherwise the work is all done in swift. You can see some great demo videos.

Spurred on by this I decided to add some optional extra low level support for many basic classes of neopixels, to give developers two options how to get started.

Here is an example of making a rainbow using the new "Fast iLED" components.

And all this in just a few lines of simple Swift code!

import AVR
typealias IntegerLiteralType = UInt8let ledPin = 4
let pixels = 60var buffer = iLED_Setup_Buffered(pin: ledPin, count: pixels, hasWhiteChip: true)func rainbow() {
    let hueDeltaPerPixel = 255 / pixels
    for i in 0..<pixels {
        buffer[Int(i)] = iLEDFastMakeColor(hue: i &* hueDeltaPerPixel, saturation: 255, value: 255, white: 0)