Skip to content

Instantly share code, notes, and snippets.

@ohc209
Last active February 20, 2017 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohc209/ea1c75fb150d69af24d7c60f42bdc42c to your computer and use it in GitHub Desktop.
Save ohc209/ea1c75fb150d69af24d7c60f42bdc42c to your computer and use it in GitHub Desktop.
/* Ella Dagan, Olivia Cueva, Vinyata Pany
Feb 20, 2017
Music Player, Tangible Interaction Workshop
To control p5.js music player sketch created by Tom Igoe
https://github.com/tigoe/p5js_examples/tree/master/musicPlayer
Sensors
- Toggle Switch
- Momentary Push Button
- Joystick with push button
Code References:
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
//Constant Variables wont change
const int stopPlayPin = 7; // setting stop/play button as variable that will not change
const int playPausePin = 4; // setting pause/play button as variable that will not change
const int joyStickXAxis = A0; // xAxis (Horizontal Axis) pin
const int shufflePin = 8; // shufflePin on joystick
// These variables will change
int readingJoyStick; // reading the information coming in from the joystick pin
// To set a minimum and maximum limit for the xAxis of the joystick
int threshold = 350 / 10; //set at this value because the resting value
// of the joystick at 454. Dividing by 10 to get a more precise reading.
int thresholdBack = 70;
//To detect if you pressing once or holding down right xAxis of joystick
int interval = 700; // 700 milli seconds of an interaval
boolean shortMsgSent = false; // begins with doing nothing
boolean longMsgSent = false; // begins with doing nothing
boolean isShortPress = false; // begins with doing nothing
// stopPlayButtonSwitch variable will change states:
int stopPlayButtonSwitchCounter = 0; // counter for the number of button presses
int stopPlayButtonState = 0; // current state of the button
int lastStopPlayButtonState = 0; // previous state of the button
//playPauseButtonSwitch variable will change states:
int playPauseButtonSwitchCounter = 0;
int playPauseButtonState = 0;
int lastPlayPauseButtonState = 0;
//shuffleButtonSwitch variable will change states:
int shuffleButtonSwitchCounter = 0;
int shuffleButtonState = 0;
int lastShuffleButtonState = 0;
// these long integers read the millis and will detect change in time going forward
unsigned long prevMillis = 0;
unsigned long currMillis = 0;
// these long integers read the millis and will detect change in time for going Back
unsigned long prevMillisBack = 0;
unsigned long currMillisBack = 0;
void setup() {
pinMode(stopPlayPin, INPUT_PULLUP);
pinMode(playPausePin, INPUT);
pinMode(shufflePin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
///////// Stop and Play button code /////////
stopPlayButtonState = digitalRead(stopPlayPin);
//compare the stopPlayButtonState to it's previous state
if (stopPlayButtonState != lastStopPlayButtonState) { // if current button state is different than the last state
Serial.write(49); // write to p5
lastStopPlayButtonState = stopPlayButtonState; // now the last button state becomes the current state
}
///////// Joystick code /////////
readingJoyStick = (analogRead(joyStickXAxis)) / 10; // these reads what's coming in from A0
// Joystick skip backward reading
if (readingJoyStick > thresholdBack) { //if data coming in from A0 is more than the threshold(70)
currMillisBack = millis(); // and the current millis becomes the time since
// the arduino program started running.
if (currMillisBack - prevMillisBack > interval) { // long press test
prevMillisBack = currMillisBack;
Serial.write(52);
}
}
// Joystick skip forward and speed up song reading
if (readingJoyStick < threshold) { //if data coming in from A0 is less than the threshold(350)
// we know the joystick shifted to the right
currMillis = millis(); // and the current millis becomes the time since
// the arduino program started running.
if (currMillis - prevMillis > interval) { // long press test
prevMillis = currMillis;
isShortPress = false;
shortMsgSent = true;
// print LongPress msg
if (!longMsgSent) {
//Serial.println(2); // long
Serial.write(53);
longMsgSent = true;
}
} else {
if (!longMsgSent) {
isShortPress = true;
shortMsgSent = false;
}
}
} else {
prevMillis = millis();
currMillis = prevMillis;
if (isShortPress && !shortMsgSent) {
// print ShortPress msg
Serial.write(51);
shortMsgSent = true;
}
longMsgSent = false;
}
///////// Play and Pause button code //////////
playPauseButtonState = digitalRead(playPausePin);
if (playPauseButtonState != lastPlayPauseButtonState) { // if current button state is different than the last state
if (playPauseButtonState == HIGH) {
//Serial.println("change detected");
Serial.write(50); // write to p5
}
}
lastPlayPauseButtonState = playPauseButtonState; // now the last button state becomes the current state
////// Joystick BUTTON code (SHUFFLE button) //////
shuffleButtonState = digitalRead(shufflePin);
//compare the stopPlayButtonState to it's previous state
if (shuffleButtonState != lastShuffleButtonState) { // if current button state is different than the last state
Serial.write(54); // write to p5
lastShuffleButtonState = shuffleButtonState; // now the last button state becomes the current state
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment