Lessons 8 & 9 – code example

Here is the code of the Piano Program

// By Roee Bloch
//All right Reserved
// Copyright (c) 2015 All Right Reserved, http://www.electronics-freak.com
//
// This source is subject to the Roee Bloch License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
//
// Roee Bloch
// roeebloch@walla.co.il
// June 2015

#define octave 2 // Octav to hear Lower tones leave value 1
#define Buz1 5 // buzzer pin 1
#define Buz2 7 // buzzer pin 2
#define DO 2500/octave //cycle time of DO in Micro seconds of musical notes
#define RE 2028/octave
#define MI 1805/octave
#define FA 1702/octave
#define SOL 1516/octave
#define LA 1351/octave
#define SI 1204/octave
#define analog_in 0

int my_analog;
int remap;

     void setup() {
    // put your setup code here, to run once:
       pinMode(Buz1, OUTPUT);
       pinMode(Buz2, OUTPUT);
       Serial.begin(9600);

     }

void loop() {
// put your main code here, to run repeatedly:
my_analog = analogRead(analog_in);
Serial.println (my_analog); //for debug only

remap = map (my_analog, 0, 1024, 0, 8); // remapping to switch case
// Serial.println (remap); // also for debug
switch (remap)
{

  case 1: Serial.println ("DO");
          sound(DO);
  break;
  case 2: Serial.println ("RE");
          sound(RE);
  break;
  case 3: Serial.println ("MI");
          sound(MI);
  break;
  case 4: Serial.println ("FA");
          sound(FA);
  break;
  case 5: Serial.println ("SOL");
          sound(SOL);
  break;
  case 6: Serial.println ("LA");
          sound(LA);
  break;
  case 7: Serial.println ("SI");
          sound(SI);
  break;

}
delay(5);

}
void sound(int mydelay) //mydelay is the actual frequency according to musical note
{
    for (int q = 1; q < 100; q++)
    {
        digitalWrite(Buz1, 1);
        digitalWrite(Buz2, 0);
        delayMicroseconds(mydelay / 2);
        digitalWrite(Buz1, 0);
        digitalWrite(Buz2, 1);
        delayMicroseconds(mydelay / 2);
    }
}

Lessons 8,9 – Code Example

[adsenseyu1]