Plug in GEMMA over USB and load the following code into your Adafruit Arduino IDE. If you haven't before, check out our Introducing GEMMA guide to get started with the software.

// SPDX-FileCopyrightText: 2018 Mikey Sklar for Adafruit Industries
//
// SPDX-License-Identifier: MIT

int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
int counter = 0;       // counter to keep track of cycles

// the setup routine runs once when you press reset:
void setup()  { 
  // declare pins to be an outputs:
  pinMode(0, OUTPUT);
  pinMode(2, OUTPUT);
} 

// the loop routine runs over and over again forever:
void loop()  { 
  // set the brightness of the analog-connected LEDs:
  analogWrite(0, brightness);
  
  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount; 
    counter++;
  }     
  // wait for 15 milliseconds to see the dimming effect    
  delay(15); 

// turns on the other LEDs every four times through the fade by 
// checking the modulo of the counter.
// the modulo function gives you the remainder of 
// the division of two numbers:
  if (counter % 4 == 0) {
    digitalWrite(2, HIGH);
  } else {
   digitalWrite(2, LOW);
  }  
}

This sketch is a mash-up of two very basic Arduino examples: blink and fade. You can modify it to display the patterns of light you like best or code up your own sketch starting with the examples provided from inside Arduino.

Once your program is doing what you like, unplug the USB cable and plug in a battery pack like our 2x2032 holder with on/off switch.

Take the batteries out if you get stuck in a rainstorm and for washing. Enjoy your new light-up hat!

This guide was first published on Apr 02, 2014. It was last updated on Mar 19, 2024.

This page (Arduino Code) was last updated on Mar 18, 2024.

Text editor powered by tinymce.