Skip to content

Instantly share code, notes, and snippets.

@kumadasu
Created April 22, 2018 16:17
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 kumadasu/574b5946eaca3f01fd3f1e27d5726810 to your computer and use it in GitHub Desktop.
Save kumadasu/574b5946eaca3f01fd3f1e27d5726810 to your computer and use it in GitHub Desktop.
This is a code to move servos by music
/**
* I forgot this is original code or modified one.
* If you use this code, take care about the license.
* Sorry for inconvinence.
*
* This code is for SparkFun Spectrum Shield
* https://www.sparkfun.com/products/13116
*
* Sample movie
* https://www.youtube.com/watch?v=MphrcR2r3j4
*/
#include <Servo.h>
const int SERVONUM = 7;
const int servopin[] = {6,7,8,9,10,11,12};
const int spectrumStrobe = 4;
const int spectrumReset = 5;
const int spectrumAnalog = 0; //0: left channel, 1: right channel
const int dispBand = 0;
// Spectrum analyzer read values will be kept here.
int Spectrum[SERVONUM];
Servo servoAry[SERVONUM];
void setup() {
Serial.begin(9600);
//Setup pins to drive the spectrum analyzer. It needs RESET and STROBE pins.
pinMode(spectrumReset, OUTPUT);
pinMode(spectrumStrobe, OUTPUT);
for ( int i = 0; i<SERVONUM; i++ ) {
pinMode( servopin[i], OUTPUT );
}
initSpectrum();
for ( int i = 0; i<SERVONUM; i++ ) {
servoAry[i].attach(servopin[i]);
}
}
void loop() {
readSpectrum();
printBlockToSerial( Spectrum[dispBand]);
//analogWrite( ledPin, Spectrum[freq] );
for ( int i=0; i<SERVONUM; i++ ) {
servoAry[i].write(map(Spectrum[i],400,900,0,30));
}
delay(15);
}
void initSpectrum()
{
//Init spectrum analyzer
digitalWrite(spectrumStrobe,LOW);
delay(1);
digitalWrite(spectrumReset,HIGH);
delay(1);
digitalWrite(spectrumStrobe,HIGH);
delay(1);
digitalWrite(spectrumStrobe,LOW);
delay(1);
digitalWrite(spectrumReset,LOW);
delay(5);
}
// Read 7 band equalizer.
void readSpectrum()
{
byte Band;
for(Band=0;Band <7; Band++)
{
//Spectrum[Band] = (analogRead(spectrumAnalog) + analogRead(spectrumAnalog) ) >>1; //Read twice and take the average by dividing by 2
Spectrum[Band] = analogRead(spectrumAnalog>>2); // Spectrum[Band] is a global array to hold the readings
digitalWrite(spectrumStrobe,HIGH);
digitalWrite(spectrumStrobe,LOW);
}
}
void printBlockToSerial( int spectrum )
{
int i;
for (i=0; i<spectrum/10; i++)
{
Serial.print( "X" );
}
Serial.println( "" );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment