ElectroPeak
Published © GPL3+

DIY Smart Necklace Snowing by Shake with Arduino

Our idea is to make a necklace using an Arduino Pro Mini and OLED display to show an animation on it.

BeginnerFull instructions provided2 hours6,319

Things used in this project

Hardware components

Arduino Pro Mini
×1
ElectroPeak Mercury Tilt Sensor Switch
×1
Battery 80mAh 3.7 V lipo Polymer
×1
OLED 0.96 inch 128*64 SPI Display
×1

Software apps and online services

Arduino IDE

Story

Read more

Code

Code

Arduino
#include "SPI.h"
#include "Wire.h"
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI   2
#define OLED_CLK   3
#define OLED_DC    4
#define OLED_CS    5
#define OLED_RESET 6
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

/* Comment out above, uncomment this block to use hardware SPI
#define OLED_DC     6
#define OLED_CS     7
#define OLED_RESET  8
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  &SPI, OLED_DC, OLED_RESET, OLED_CS);
*/

#define NUMFLAKES     15 // Number of snowflakes in the animation example

#define LOGO_HEIGHT   4
#define LOGO_WIDTH    4

static const unsigned char PROGMEM logo_tree[] =
{
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xe0, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0xe0, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf3, 0x30, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc3, 0x30, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x07, 0x83, 0x38, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x0f, 0x03, 0x18, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x1c, 0x03, 0x1c, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfe, 0x78, 0x03, 0x0c, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xb7, 0xf0, 0x03, 0x0e, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x33, 0xe0, 0x03, 0x0e, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x33, 0x80, 0x03, 0x06, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x3c, 0x33, 0x80, 0x07, 0x07, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf0, 0xf0, 0x31, 0x80, 0x07, 0x07, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xf1, 0xe0, 0x31, 0xc1, 0xc7, 0x03, 0x00, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xf3, 0xc0, 0x31, 0xc1, 0xc6, 0x03, 0x00, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d, 0xff, 0x00, 0x30, 0xc0, 0x86, 0x03, 0x80, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79, 0xde, 0x0c, 0x70, 0xc0, 0x06, 0x03, 0x80, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf1, 0x9c, 0x1c, 0x70, 0xe0, 0x06, 0x01, 0x80, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xe1, 0x9c, 0x0c, 0x60, 0xe0, 0x0e, 0x01, 0x80, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x81, 0x9c, 0x00, 0x60, 0x60, 0x0e, 0x01, 0x80, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x01, 0x8c, 0x00, 0x60, 0x60, 0x0c, 0x21, 0x80, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x03, 0x8c, 0x00, 0xe0, 0x60, 0x0c, 0x71, 0xc0, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x08, 0x00, 0x3c, 0x03, 0x0c, 0x00, 0xc0, 0x60, 0x1c, 0x71, 0xff, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x0c, 0x00, 0xf0, 0xc3, 0x0c, 0x00, 0xc0, 0x70, 0x1c, 0x01, 0xff, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x0e, 0x31, 0xe1, 0xc7, 0x0c, 0x01, 0xc0, 0x70, 0x18, 0x01, 0xff, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x0f, 0xe3, 0xc0, 0xc6, 0x0e, 0x01, 0x80, 0x70, 0x18, 0x00, 0xc0, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x1f, 0xe7, 0x00, 0x0e, 0x0e, 0x03, 0x80, 0x70, 0x38, 0x00, 0xc0, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0xff, 0xce, 0x00, 0x0c, 0x0e, 0x03, 0x80, 0x70, 0x30, 0x00, 0xc0, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0xff, 0xce, 0x00, 0x1c, 0x0e, 0x03, 0x0c, 0x70, 0x30, 0x00, 0xc0, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x1f, 0xe7, 0x00, 0x38, 0x0e, 0x07, 0x0e, 0x70, 0x70, 0x00, 0xff, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x0f, 0xe3, 0xc0, 0x70, 0x0e, 0x06, 0x0c, 0x70, 0x60, 0x00, 0xff, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x0e, 0x71, 0xe0, 0xf0, 0x0e, 0x0e, 0x00, 0x70, 0xe0, 0x01, 0xff, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x0c, 0x00, 0xf1, 0xe0, 0x0c, 0x1c, 0x00, 0x70, 0xe0, 0x01, 0xc0, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x08, 0x00, 0x7b, 0xc3, 0x8c, 0x18, 0x00, 0x61, 0xc0, 0x01, 0xc0, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x03, 0x8c, 0x38, 0x00, 0x61, 0xc2, 0x01, 0xc0, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x03, 0x8c, 0x70, 0x00, 0x61, 0x87, 0x01, 0x80, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x1c, 0xe0, 0x00, 0x63, 0x87, 0x01, 0x80, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x19, 0xc0, 0x00, 0xe7, 0x02, 0x01, 0x80, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x1f, 0xc0, 0x00, 0xe7, 0x00, 0x01, 0x80, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x1f, 0x81, 0xc0, 0xce, 0x00, 0x03, 0x80, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x3f, 0x01, 0xc0, 0xcc, 0x00, 0x03, 0x80, 0xc0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x33, 0xc0, 0x81, 0xdc, 0x00, 0x03, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xb1, 0xe0, 0x01, 0xb8, 0x00, 0x03, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf0, 0xf0, 0x01, 0xf0, 0x00, 0x07, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x78, 0x03, 0xf0, 0x00, 0x07, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x03, 0xe0, 0x00, 0x06, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x03, 0xe0, 0xe0, 0x0e, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x87, 0xf0, 0xe0, 0x0e, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xe6, 0x78, 0x40, 0x0c, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x3c, 0x00, 0x1c, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x0f, 0x00, 0x18, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x07, 0x80, 0x38, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x38, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x70, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x70, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0xe0, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xc0, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

static const unsigned char PROGMEM logo_bmp[] =
{     0x60, 
    0xf0, 
    0xf0, 
    0x60
};

bool merc;

void setup() {
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }


  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  merc=digitalRead(7);
  testdrawbitmap();    // Draw a small bitmap image
  testanimate(logo_bmp, LOGO_WIDTH, LOGO_HEIGHT); // Animate bitmaps
}

void loop() {
}


void testdrawbitmap(void) {
  display.clearDisplay();
 if (merc==true){
  display.drawBitmap(0,0,logo_tree, 128, 64, 1);
  display.display();
  delay(1000);}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define XPOS   0 // Indexes into the 'icons' array in function below
#define YPOS   1
#define DELTAY 2

void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  int8_t f, icons[NUMFLAKES][3];

  // Initialize 'snowflake' positions
  for(f=0; f< NUMFLAKES; f++) {
    icons[f][XPOS]   = random(1 - LOGO_WIDTH, display.width());
    icons[f][YPOS]   = -LOGO_HEIGHT;
    icons[f][DELTAY] = random(1, 6);
    Serial.print(F("x: "));
    Serial.print(icons[f][XPOS], DEC);
    Serial.print(F(" y: "));
    Serial.print(icons[f][YPOS], DEC);
    Serial.print(F(" dy: "));
    Serial.println(icons[f][DELTAY], DEC);
  }

  for(int i=0;i<10;i++) {
    display.clearDisplay(); // Clear the display buffer
  display.drawBitmap(0,0,logo_tree, 128, 64, 1);
  display.display();
    // Draw each snowflake:
    for(f=0; f< NUMFLAKES; f++) {
      display.drawBitmap( icons[f][YPOS],icons[f][XPOS], bitmap, 4, 4, WHITE);
    }

    display.display(); // Show the display buffer on the screen
   delay(50);        // Pause for 1/10 second

    // Then update coordinates of each flake...
    for(f=0; f< NUMFLAKES; f++) { icons[f][YPOS] += icons[f][DELTAY]; // If snowflake is off the bottom of the screen... if (icons[f][XPOS] >= display.height()) {
        // Reinitialize to a random position, just off the top
        icons[f][XPOS]   = random(1 - LOGO_WIDTH, display.width());
        icons[f][YPOS]   = -LOGO_HEIGHT;
        icons[f][DELTAY] = random(1, 6);
      }
    }
  }
}

Credits

ElectroPeak

ElectroPeak

57 projects • 731 followers
At ElectroPeak we want to teach you to enjoy electronics more. We offer Top-notch guides and worry-free shopping experience.

Comments