Thursday, February 24, 2011

Our Code for Using "Serial.read" in Lab 3

So as Vidya told you I would do in her previous post, today I will be recording our codes from Lab 3. This post contains the program we used in the Arduino, using the "Serial.read" function to create music. Instead of simply playing the tone we used in Lab 2, we created a program that would allow us to play the tune ourselves. We first defined each tone and then were able to command our board to play a tone by using the Serial Monitor and the "inputChar" function. The code is displayed below:

int pin = 5;
int inputChar;
unsigned int a = 440;
unsigned int b = 493.88;
unsigned int C = 523.25;
unsigned int Cs = 554.37;
unsigned int E = 659.26;
unsigned int D = 587.33;
void setup()
{
Serial.begin(9600);
pinMode(pin,OUTPUT);
}
void loop()
{
 inputChar = Serial.read();
 if (inputChar == 'a')
 {
   tone(pin,a);
 }
   if (inputChar == 's')
 {
   tone(pin,b);
 }
   if (inputChar == 'g')
 {
   tone(pin,E);
 }
     if (inputChar == 'r')
 {
   tone(pin,Cs);
 }
     if (inputChar == 'f')
 {
   tone(pin,D);
 }
       if (inputChar == 'd')
 {
   tone(pin,C);
 }
 
}

No comments:

Post a Comment