Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 //DigitalOut myled(LED1); 00004 00005 //int main() { 00006 // while(1) { 00007 // myled = 1; 00008 // wait(0.2); 00009 // myled = 0; 00010 // wait(0.2); 00011 // } 00012 //} 00013 00014 void setup(); 00015 void loop(); 00016 void flash_eyes(); 00017 int read_accel(); 00018 void beep(DigitalOut speakerPin, int frequency, long duration); 00019 void play(DigitalOut speakerPin, const char *note, long duration); 00020 void play_tune(); 00021 00022 //DigitalOut ledPin = 13; // LED is connected to digital pin 13 00023 DigitalOut speakerPin(p8); // speaker connected to p8 00024 DigitalOut catseye1(LED1); // cat's eye no1 00025 DigitalOut catseye2(LED2); // cat's eye no2 00026 //DigitalOut moveSensor = 3; // analogue input for accelerometer 00027 //DigitalOut buttonPin = 11; // button input 00028 00029 // A note in one octave is twice the frequency of the same note in the octave 00030 // below. We define here the frequencies of the notes in octave 8. To get 00031 // notes in lower octaves, we just divide by two however many times. 00032 00033 #define NOTE_C8 4186 00034 #define NOTE_CSHARP8 4434 00035 #define NOTE_D8 4698 00036 #define NOTE_DSHARP8 4978 00037 #define NOTE_E8 5274 00038 #define NOTE_F8 5587 00039 #define NOTE_FSHARP8 5919 00040 #define NOTE_G8 6271 00041 #define NOTE_GSHARP8 6644 00042 #define NOTE_A8 7040 00043 #define NOTE_ASHARP8 7458 00044 #define NOTE_B8 7902 00045 00046 // This is an array of note frequencies. Index the array essentially by note 00047 // letter multiplied by two (A = 0, B = 2, C = 4, etc.). Add one to index for 00048 // "sharp" note. Where no sharp note exists, the natural note is just 00049 // duplicated to make this indexing work. The play() function below does all 00050 // of this for you :) 00051 00052 int octave_notes[14] = { 00053 NOTE_A8, NOTE_ASHARP8, 00054 NOTE_B8, NOTE_B8, 00055 NOTE_C8, NOTE_CSHARP8, 00056 NOTE_D8, NOTE_DSHARP8, 00057 NOTE_E8, NOTE_E8, 00058 NOTE_F8, NOTE_FSHARP8, 00059 NOTE_G8, NOTE_GSHARP8, 00060 }; 00061 00062 // This variable tracks the current state of the eye LEDs. 00063 int eyes = 0; 00064 00065 // Arduino runs this bit of code first, then repeatedly calls loop() below. So 00066 // all initialisation of variables and setting of initial pin modes (input or 00067 // output) can be done here. 00068 00069 void setup() { 00070 //pinMode(ledPin, OUTPUT); // sets the ledPin to be an output 00071 //pinMode(speakerPin, OUTPUT); // sets the speakerPin to be an output 00072 //eyes = LOW; // initial state of cats eyes is LOW 00073 //pinMode(catseye1, OUTPUT); // sets the cats eye1 to be an output 00074 //pinMode(catseye2, OUTPUT); // sets the cats eye2 to be an output 00075 //pinMode(buttonPin, INPUT); // sets the cats eye2 to be an output 00076 // Serial.begin(9600); 00077 } 00078 00079 // Arduino will run this over and over again once setup() is done. 00080 00081 void loop() 00082 { 00083 //read_accel(); 00084 play_tune(); // call the play_tune() function 00085 wait(1.0); // delay for 1 second 00086 } 00087 00088 // ------------------------------------------------------------------------- 00089 00090 // A function to toggle the cat's eyes on and off. 00091 void flash_eyes() 00092 { 00093 // Invert the desired state of the cat's eyes: 00094 eyes = !eyes; 00095 // if (eyes == LOW) { 00096 // eyes = HIGH; 00097 // } else { 00098 // eyes = LOW; 00099 // } 00100 00101 catseye1 = eyes; 00102 catseye2 = eyes; 00103 // Write the new value to all the LED pins: 00104 //digitalWrite(ledPin, eyes); 00105 //digitalWrite(catseye1, eyes); 00106 //digitalWrite(catseye2, eyes); 00107 } 00108 00109 // ------------------------------------------------------------------------- 00110 00111 // Read accelerometer 00112 int read_accel() 00113 { 00114 // static int last_accel = 0; 00115 // int in = analogRead(moveSensor); 00116 // int diff = last_accel - in; 00117 // last_accel = in; 00118 // if (diff < -5 || diff > 5) { 00119 // return diff; 00120 // } 00121 return 0; 00122 } 00123 00124 // To produce a tone, this function toggles the speaker output pin at the 00125 // desired frequency (in Hz). It calculates how many times to do this to 00126 // produce a note of the desired length (in milliseconds). 00127 00128 void beep(DigitalOut speakerPin, int frequency, long duration) 00129 { 00130 00131 int i; 00132 long delayAmount = (long)(1000000/frequency); 00133 long loopTime = (long)((duration*1000)/(delayAmount*2)); 00134 00135 //int accel_diff = 0; 00136 // int button_in = digitalRead(buttonPin); 00137 for (i = 0; i < loopTime; i++) { 00138 speakerPin = 1; 00139 wait_us(delayAmount); 00140 speakerPin = 0; 00141 wait_us(delayAmount); 00142 } 00143 } 00144 00145 void play(DigitalOut speakerPin, const char *note, long duration) 00146 { 00147 int octave_number = 4; // default to octave 4 00148 int i = 0; 00149 00150 // Check for valid note letter 00151 if (note[i] >= 'A' && note[i] <= 'G') { 00152 // Calculate index into octave_notes[] 00153 int note_index = (note[i] - 'A') * 2; 00154 i++; 00155 // Check for sharp sign 00156 if (note[i] == '#') { 00157 note_index++; 00158 i++; 00159 } 00160 // Check for an octave number 00161 if (note[i] >= '0' && note[i] <= '8') { 00162 octave_number = note[i] - '0'; 00163 i++; 00164 } 00165 // Fetch the note frequency from the octave_notes[] table 00166 int frequency = octave_notes[note_index]; 00167 00168 // That will be the frequency for the note in octave 8, so we 00169 // need to divide it by two for each octave lower that we 00170 // actually want. 00171 00172 // The '>>' operator is a useful shorthand that (for integers 00173 // >= 0) basically translates to "divide by two this many 00174 // times", so we will use that: 00175 00176 frequency = frequency >> (8 - octave_number); 00177 00178 // Actually play the note! 00179 beep(speakerPin, frequency, duration); 00180 } 00181 } 00182 00183 void play_tune() 00184 { 00185 flash_eyes(); play(speakerPin, "C6", 500); // twin- 00186 flash_eyes(); play(speakerPin, "C6", 500); // -kle 00187 flash_eyes(); play(speakerPin, "G6", 500); // twin- 00188 flash_eyes(); play(speakerPin, "G6", 500); // -kle 00189 flash_eyes(); play(speakerPin, "A6", 500); // lit- 00190 flash_eyes(); play(speakerPin, "A6", 500); // -tle 00191 flash_eyes(); play(speakerPin, "G6", 1000); // star 00192 flash_eyes(); play(speakerPin, "F6", 500); // how 00193 flash_eyes(); play(speakerPin, "F6", 500); // i 00194 flash_eyes(); play(speakerPin, "E6", 500); // won- 00195 flash_eyes(); play(speakerPin, "E6", 500); // -der 00196 flash_eyes(); play(speakerPin, "D6", 500); // what 00197 flash_eyes(); play(speakerPin, "D6", 500); // you 00198 flash_eyes(); play(speakerPin, "C6", 1000); // are 00199 00200 flash_eyes(); play(speakerPin, "G6", 500); // up 00201 flash_eyes(); play(speakerPin, "G6", 500); // a- 00202 flash_eyes(); play(speakerPin, "F6", 500); // -bove 00203 flash_eyes(); play(speakerPin, "F6", 500); // the 00204 flash_eyes(); play(speakerPin, "E6", 500); // world 00205 flash_eyes(); play(speakerPin, "E6", 500); // so 00206 flash_eyes(); play(speakerPin, "D6", 1000); // high 00207 flash_eyes(); play(speakerPin, "G6", 500); // like 00208 flash_eyes(); play(speakerPin, "G6", 500); // a 00209 flash_eyes(); play(speakerPin, "F6", 500); // dia- 00210 flash_eyes(); play(speakerPin, "F6", 500); // -mond 00211 flash_eyes(); play(speakerPin, "E6", 500); // in 00212 flash_eyes(); play(speakerPin, "E6", 500); // the 00213 flash_eyes(); play(speakerPin, "D6", 1000); // sky 00214 00215 flash_eyes(); play(speakerPin, "C6", 500); // twin- 00216 flash_eyes(); play(speakerPin, "C6", 500); // -kle 00217 flash_eyes(); play(speakerPin, "G6", 500); // twin- 00218 flash_eyes(); play(speakerPin, "G6", 500); // -kle 00219 flash_eyes(); play(speakerPin, "A6", 500); // lit- 00220 flash_eyes(); play(speakerPin, "A6", 500); // -tle 00221 flash_eyes(); play(speakerPin, "G6", 1000); // star 00222 flash_eyes(); play(speakerPin, "F6", 500); // how 00223 flash_eyes(); play(speakerPin, "F6", 500); // i 00224 flash_eyes(); play(speakerPin, "E6", 500); // won- 00225 flash_eyes(); play(speakerPin, "E6", 500); // -der 00226 flash_eyes(); play(speakerPin, "D6", 500); // what 00227 flash_eyes(); play(speakerPin, "D6", 500); // you 00228 flash_eyes(); play(speakerPin, "C6", 1000); // are 00229 00230 } 00231 00232 int main(void) 00233 { 00234 //init(); 00235 00236 setup(); 00237 00238 while (1) { 00239 loop(); 00240 } 00241 00242 return 0; 00243 } 00244
Generated on Thu Aug 11 2022 07:45:03 by
1.7.2