Source

arduino_hack_day / pianinho.pde

Full commit
/*
 * Copyright (C) 2009-2010 Álvaro Justen <alvaro@justen.eng.br>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 dated June, 1991.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * If you find any bugs or have any suggestions, please email me.
 *
 * This software was created on Arduino Hack Day (on 2009-12-) 
 * See more details and authors at:
 */

const int buzzer = 2;
int delay_microsec = 10000;
int byte_lido = 0;
int ligado = 0;

#define LEDR 10
#define LEDG 11
#define LEDB 13

#define BDO 9
#define BRE 8
#define BMI 7
#define BFA 6
#define BSOL 5
#define BLA 4
#define BSI 3
#define BON 12

#define DO 261
#define DOs 277
#define RE 293
#define REs 311
#define MI 329
#define FA 349
#define FAs 369
#define SOL 391
#define SOLs 415
#define LA 440
#define LAs 466
#define SI 493
#define DO2 523


void setup()   {                
  pinMode(buzzer, OUTPUT);
  pinMode(BDO, INPUT);
  pinMode(BRE, INPUT);
  pinMode(BMI, INPUT);
  pinMode(BFA, INPUT);
  pinMode(BSOL, INPUT);
  pinMode(BLA, INPUT);
  pinMode(BSI, INPUT);
  pinMode(BON, INPUT);

  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDG, HIGH);
  digitalWrite(LEDB, HIGH);

  digitalWrite(BDO, HIGH);
  digitalWrite(BRE, HIGH);
  digitalWrite(BMI, HIGH);
  digitalWrite(BFA, HIGH);
  digitalWrite(BSOL, HIGH);
  digitalWrite(BLA, HIGH);
  digitalWrite(BSI, HIGH);
  digitalWrite(BON, HIGH);
  //Serial.begin(9600);
}

int nota_to_delay(int nota) {
  return (1000000 / 2) / nota;
}

void loop() {
  if (digitalRead(BDO) == LOW) {
    delay_microsec = nota_to_delay(DO);
    digitalWrite(LEDR, LOW);
    ligado = 1;
  }
  else if (digitalRead(BRE) == LOW) {
    delay_microsec = nota_to_delay(RE);
    digitalWrite(LEDG, LOW);
    ligado = 1;
  }
  else if (digitalRead(BMI) == LOW) {
    delay_microsec = nota_to_delay(MI);
    digitalWrite(LEDB, LOW);
    ligado = 1;
  }
  else if (digitalRead(BFA) == LOW) {
    delay_microsec = nota_to_delay(FA);
    ligado = 1;
    digitalWrite(LEDR, HIGH);
  }
  else if (digitalRead(BSOL) == LOW) {
    delay_microsec = nota_to_delay(SOL);
    ligado = 1;
    digitalWrite(LEDG, HIGH);
  }
  else if (digitalRead(BLA) == LOW) {
    delay_microsec = nota_to_delay(LA);
    ligado = 1;
    digitalWrite(LEDB, HIGH);
  }
  else if (digitalRead(BSI) == LOW) {
    delay_microsec = nota_to_delay(SI);
    ligado = 1;
    digitalWrite(LEDR, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDB, HIGH);
  }
  else {
    ligado = 0;
    //digitalWrite(LEDR, HIGH);
    //digitalWrite(LEDG, HIGH);
    //digitalWrite(LEDB, HIGH);
  }

  if (digitalRead(BON) == LOW) {
    ligado = 0;
  }

  if (ligado == 1) {
    analogWrite(buzzer, 255);
    delayMicroseconds(delay_microsec);
    analogWrite(buzzer, 0);
    delayMicroseconds(delay_microsec);
  }
  /*
  if (Serial.available() > 0) {    
    byte_lido = Serial.read();
    if (byte_lido >= '0' && byte_lido <= '9') {
      switch (byte_lido) {
        case '1':
          Serial.println("Do");
          break;
        case '2':
          delay_microsec = nota_to_delay(RE);
          Serial.println("Re");
          break;
        case '3':
          delay_microsec = nota_to_delay(MI);
          Serial.println("Mi");
          break;        
        case '4':
          delay_microsec = nota_to_delay(FA);
          Serial.println("Fa");
          break;
        case '5':
          delay_microsec = nota_to_delay(SOL);
          Serial.println("Sol");
          break;
        case '6':
          delay_microsec = nota_to_delay(LA);
          Serial.println("La");
          break;
        case '7':
          delay_microsec = nota_to_delay(SI);
          Serial.println("Si");
          break;
        case '8':
          delay_microsec = nota_to_delay(DO2);
          Serial.println("Do2");
          break;
      }
      ligado = 1;
    }
    else {
      ligado = 0;
    }
  }
  */
}