7161浏览
查看: 7161|回复: 6

[入门] 《Arduino For Musicians》阅读笔记 Chapter 2

[复制链接]
{:5_142:}楼主最近打算用Arduino做个好玩的合成器啥的~,于是开始刷《Arduino For Musicians》这本书~~~哈哈哈哈哈哈~~

Chapter 2讲的是Introduction To Programming……楼主瞅了眼,讲的是基础的C编程,变量类型(int, float......)、判断(if..else.., switch)、循环(for, while......)、function..
基本就这些内容。

所有的Arduino源码可以在这里下载到:

楼主捡两个比较好玩的贴上来:
[mw_shl_code=c,true]//Arduino for Musicians
//Listing 2.7: MIDI Note Explorer

//An enumeration is a convenient way to refer to note numbers.
//In this case, C = 0, Db = 1, and so on
enum{C, Db, D, Eb, E, F, Gb, G, Ab, A, Bb, B};

void setup()
{
    Serial.begin(9600);
    Serial.println("Enter a MIDI note number: ");
}

void loop()
{
     if (Serial.available() > 0)
    {
        //Read the incoming byte.
        int value = Serial.parseInt();
               
        //Print note information.
        printNoteInfo(value, getOctave(value), getNoteName(value));
               
        //Prompt the user to enter another number:
        Serial.println("Enter a MIDI note number: ");
    }
}


int getOctave(int midi_note)
{
    //Calculate the octave of the midi note
    return (midi_note / 12) -1;
}

int getNoteNumber(int midi_note)
{
    //Calculate the midi note value (0-11)
    return midi_note % 12;
}

char const *getNoteName(int midi_note)
{
     //Get note number (0-11)
     int note = getNoteNumber(midi_note);
     
     //Use a switch statement to determine note name. This could also
     //be done with a series of "if" statements.
     switch(note)
     {
       //Note: each case returns so break keyword is not needed here
        case C:   return "C";   
        case Db:  return "Db";
        case D:   return "D";
        case Eb:  return "Eb";
        case E:   return "E";
        case F:   return "F";
        case Gb:  return "Gb";
        case G:   return "G";
        case Ab:  return "Ab";
        case A:   return "A";
        case Bb:  return "Bb";  
        case B:   return "B";
        default: return "Unknown";
     }
}

void printNoteInfo(int value, int octave, char const * note_name)
{
      //Print information about the note:
      Serial.print("Value: ");    Serial.println(value);
      Serial.print("Octave: ");   Serial.println(octave);
      Serial.print("Note: ");     Serial.println(note_name);
      Serial.println("=========================");
}

[/mw_shl_code]


[mw_shl_code=c,true]//Arduino for Musicians
//Listing 2-14 through 2-17: Tempo Game


//LISTING 2.14
const int LED_PIN = 13;

int tempo;                      //Tempo in BPM
long delay_MS;                  //Delay in milliseconds
boolean blink_status = true;    //true == LED on, false == LED off
int tries;                      //Tracks the number of tries to guess the tempo
int precision= 3;               //Determines accuracy of guess (within +/- 3 BPM)

void setup()
{
    //Establish 9600 baud serial connection
    Serial.begin(9600);
   
    //Set up digital pin as an output:
    pinMode(LED_PIN, OUTPUT);   
   
    //Initialize (seed) the random number generator
    randomSeed(analogRead(0));
   
    //Set up a new game:
    newGame();
}

//END LISTING 2.14

//LISTING 2.15
void newGame()
{
    //Reset the number of tries
    tries = 1;
   
    //select a random tempo between 30 and 150 BPM
    tempo = random(30, 150);
   
    //Calculate the amount of delay:
    //milliseconds per minute/beats per minute
    unsigned int milliseconds_per_minute = 1000 * 60;
    delay_MS = milliseconds_per_minute/tempo;
   
    //divide by 2 to get half the period
    delay_MS = delay_MS/2;
   
    //Print a message:
    Serial.println("Enter the tempo in BPM (e.g. 120): ");  
}

//END LISTING 2.15

//LISTING 2.16
void blinkLED()
{
    static long last_time = 0;  //Used to track milliseconds between blinks
   
    //Get the current time in milliseconds
    long current_time = millis();
   
    //If the current time - last time is greater than the delay time
    if(current_time - last_time >= delay_MS)
    {
        //Toggle blink status (false = true or true = false
        blink_status = !blink_status;
        last_time = current_time;  //reset last_time to current_time
        updateLED();
    }
}

void updateLED()
{
     //Toggle the LED
    if(blink_status == true)
    {
        digitalWrite(LED_PIN, HIGH);
    }else{
        digitalWrite(LED_PIN, LOW);
    }
}

void showCorrectAnswer()
{
    Serial.print("Congratulations! You selected the correct tempo in ");
    Serial.print(tries);
    Serial.println(" tries!");
    Serial.print("The exact tempo is: ");
    Serial.println(tempo);
}

//END LISTING 2.16

//LISTING 2.17
void loop()
{
    blinkLED();
   
    //Check responses
    if (Serial.available() > 0)
    {
        //Read the incoming byte.
        long input = Serial.parseInt();
        
        //Check responses:
        if(input > tempo + precision)
        {
           Serial.println("Sorry, your guess is too high. Try again.");
           tries++;
        }
        
        if(input < tempo - precision)
        {
           Serial.println("Sorry, your guess is too low. Try again.");
           tries++;
        }
        
                  //See if guess is within the range of the current tempo + precision
                  //and current tempo - precision
        if(input <= tempo + precision && input >= tempo - precision)
        {
             //Correct answer
             showCorrectAnswer();
            
                        //Start a new game:
             newGame();
        }
        
    }
}

//END LISTING 2.17
[/mw_shl_code]



啊其实这俩小游戏还挺好玩的。。{:5_183:}

dsweiliang  初级技神

发表于 2016-11-7 08:08:16

回复

使用道具 举报

iooops  中级技匠
 楼主|

发表于 2016-11-7 22:41:39

回复

使用道具 举报

swanglei  中级技神

发表于 2016-11-8 11:32:29

代码不一样了,这么弄的。。。
回复

使用道具 举报

iooops  中级技匠
 楼主|

发表于 2016-11-10 21:37:19

swanglei 发表于 2016-11-8 11:32
代码不一样了,这么弄的。。。

不是我弄的 - - 后台整的吧
回复

使用道具 举报

xinhailin  学徒

发表于 2017-1-4 16:11:19

你好,能分享下Arduino For Musicians这本书吗
回复

使用道具 举报

iooops  中级技匠
 楼主|

发表于 2017-1-5 12:06:43

xinhailin 发表于 2017-1-4 16:11
你好,能分享下Arduino For Musicians这本书吗

给个QQ或者邮箱吧 - -
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail