/hardware/

Piano

2013-07-10 20:14:29

const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;

int potencjometrPin = A0;

const int ledPin =  13;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;

const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;

const int buzzerPin = 8;
const int ledPin1 = 12;
const int ledPin2 = 13;

int counter = 0;

int sensorValue = 0;

void setup() {
  pinMode(ledPin, OUTPUT);      

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);

  Serial.begin(9600);
}

void loop(){

  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);

  if (buttonState1 == HIGH) {     
      beep(c, 225);
  }
  if (buttonState2 == HIGH) {     
      beep(d, 225);
  }
  if (buttonState3 == HIGH) {     
      beep(e, 225);
  }
  if (buttonState4 == HIGH) {     
      beep(f, 225);
  }
  if (buttonState5 == HIGH) {     
      beep(g, 225);
  }
}

void beep(int note, int duration)
{

  sensorValue = analogRead(potencjometrPin);

  //Play tone on buzzerPin
  _log(sensorValue);
  tone(buzzerPin, note+sensorValue, duration);

  //Play different LED depending on value of 'counter'
  if(counter % 2 == 0)
  {
    digitalWrite(ledPin1, HIGH);
    delay(duration);
    digitalWrite(ledPin1, LOW);
  }else
  {
    digitalWrite(ledPin2, HIGH);
    delay(duration);
    digitalWrite(ledPin2, LOW);
  }

  //Stop tone on buzzerPin
  noTone(buzzerPin);

  delay(50);

  //Increment counter
  counter++;
}

void _log(int message){
  Serial.println(message);
}