Kentaro Kuribayashi's blog

Software Engineering, Management, Books, and Daily Journal.

Generating music using Arduino

I bought japanino, yet another Arduino compatible board shipped with 大人の科学 Vol.27, yesterday and created generative music using it. The algorithm of the music is borrowed from this entry. It leaves much to improve but very simple and fun enough. Why don't you try it?

It's made with very simple codes below::

int t = 0;
int buzzer = 14;
int start  = 7;
int end    = 13;

void setup () {
  for (int i = start; i <= end; i++) {
    pinMode(i, OUTPUT); 
  }

  pinMode(buzzer, OUTPUT);
}

void loop () {
  t++;
  tone(buzzer, t*(((t>>12)|(t>>8))&(63&(t>>4))));

  int rand = random(start, end + 1);
  for (int i = start; i <= end; i++) {
    int mode = i == rand ? HIGH : LOW;
    digitalWrite(i, mode);
  }

  delay(3);
}