Page 87
#define max(a,b) ((a) > (b) ? (a) : (b))
#define square(x) x * x
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
#define dprint(expr) Serial << #expr " = " << expr << "\n";
void setup() {
Serial.begin(115200);
int x = 20.0;
int y = 30.0;
int z = 6.0;
Serial << "max(x, y): " << max(x, y) << "\n";
Serial << "max(x+, y++): " << max(x++, y++) << "\n"; // returns 31
dprint(max(x, y));
dprint(max(x++, y++)); // returns 33, K&R expected 32 from their dprint
dprint(square(z));
dprint(square(z+1)); // evaluates to 6 + 1 * 6 + 1 = 13
dprint(square((z+1))); // returns 49 (note brackets)
}
Page 90
#include "pitches.h"
// notes of the tune as frequencies:
const unsigned int tune[] = {
C4, C4, C4, D4, D4, D4, E4, F4, G4, C4, B3, C4, D4, A3,
C4, C4, C4, D4, D4, D4, E4, F4, G4, C4, D4, B3, C4,
C4, C4, C4, C4, C4, D4, C4, C5, C5, C5, C5, C5, D5, C5
};
const int notes[] = {
4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2,
4, 8, 8, 4, 4, 1, 1, 4, 8, 8, 4, 4, 1, 1
};
// note = duration: 4 crotchet, 2 minim, 1 semibreve, 8 quaver
const byte tonePin = 3;
const int timeSig = 1000; // initial value based on 1 second
void setup() {
for (int i = 0, j = sizeof(tune) / sizeof(int); i < j; i++) {
int duration = timeSig / notes[i];
int space = duration * 1.3;
tone(tonePin, tune[i], duration);
delay(space); // delay starts aprox at tone start
}
noTone(tonePin);
}
pitches.h
/*
* My version of pitches.h
* Includes actual values as comments for tweaking
* Range of frequencies is valid for most Arduino models
*/
#define REST 0 // use as a rest
#define B0 31 //30.87
#define C1 33 // 32.7
#define CS1 35 // DF1 34.65
#define D1 37 // 36.71
#define DS1 39 // EF1 38.89
#define E1 41 // 41.2
#define F1 44 // 43.65
#define FS1 46 // GF1 46.25
#define G1 49
#define GS1 52 // AF1 51.91
#define A1 55
#define AS1 58 // BF1 58.27
#define B1 62 // 61.74
#define C2 65 // 65.41
#define CS2 69 // DF2 69.3
#define D2 73 // 73.42
#define DS2 78 // EF2 77.78
#define E2 82 // 82.41
#define F2 87 //87.31
#define FS2 93 // GF2 92.5 **
#define G2 98
#define GS2 104 // AF2 103.83
#define A2 110
#define AS2 117 // BF2 116.54
#define B2 123 // 123.47
#define C3 131 // 130.81
#define CS3 139 // DF3 138.59
#define D3 147 // 146.83
#define DS3 156 // 155.56
#define E3 165 // 164.81
#define F3 175 // 174.61
#define FS3 185
#define G3 196
#define GS3 208 // 207.65
#define A3 220
#define AS3 233 // 233.08
#define B3 247 // 246.94
#define C4 262 // 261.63 Middle C
#define CS4 277 // 277.18
#define D4 294 // 293.66
#define DS4 311 // 311.13
#define E4 330 // 329.63
#define F4 349 // 249.23
#define FS4 370 // 369.99
#define G4 392
#define GS4 415 // 415.3
#define A4 440
#define AS4 466 // 466.16
#define B4 494 // 493.88
#define C5 523 // 523.25
#define CS5 554 // 554.37
#define D5 587 // 587.33
#define DS5 622 // 622.25
#define E5 659 // 659.25
#define F5 698 // 698.46
#define FS5 740 // 739.99
#define G5 784 // 783.99
#define GS5 831 //830.61
#define A5 880
#define AS5 932 // 932.33
#define B5 988 // 987.77
#define C6 1047 // 1046.5 **
#define CS6 1109 // 1108.73
#define D6 1175 // 1174.66
#define DS6 1245 // 1244.51
#define E6 1319 // 1318.51
#define F6 1397 // 1396.91
#define FS6 1480 // 1479.98
#define G6 1568 // 1567.98
#define GS6 1661 // 1661.22
#define A6 1760
#define AS6 1865 // 1864.66
#define B6 1976 // 1975.53
#define C7 2093
#define CS7 2217 // 2217.46
#define D7 2349 // 2349.32
#define DS7 2489 // 2489.02
#define E7 2637 // 2637.02
#define F7 2794 // 2793.83
#define FS7 2960 // 2959.96
#define G7 3136 // 3135.96
#define GS7 3322 // 3322.44
#define A7 3520
#define AS7 3729 // 3729.31
#define B7 3951 // 3951.07
#define C8 4186 // 4186.01
#define CS8 4435 // 4434.92
#define D8 4699 // 4698.63
#define DS8 4978 // 4978.03
#define E8 5274 // 5274.04
#define F8 5588 // 5587.65
#define FS8 5920 // 5919.91
#define G8 6272 // 6271.93