9. Embedded programming (Obtainium Music)

This week I worked on reading the ATTiny44 data sheet and programmed my board (using C++) to play music when the button was touched…

Research

Obtainium: Material used to create, that wasn’t bought new, but obtained in other ways, such as second-hand, dumpster diving, chance findings or donations. Obtainium users create from things that others throw away, I call what I did for this project Obtainium Music.

Code Example

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 3;     // the number of the pushbutton pin
const int ledPin =  7;      // the number of the LED pin

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

int speakerOut = 2;     // The number of the Speaker          
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//                                10                  20                  30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

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

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    delay(250);

 analogWrite(speakerOut, 0);     
  for (count = 0; count < MAX_COUNT; count++) {
    statePin = !statePin;
    digitalWrite(ledPin, statePin);
    for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
      for (count2=0;count2<8;count2++) {
        if (names[count2] == melody[count*2 + 1]) {       
          analogWrite(speakerOut,500);
          delayMicroseconds(tones[count2]);
          analogWrite(speakerOut, 0);
          delayMicroseconds(tones[count2]);
        } 
        if (melody[count*2 + 1] == 'p') {
          // make a pause of a certain size
          analogWrite(speakerOut, 0);
          delayMicroseconds(500);
        }
      }
    }
  }

  }
}

For this week assignment, I used our Hello world modified board ( with the added button and Led) and attached a buzzer (that belonged to one of my Daughters destroyed toys) to the pin 11. The pin 11 according to the ATTiny44 Datasheet corresponds to the Port A driver 2 of the Microcontroller. So, I modified the program used in the week 7 assignment to play a series of tones whenever the button was pressed.

By adding/recovering a component from a destroyed toy, that otherwise would have been discarded in the trash, we gave new life to this Obtainium Music Generator

From Youtube

Playing Obtainium Music....