Created
January 31, 2019 06:17
-
-
Save Jerware/c8056743ff5772373f24538cd3798f7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_TiCoServo.h> | |
#include <SPI.h> | |
#include <Adafruit_VS1053.h> | |
#include <SD.h> | |
int16_t servoHeadMin = 0; | |
int16_t servoHeadMax = 90; | |
int16_t servoHeadIntervalMin = 2000; | |
int16_t servoHeadIntervalMax = 6000; | |
long servoHeadActivate = 0; | |
int16_t servoArmMin = 0; | |
int16_t servoArmMax = 45; | |
int16_t servoArmIntervalMin = 2000; | |
int16_t servoArmIntervalMax = 8000; | |
long servoArmActivate = 0; | |
int16_t lastSound = 6; | |
int16_t soundIndex = 0; | |
int16_t soundIntervalMin = 2000; | |
int16_t soundIntervalMax = 5000; | |
long soundActivate = 0; | |
bool queueSound = false; | |
bool randomSounds = false; | |
Adafruit_TiCoServo servoHead; // create servo object to control a servo | |
Adafruit_TiCoServo servoArm; // create servo object to control a servo | |
bool ledState = true; | |
const int ledPin = 13; | |
// These are the pins used for the music maker shield | |
#define SHIELD_RESET -1 // VS1053 reset pin (unused!) | |
#define SHIELD_CS 7 // VS1053 chip select pin (output) | |
#define SHIELD_DCS 6 // VS1053 Data/command select pin (output) | |
#define CARDCS 4 // Card chip select pin | |
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt | |
#define DREQ 3 // VS1053 Data request, ideally an Interrupt pin | |
Adafruit_VS1053_FilePlayer musicPlayer = | |
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS); | |
void setup() { | |
Serial.begin(57600); | |
delay(1000); | |
pinMode(ledPin, OUTPUT); | |
randomSeed(analogRead(0)); | |
servoHead.attach(9); // attaches the servo to the servo object | |
servoArm.attach(10); // attaches the servo to the servo object | |
// init servos to middle | |
servoHead.write(servoHeadMax/2); | |
servoArm.write(servoArmMax/2); | |
// flash that LED to indicate start | |
for (int x=0; x<10; x++) | |
{ | |
blinkLed(); | |
} | |
delay(2000); | |
if (! musicPlayer.begin()) { // initialise the music player | |
Serial.println(F("Couldn't find VS1053, do you have the right pins defined?")); | |
} | |
else Serial.println(F("VS1053 found")); | |
if (!SD.begin(CARDCS)) { | |
Serial.println(F("SD failed, or not present")); | |
} | |
else Serial.println("SD OK!"); | |
// Set volume for left, right channels. lower numbers == louder volume! | |
musicPlayer.setVolume(20,20); | |
if (!musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT)) | |
Serial.println(F("DREQ pin is not an interrupt pin")); | |
// Start up animation?... | |
// Here we go! | |
Serial.println("I am C-3PO, human-cyborg relations."); | |
} | |
void loop() { | |
// move head | |
if (millis() > servoHeadActivate) | |
{ | |
int16_t servoTarget = random(servoHeadMin, servoHeadMax); | |
servoHead.write(servoTarget); | |
servoHeadActivate = millis() + random(servoHeadIntervalMin, servoHeadIntervalMax); | |
Serial.print("Head: "); | |
Serial.println(servoTarget); | |
} | |
// move arm | |
if (millis() > servoArmActivate) | |
{ | |
int16_t servoTarget = random(servoArmMin, servoArmMax); | |
servoArm.write(servoTarget); | |
servoArmActivate = millis() + random(servoArmIntervalMin, servoArmIntervalMax); | |
Serial.print("Arm: "); | |
Serial.println(servoTarget); | |
} | |
// queue sound after last one finishes | |
if (!musicPlayer.playingMusic && queueSound) | |
{ | |
long randomInterval = random(soundIntervalMin, soundIntervalMax); | |
soundActivate = millis() + randomInterval; | |
queueSound = false; | |
Serial.print("Sound finished. Queue next track in "); | |
Serial.print(randomInterval); | |
Serial.println(" ms"); | |
} | |
// play sound | |
if (millis() > soundActivate && !musicPlayer.playingMusic && !queueSound) | |
{ | |
queueSound = true; | |
char filename[13]; | |
char fileNumber[3]; | |
itoa(soundIndex, fileNumber, 10); | |
strcpy (filename, "c3po_"); | |
strcat (filename, fileNumber); | |
strcat(filename, ".mp3"); | |
musicPlayer.startPlayingFile(filename); | |
Serial.print("Sound: "); | |
Serial.println(filename); | |
if (randomSounds) | |
{ | |
soundIndex = random(0, lastSound+1); | |
} | |
else | |
{ | |
soundIndex++; | |
if (soundIndex > lastSound) soundIndex = 0; | |
} | |
} | |
} | |
void blinkLed() { | |
if (ledState) digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level) | |
else digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW | |
ledState = !ledState; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment