Wake-on-Shake Hookup Guide

Pages
Contributors: Toni_K, SFUptownMaker
Favorited Favorite 7

Code

Now that you have everything hooked up, it's time to program your Arduino Pro Mini and get your project running!

Upload the Code

Download the zip of the demo sketch from the link below, or find the most up-to-date code in the GitHub repository.

Upload this sketch to your Arduino. If you are unsure how to do this, please review our tutorial on using the 3.3V Pro Mini here.

Code Functionality

In the first section of the code, we must define the pin numbers on the Arduino that are connected to either the LEDs or the Wake-on-Shake.

language:c
//Define LED Pin Connections
int LED1 = 3;
int LED2 = 5;
int LED3 = 6;
int LED4 = 9;

//Define Wake-on-Shake WAKE pin connection
int WAKE = 10;

The setup loop of the sketch is where the main action occurs. Remember, we will only be running a few basic functions when the system turns on.

We first define the connected pins as outputs, as the Arduino will be driving all of these pins. We then write the WAKE pin HIGH, to prevent the Wake-on-Shake from returning to the 'sleep' state. This allows us to run through the desired functionality on the Arduino, before returning to the low-power state.

We then slowly brighten each LED using the analogWrite function. Once the LED has reached full brightness, the Pro Mini turns it off and turns the next LED on.

Once all the LEDs have been cycled through, we pull the WAKE pin LOW, allowing the Wake-on-Shake to enter low-power mode and put the system to sleep.

language:c
void setup() {

  //Set LED pin connections as outputs
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(WAKE, OUTPUT);

  //Set WAKE pin HIGH to prevent the Wake-on-Shake from 'sleeping'
  digitalWrite(WAKE, HIGH);

  //Functions to occur on each wake-up of the system
  //This will fade the LEDs up to full brightness one by one

  //Slowly birghten the first LED
  for( int i = 0; i<255; i++)
  {
    analogWrite(LED1, i);
    delay(20);
  }
  //Turn off the LED
  digitalWrite(LED1, LOW);

  for( int i = 0; i<255; i++)
  {
    analogWrite(LED2, i);
    delay(20);
  }
  digitalWrite(LED2, LOW);

  for( int i = 0; i<255; i++)
  {
    analogWrite(LED3, i);
    delay(20);
  }
  digitalWrite(LED3, LOW);

  for( int i = 0; i<255; i++)
  {
    analogWrite(LED4, i);
    delay(20);
  }
  digitalWrite(LED4, LOW);

  //Allow the Wake-on-Shake to go to sleep
  digitalWrite(WAKE, LOW);
}

In the final section of the code, we have the main loop. Again, there are no functions occurring in here, as the Arduino will not be remaining on for any extended periods of time.

language:c
    /*********************Main Loop************************/
void loop() {
//No main function here as the Wake-on-Shake will return to low-power mode
}