Project Pumpkin

From All Hands Active Wiki
Revision as of 20:34, 20 October 2013 by JoshW (talk | contribs)
Jump to navigation Jump to search

For the 2013 Halloween Tyler hosted an Arduino + Pumpkin workshop. The goal: Make Pumpkins more awesome using an Arduino.

So far we have pummeled three SPST switches into the top of a pumpkin.

Two of them are wired up to the Arduino on pins 3 & 4.

- Switch on Pin 3 activates a light display from three LEDs attached to pins 9, 10, and 11.
- Switch on Pin 4 activates the halloween theme on a speaker connected to pins 6.
- Unwired switch doesn't do anything, yet.

Short video: http://www.youtube.com/watch?v=Xpr7qTlyAkQ&feature=youtu.be

Tyler wrote the code for the light display. Frank wrote the code for the halloween theme. Josh mashed it together in all the wrong ways:

// give it a name:
int led[] = {9, 10, 11};
int ledCount = 3;
int button1Pin = 3;
int button2Pin = 4;
int delayTime = 200;
int buttonState1 = 0; 
int buttonState2 = 0; 

// ######## pumpkin audio setup code ######## 

int speakerPin = 6;

int length = 10; // the number of notes
//C, F, F, C, F, F, C, F, C#, F
char notes[] = "CffCffCfcf"; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int tempo = 130;

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}
//
void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  //low pitched
  int tones[] = { 890, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
  //high pitched
 // int tones[] = { 590, 1400, 1219, 1132, 975, 836, 714, 656 };

  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}
// ######## end pumpkin audio setup code ######## 


void setup() {                
  // initialize the digital pin as an output.
  for (int thisPin = 0; thisPin < ledCount; thisPin++)  {
    pinMode(led[thisPin], OUTPUT);      
  }
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(speakerPin, OUTPUT);

  Serial.begin(9600);
}

void loop() {
//  buttonState1 = digitalRead(button1Pin);
//  buttonState2 = digitalRead(button2Pin);

  Serial.print("B1");
  Serial.print(buttonState1 && ";");
  Serial.print("\t");
  Serial.print("B2");
  Serial.println(buttonState2 && ";");
  if (buttonState1 == HIGH) {    
    for (int thisPin = 0; thisPin < ledCount; thisPin++) {
      digitalWrite(led[thisPin], HIGH);  
      delay(delayTime);
      digitalWrite(led[thisPin], LOW);    
    }
    for (int thisPin = ledCount - 1; thisPin >= 0; thisPin--) {
      digitalWrite(led[thisPin], HIGH);
      delay(delayTime);
      digitalWrite(led[thisPin], LOW);
    }
  } else {
    for (int thisPin = 0; thisPin < ledCount; thisPin++) {
      digitalWrite(led[thisPin], LOW);    
    }
  }
  
  if (buttonState2 == HIGH){
      for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats[i] * tempo);
    }

    // pause between notes
    delay(tempo / 2); 
  }
  }
}