Baby steps into Arduino

I have always programmed in VB (5, 6, .NET even VBA!), I’m also starting to look at C# again after a brief dalliance 5 years ago, and of course I have a passable knowledge of Python from working with the Raspberry Pi.

Ever since picking up and playing with the Pi I have become aware of these Arduino thingies. What really peaked my interest were the wearables like GEMMA and FLORA and the work of Charlotte Godley. They’re small, uncomplicated (in theory) devices to hack about on fairly easily.

Fast forward to the beginning of the month at the July CamJam and I was enticed by The PiHut and their wares, eventually crumbling and buying an Adafruit Trinket along with a NeoPixel ring (mmmm 16 LEDs!). After getting the IDE and drivers setup (more on that later) I loaded the obligatory blink sketch, which is the Arduino equivalent of ‘hello world’.

Flushed with success (or something like that) I moved on to the NeoPixel ring, wired it up and loaded the Larson Scanner sample. It needed a couple of tweaks; namely the data pin and the number of pixels, but it worked very well.

Ultimately it was time to write my first ever bit of Arduino (or C for that matter) code, cribbing from and building on the examples I had come across thus far.

The embedded video shows alternating green and purple filling up the ring, followed by yellow and red. I was working on this while helping run Pi sessions the tech zone at Gilwell 24 so the patterns arise from the Scout brand colours and the colours of my Scout scarf respectively. My code, for what it’s worth, is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <Adafruit_NeoPixel.h>
 
#define PIN 1
 
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
 
void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}
 
void loop() {
  // Scout green and purple
  spin(strip.Color(132/4, 164/4, 11/4),strip.Color(77/4, 33/4, 119/4));
  // BWV Explorers red and yellow
  spin(strip.Color(255/4,0, 0), strip.Color(255/4,255/4, 0));
}
 
void spin(uint32_t c1, uint32_t c2){
  uint32_t c;
 
  // fill in reverse, so highest pixel number first
  for(int8_t p=strip.numPixels() - 1; p>=0; p--){
    // set even pixels to colour 'c1', odd to 'c2'
    if (p%2==0){
      c = c1;  
    }
    else{
      c = c2;
    }
    cycle(0,p,c);
  }
  delay(100);
}
 
void cycle(int8_t f, int8_t l, uint32_t c){
 
  for(uint8_t i=f;i<=l;i++)
  {
      // clear last pixel, set current pixel
      strip.setPixelColor(i-1, 0);
      strip.setPixelColor(i, c);
      strip.show();
      delay(100);
  }
}

Things I Learnt

The USB is temperamental – After much faffing and cursing I discovered in the Adafruit FAQ about reducing the chip_erase_delay. This made things much happier on my laptop!

Updating AVRDUDE helped too – After working well on another laptop my home one refused to work again. A few Google searches later and the AdaFruit forum came to the rescue with AVRDUDE v1.15.13

Commands in C must end with ‘;’ – Obvious I know but the number of times I forgot!

For loops work differently – I’m used to a For … Until construct (i.e. For n = 0 to 15 or for n in range(0 to 16)). It took me ages to work out why my for (n= 15; p=0; p--) didn’t work correctly!

Debugging is hard – The Trinket has no serial console so you can’t see what the code is doing. To resolve one issue I ended up downloading Tiny C Complier (TCC) and creating test code to see what I was doing wrong. For reference a uint_8 of 0 minus 1 is 255 not -1 because it’s unsigned – doh!

All in all it’s been a very fun learning curve and I’m looking forward to trying out a few more AdaFruit examples. Oh wait, what’s that in the next article?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.