Final Project: Sombrero pics and update

Final Project SOMBRERO: Is up and running and putting the hat together was the way I kind of planned it would look like; however it was not easy as looks
Function: A button would be pressed and 3 other switches and gears would be activate which is the buzzer piezo playing the French Can Can, the continuous servo which is rotating a small car with a magnet that will hit 3 magnetic reed switches. These would light up the 7 LED. Then flipping the switch would shut the whole thing off all together.
Where it would be used: Parties, school on crazy hat day, or it could be part of a Halloween costume.

Here we got me hooking up the 7 LED lights in Parallel.

I was following this map of the Resistors to the 7 LEDs this needed  Three resistors of 56 ohms and One resistor of 180 Ohms.

 Getting this hooked up to the hat is no easy method as it involved for me tape and sewing that tap onto the hat. The reason I had to put this wire configuration on the baseball hat was to allow the user to wear the hat without the feeling of wires. As the sombrero is a bit high enough to allow them in

This hat needed a stable train track to hold the lego car with the magnet in the air. The magnet reeds are sticking out on the sides as they are attracted to the magnet on the Lego car. The buzzer will be playing the French can can which all functions of the rotating servo to the magnetic reeds and the Song the French Can Can are activated by a button that the user will hold.


The hat’s problem was the car, and the train tracks weren’t securely fasted onto the hat. So I gluegunned the train track to the hat. Then measured some wire strung to one of the legs on the servo. Get a sturdy wire and string it.

This is the my code for my final project:

#include <Servo.h> 

// constants won’t change. They’re used here to 
// set pin numbers:
const int buttonPin = 10;// the number of the pushbutton pin
const int ledPin =  5;      // the number of the LED pin 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
int pos = 0;    // variable to store the servo position 

const int magbuttonPin = 2;// the number of the pushbutton pin
const int magbuttonPin2 = 12;
const int magbuttonPin3 = 8;
const int magledPin =  13;      // the number of the LED pin

// variables will change:
int magbuttonState = 0;         // variable for reading the pushbutton status
int magbuttonState2 = 0;
int magbuttonState3 = 0;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

int noteFreqArr[] = 
{
49.4, 52.3, 55.4, 58.7, 62.2, 65.9, 69.9, 74, 78.4, 83.1, 88, 93.2, 
98.8, 105, 111, 117, 124, 132, 140, 148, 157, 166, 176, 186, 
198, 209, 222, 235, 249, 264, 279, 296, 314, 332, 352, 373, 
395, 419, 444, 470, 498, 527, 559, 592, 627, 665, 704, 746, 
790, 837, 887, 940, 996, 1050, 1110, 1180, 1250, 1320, 1400, 1490, 
1580, 1670, 1770, 1870, 1990, 2100};

void setup() 
{
    pinMode(magledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(magbuttonPin, INPUT);
  pinMode(magbuttonPin2, INPUT); 
  pinMode(magbuttonPin3, INPUT); 
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  pinMode(3, OUTPUT); // set a pin for buzzer output
}

void playNote(int noteInt, long length, long breath = 20) 
{
  magbuttonState = digitalRead(magbuttonPin);
  magbuttonState2 = digitalRead(magbuttonPin2);
  magbuttonState3 = digitalRead(magbuttonPin3);
  if (magbuttonState == 0 || magbuttonState2 == 0 || magbuttonState3 == 0) 
  {     
    // turn LED on:    
    digitalWrite(magledPin, HIGH);  
  } 
  else {

    // turn LED off:
    digitalWrite(magledPin, LOW); 
  }
  
  length = length – breath;
  buzz(3, noteFreqArr[noteInt], length);
  if(breath > 0) 
  { //take a short pause or ‘breath’ if specified
    delay(breath);
  }
}

void loop()
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
//  if (buttonState == HIGH) 
//  {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
    //
    //pos = ++pos;  // goes from 0 degrees to 180 degrees                            
    myservo.write(82);              // tell servo to go to position in variable ‘pos’ 
  //  delay(200);  
    delay(15); 
    
  playNote(24,500);
  playNote(17,1000);
  playNote(19,250);
  playNote(22,250);
  playNote(21,250);
  playNote(19,250);
  playNote(24,500);
  playNote(24,500);
  playNote(24,250);
  playNote(26,250);
  playNote(21,250);
  playNote(22,250);
  playNote(19,500);
  playNote(19,500);
  playNote(19,250);
  playNote(22,250);
  playNote(21,250);
  playNote(19,250);
  playNote(17,250);
  playNote(29,250);
  playNote(28,250);
  playNote(26,250);
  playNote(24,250);
  playNote(22,250);
  playNote(21,250);
  playNote(19,250);
  playNote(17,1000);
  playNote(19,250);
  playNote(22,250);
  playNote(21,250);
  playNote(19,250);
  playNote(24,500);
  playNote(24,500);
  playNote(24,250);
  playNote(26,250);
  playNote(21,250);
  playNote(22,250);
  playNote(19,500);
  playNote(19,500);
  playNote(19,250);
  playNote(22,250);
  playNote(21,250);
  playNote(19,250);
  playNote(17,250);
  playNote(24,250);
  playNote(19,250);
  playNote(21,250);
  playNote(17,250);
  delay(250);
 // } 

// if (buttonState == LOW) 
//  {
//    // turn LED off:
//    digitalWrite(ledPin, LOW); 
//    //
//    //pos = 0;  // goes from 0 degrees to 180 degrees                            
//    myservo.detach();//write(pos);              // tell servo to go to position in variable ‘pos’ 
//    delay(250); 
//    myservo.attach(9); 
//  }
}

void buzz(int targetPin, long frequency, long length) {
  long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
  //// 1 second’s worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to 
  //// get the total number of cycles to produce
  for (long i=0; i < numCycles; i++){ // for the calculated length of time…
    digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait againf or the calculated delay value
  }
}