《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源码可以在这里下载到:
楼主捡两个比较好玩的贴上来:
//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("=========================");
}
//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
啊其实这俩小游戏还挺好玩的。。{:5_183:}
{:5_126:} dsweiliang 发表于 2016-11-7 08:08
{:5_196:} 代码不一样了,这么弄的。。。 swanglei 发表于 2016-11-8 11:32
代码不一样了,这么弄的。。。
不是我弄的 - - 后台整的吧 你好,能分享下Arduino For Musicians这本书吗 xinhailin 发表于 2017-1-4 16:11
你好,能分享下Arduino For Musicians这本书吗
{:5_153:} 给个QQ或者邮箱吧 - -
页:
[1]