Thursday, February 11, 2010

IPOD+ARDUINO SIMPLE REMOTE

/*******************************************************************************
* Copyright (c) 2009 David Findlay
* All rights reserved.
* Modified by Dustin Evans 2010-02-11 evansdustin08@gmail.com www.MyArduinoProjects.BlogSpot.com
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
#include <8X8Pattern.h> // 8X8 LED MATRIX LIBRARY (NOT USED IN THIS EXAMPLE)
#include // USED FOR SIMPLE REMOTE COMMANDS
#include // USED TO COMMUNICATE TO THE IPOD
const int PLAY_BUTTON = 5; // PLAY/PAUSE ON PIN 5
const int NEXT_BUTTON = 6; // NEXT TRACK ON PIN 6
const int PREVIOUS_BUTTON = 7; // PREVIOUS TRACK ON PIN 7
const int VOLUMEUP_BUTTON = 8; // VOLUME INCREASE ON PIN 8
const int VOLUMEDOWN_BUTTON = 9; // VOLUME DECREASE ON PIN 9
const int RANDOMON_BUTTON = 10; // TURN RANDOM ON/0FF ON PIN 10
const int IPODON_BUTTON = 11; // WAKE UP IPOD ON PIN 11
const int IPODOFF_BUTTON = 12; // MAKE IPOD SLEEP PIN 12
const long DEBOUNCE_MILLIS = 150; // DEBOUNCE USED TO CANCEL OUT ANY ACCIDENTAL BUTTON PRESSES
int lastButtonState = LOW; // STATE OF THE BUTTONS
long lastToggleTime = 0; // TOGGLE TIME USED TO CALCULATE BETWEEN NOW AND DEBOUNCE
SimpleRemote simpleRemote;
void setup()
{
pinMode(PLAY_BUTTON, INPUT); // SET ALL THE BUTTONS AS INPUTS
pinMode(NEXT_BUTTON, INPUT);
pinMode(PREVIOUS_BUTTON, INPUT);
pinMode(VOLUMEUP_BUTTON, INPUT);
pinMode(VOLUMEDOWN_BUTTON, INPUT);
pinMode(RANDOMON_BUTTON, INPUT);
pinMode(IPODON_BUTTON, INPUT);
pinMode(IPODOFF_BUTTON, INPUT);
// turn on the pull-up resistor for example using Arduino resistor. in actual program won't need this
digitalWrite(PLAY_BUTTON, HIGH);
digitalWrite(NEXT_BUTTON, HIGH);
digitalWrite(PREVIOUS_BUTTON, HIGH);
digitalWrite(VOLUMEUP_BUTTON, HIGH);
digitalWrite(VOLUMEDOWN_BUTTON, HIGH);
digitalWrite(RANDOMON_BUTTON, HIGH);
digitalWrite(IPODON_BUTTON, HIGH);
digitalWrite(IPODOFF_BUTTON, HIGH);
simpleRemote.setup();
}
void loop()
{
simpleRemote.loop();
const int PLAY_BUTTON_STATE = digitalRead(PLAY_BUTTON);
const int NEXT_BUTTON_STATE = digitalRead(NEXT_BUTTON);
const int PREVIOUS_BUTTON_STATE = digitalRead(PREVIOUS_BUTTON);
const int VOLUMEUP_BUTTON_STATE = digitalRead(VOLUMEUP_BUTTON);
const int VOLUMEDOWN_BUTTON_STATE = digitalRead(VOLUMEDOWN_BUTTON);
const int RANDOMON_BUTTON_STATE = digitalRead(RANDOMON_BUTTON);
const int IPODON_BUTTON_STATE = digitalRead(IPODON_BUTTON);
const int IPODOFF_BUTTON_STATE = digitalRead(IPODOFF_BUTTON);
const long now = millis();
if ((PLAY_BUTTON_STATE != lastButtonState) && ((now - lastToggleTime) > DEBOUNCE_MILLIS))
{
if (PLAY_BUTTON_STATE == LOW)
{
simpleRemote.sendPlay();
}
else
{
simpleRemote.sendButtonReleased();
}
lastButtonState = PLAY_BUTTON_STATE;
lastToggleTime = now;
}
else if ((NEXT_BUTTON_STATE != lastButtonState) && ((now - lastToggleTime) > DEBOUNCE_MILLIS))
{
if (NEXT_BUTTON_STATE == LOW)
{
simpleRemote.sendSkipForward();
}
else
{
simpleRemote.sendButtonReleased();
}
lastButtonState = NEXT_BUTTON_STATE;
lastToggleTime = now;
}
else if ((PREVIOUS_BUTTON_STATE != lastButtonState) && ((now - lastToggleTime) > DEBOUNCE_MILLIS))
{
if (PREVIOUS_BUTTON_STATE == LOW)
{
simpleRemote.sendSkipBackward();
}
else
{
simpleRemote.sendButtonReleased();
}
lastButtonState = PREVIOUS_BUTTON_STATE;
lastToggleTime = now;
}
else if ((VOLUMEUP_BUTTON_STATE != lastButtonState) && ((now - lastToggleTime) > DEBOUNCE_MILLIS))
{
if (VOLUMEUP_BUTTON_STATE == LOW)
{
simpleRemote.sendVolPlus();
}
else
{
simpleRemote.sendButtonReleased();
}
lastButtonState = VOLUMEUP_BUTTON_STATE;
lastToggleTime = now;
}
else if ((VOLUMEDOWN_BUTTON_STATE != lastButtonState) && ((now - lastToggleTime) > DEBOUNCE_MILLIS))
{
if (VOLUMEDOWN_BUTTON_STATE == LOW)
{
simpleRemote.sendVolMinus();
}
else
{
simpleRemote.sendButtonReleased();
}
lastButtonState = VOLUMEDOWN_BUTTON_STATE;
lastToggleTime = now;
}
else if ((RANDOMON_BUTTON_STATE != lastButtonState) && ((now - lastToggleTime) > DEBOUNCE_MILLIS))
{
if (RANDOMON_BUTTON_STATE == LOW)
{
simpleRemote.sendToggleShuffle();
}
lastButtonState = RANDOMON_BUTTON_STATE;
lastToggleTime = now;
}
else if ((IPODON_BUTTON_STATE != lastButtonState) && ((now - lastToggleTime) > DEBOUNCE_MILLIS))
{
if (IPODON_BUTTON_STATE == LOW)
{
simpleRemote.sendiPodOn();
}
lastButtonState = IPODON_BUTTON_STATE;
lastToggleTime = now;
}
else if ((IPODOFF_BUTTON_STATE != lastButtonState) && ((now - lastToggleTime) > DEBOUNCE_MILLIS))
{
if (IPODOFF_BUTTON_STATE == LOW)
{
simpleRemote.sendiPodOff();
}
lastButtonState = IPODOFF_BUTTON_STATE;
lastToggleTime = now;
}
}

No comments:

Post a Comment