///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /*Made by Ashley Calkins 4/8/2018 is designed to rotate a motor for use in a music box,and and flash LED's at in time with the music box, it is controlled by buttons, which do various things, such as control the speed, start the music box, stop the music box, or have the music box repeat */ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /*Pins are defined*/ #define pin1 9 // These pins control the motor #define pin2 8 #define pin3 7 #define pin4 6 #define pin5 11 // These pins control the LEDS #define pin6 12 #define pin7 13 #define pin8 2 //These pins control the buttons #define pin9 3 #define pin10 4 #define pin11 5 /*arrays are created that store the pin numbers, based on type, so that we can use for loops efficiently */ int motorpins[]={pin1,pin2,pin3,pin4}; int LEDpins[]={pin5,pin6,pin7}; int buttonpins[]={pin8,pin9,pin10,pin11}; /*This sets up all our variables and more arrays*/ int delaytime= 30; //determines the speed that our motor runs at, and speed of the entire program, should not be less than 8, optimal speed (128 bpm) is about 20 int i,j,k,l; //varables for our loops //byte count=1; //*testing used with calibration and testing, but is not necessary during actual runtime byte pause=0; // pauses the program when i is 1 byte fwds=0; // when 0, it is stopped,1, it runs forwards and 2, it runs backwards byte loops=0; // determines if the motor will continue to run after 1 rotation byte presscount[3]; //determines how many times a button has been pressed, when needed byte buttonStates[4]; // used to determine if a button has been pressed /*This sets up our functions*/ void fwdloop(int pinset[],int len,int count); void bwdloop(int pinset[],int len,int count); void buttons(); void on(); void setup() { // Serial.begin(9600); //*testing for(i=0;i<4;i++) //defines motor pins as out put pinMode(motorpins[i],OUTPUT); for(i=0;i<3;i++) // defines LED pins as output pinMode(LEDpins[i],OUTPUT); for(i=0;i<4;i++) digitalWrite(buttonpins[i],LOW); for(i=0;i<4;i++) pinMode(buttonpins[i],INPUT); //defines button pins as input for(i=0;i<4;i++) digitalWrite(buttonpins[i],HIGH); //sets button pins as high } void loop() { buttons(); // wont start unless input has been received if(fwds!=0){ //this for loop runs for 1 rotation, which is 21 1/3 times through the on function, because there should be 48 beats, and 2048 steps in a rotation for(l=0;l<21;l++){ for(k=0;k<3;k++){ on(); if(pause==1) //stops the loop, if pause is 1 break; } if(pause==1) break; } if(l==21){ k=0; on(); if (fwds==1){ //turns off the last LED, without turning another on digitalWrite(LEDpins[2],LOW); digitalWrite(motorpins[3],LOW); } else if(fwds==2){ digitalWrite(LEDpins[0],LOW); digitalWrite(motorpins[0],LOW); } } } if(loops==0){ //determines if another rotation should occur based on "loops" fwds=0; } else if(loops<3){ // if its 0, it wont rotate again, if its 1, or 2, it will rotate that many times again,and if its 3, it will rotate until its stopped loops--; } }