/* * Example 1 demonstrates: * -- main * -- function calls, prototypes and definitions * -- declaring variables * -- conditionals * -- for loops * -- printing to the console * -- global constants using #define * -- including libraries using #include * -- the sleep function * -- Connecting to the Create, setting it in safe mode * -- Turning the Create center LED on and off * -- Playing notes on the Create */ #include #include #include #include #include #include #define NUMBER_OF_TIMES_TO_FLASH_LED 10 #define BEEP_SONG 15 // Song 15 is reserved for beeping #define BEEP_NOTE 50 // Note to use for a beep #define BEEP_DURATION 16 // Number of 1/64th's of a second /* * Flash (turn on and then off, with a half-second delay between those actions) * the center LED the given number of times. */ void flash_led(int n); /* * Beep one time. */ void beep_once(); /****************************************************************************************************** * MAIN starts here. ****************************************************************************************************** */ /* * Connects to the Create and flashes the center LED the specified number of times. * Then plays a sequence of notes, several times. */ int main() { if (create_connect() == 0) { printf("Connected to the Create robot.\n"); } else { printf("Could not connect to the Create robot.\n"); exit(1); } create_safe(); flash_led(NUMBER_OF_TIMES_TO_FLASH_LED); printf("I'm done.\n"); } /* * Flash (turn on and then off, with a half-second delay between those actions) * the center LED the given number of times. */ void flash_led(int n) { int k; for (k = 0; k < n; ++k) { create_play_led(1); sleep(0.25); // 1/4 of a second create_play_led(0); sleep(0.25); beep_once(); } } /* * Beep one time. */ void beep_once() { int song = BEEP_SONG; gc_song_array[song][0] = 1; gc_song_array[song][1] = BEEP_NOTE; // Note (must be between 31 and xxx) gc_song_array[song][2] = BEEP_DURATION; // Duration (number of 1/64th's of a second) create_load_song(song); create_play_song(song); sleep(BEEP_DURATION / 64.0); // Wait for the note-playing to finish. } // TODO 1: // Write a function called play_notes() that plays a single note for half a second. Call it from main. // -- Notes can be between 31 and 127, inclusive. // -- Durations are in units of 1/64 of a second (so playing a note for 32 units is half a second). // -- To play one note in song 0: // -- gc_song_array[0][0] = 1; // song 0 has 1 note // -- gc_song_array[0][1] = your note; // integer between 31 and 127, inclusive // -- gc_song_array[0][1] = your duration; // integer that tells how many 64th's of a second you want as duration // -- Call create_load_song sending it 0 (for song 0). // -- Call create_play_song sending it 0 (for song 0). // TODO 2: // Modify your play_notes function so that it takes the note and duration as parameters. Test it. // TODO 3: // Modify the body of your play_notes function so that it plays 8 notes that: // -- start at the given note and // -- increase as you see fit (e.g., notes could increase by 3 through the loop). // If you wish, also have the durations change inside the loop.