越而胜己 / Beyond awesome

Today we did Arduino. Arduino is an development kit designed for novices and professionals to develop their own device to control real-life appliances in an inexpensive way. With Arduino, a developer can make a project with only a few lines of code. Its efficiency and easiness makes using Arduino interesting and helpful.

Apart from the examples given by the guide, we did a project on our own. It was 8-digit a binary counter, which counts from 0 to 255. We used eight LED lights and eight 330-ohm resistors to do the project.

int ledPins[] = {13,12,11,10,9,8,7,6};
int a[8];
void setup() {
  // put your setup code here, to run once:
  for (int i = 0; i < 8; i++)
    pinMode(ledPins[i],OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  int n;
  for (int i = 0; i < 256; i++){
    n = i;
    for (int j = 0; j < 8; j++){
      a[j] = n % 2;
      n /= 2;
    }
    for (int k = 0; k < 8; k++)
      digitalWrite(ledPins[k], a[k]);  
    delay(300);
  }
}


Apart from that, we did a music player which plays 千本桜 and Zelda's Lullaby. We just edited some code from the examples to makes this. The Zelda's Lullaby code goes below:

const int buzzerPin = 9;
const int songLength = 57;
char notes[] = "bDagabDabDAGDCbabDagabDabDAG2221B1BG1BABAE21B1BG155"; // a space represents a rest
int beats[] = {8,4,8,2,2, 8,4,12, 8,4,8,4,8,2,2,12, 8,4,8,2,2,8,4,12, 8,4,8,4,12,12, 8,2,2,2,2,8,8,2,2,2,2,8,8,2,2,2,2,4,4,12,12};
int tempo = 150;
void setup() 
{
  pinMode(buzzerPin, OUTPUT);
}
void loop() 
{
  int i, duration;
  for (i = 0; i < songLength; i++) 
  {
    duration = beats[i] * tempo;  
    if (notes[i] == ' ')     
    {
      delay(duration);      
    }
    else                  
    {
      tone(buzzerPin, frequency(notes[i]), duration);
      delay(duration);        
    }
    delay(tempo/10);             
  }
}
int frequency(char note) 
{
  int i;
  const int numNotes = 16; 
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B', '1', '2', '3', '4', '5' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523, 587, 659, 698, 784, 880, 988, 1046, 1175, 1318, 1397, 1568};
  for (i = 0; i < numNotes; i++)
  {
    if (names[i] == note)  
    {
      return(frequencies[i]); 
    }
  }
  return(0);  
}