All white space changes. Moved hardware versions to header file.

I corrected all the /* style comments to // comments to fit with
SparkFun style guides. Now works using the Arduino auto-format function.
This commit is contained in:
Nathan Seidle 2012-12-26 16:14:22 -07:00
parent 1c89338d1b
commit b26205e706
2 changed files with 362 additions and 377 deletions

View File

@ -39,148 +39,16 @@
The current version of Simon uses the ATmega328. The external osciallator The current version of Simon uses the ATmega328. The external osciallator
was removed to reduce component count. This version of simon relies on the was removed to reduce component count. This version of simon relies on the
internal default 1MHz osciallator. Do not set the external fuses. internal default 1MHz osciallator. Do not set the external fuses.
*/ */
#include <avr/io.h> #include "hardware_versions.h"
#include <avr/interrupt.h>
/* Uncomment one of the following, corresponding to the board you have. */
//#define BOARD_REV_6_25_08
// #define BOARD_REV_4_9_2009
#define BOARD_REV_PTH
#ifdef BOARD_REV_PTH
#define CHIP_ATMEGA168
#define LED_RED (1 << 0)
#define LED_GREEN (1 << 1)
#define LED_BLUE (1 << 2)
#define LED_YELLOW (1 << 3)
/* LED pin definitions */
#define LED_RED_PIN 2
#define LED_RED_PORT PORTB
#define LED_GREEN_PIN 3
#define LED_GREEN_PORT PORTD
#define LED_BLUE_PIN 5
#define LED_BLUE_PORT PORTB
#define LED_YELLOW_PIN 5
#define LED_YELLOW_PORT PORTD
/* Button pin definitions */
#define BUTTON_RED_PIN 1
#define BUTTON_RED_PORT PINB
#define BUTTON_GREEN_PIN 2
#define BUTTON_GREEN_PORT PIND
#define BUTTON_BLUE_PIN 4
#define BUTTON_BLUE_PORT PINB
#define BUTTON_YELLOW_PIN 6
#define BUTTON_YELLOW_PORT PIND
/* Buzzer pin definitions */
#define BUZZER1 4
#define BUZZER1_PORT PORTD
#define BUZZER2 7
#define BUZZER2_PORT PORTD
#endif /* BOARD_REV_PTH */
#ifdef BOARD_REV_6_25_08
#define CHIP_ATMEGA168
#define LED_RED (1 << 0)
#define LED_GREEN (1 << 1)
#define LED_BLUE (1 << 2)
#define LED_YELLOW (1 << 3)
/* LED pin definitions */
#define LED_RED_PIN 3
#define LED_RED_PORT PORTC
#define LED_GREEN_PIN 2
#define LED_GREEN_PORT PORTD
#define LED_BLUE_PIN 0
#define LED_BLUE_PORT PORTC
#define LED_YELLOW_PIN 5
#define LED_YELLOW_PORT PORTD
/* Button pin definitions */
#define BUTTON_RED_PIN 2
#define BUTTON_RED_PORT PINC
#define BUTTON_GREEN_PIN 5
#define BUTTON_GREEN_PORT PINC
#define BUTTON_BLUE_PIN 1
#define BUTTON_BLUE_PORT PINC
#define BUTTON_YELLOW_PIN 6
#define BUTTON_YELLOW_PORT PIND
/* Buzzer pin definitions */
#define BUZZER1 3
#define BUZZER1_PORT PORTD
#define BUZZER2 4
#define BUZZER2_PORT PORTD
#endif /* BOARD_REV_6_25_08 */
#ifdef BOARD_REV_4_9_2009
#define LED_RED (1 << 0)
#define LED_GREEN (1 << 1)
#define LED_BLUE (1 << 2)
#define LED_YELLOW (1 << 3)
#define CHIP_ATMEGA168
/* LED pin definitions */
#define LED_BLUE_PIN 5
#define LED_BLUE_PORT PORTB
#define LED_YELLOW_PIN 5
#define LED_YELLOW_PORT PORTD
#define LED_RED_PIN 2
#define LED_RED_PORT PORTB
#define LED_GREEN_PIN 2
#define LED_GREEN_PORT PORTD
/* Button pin definitions */
#define BUTTON_RED_PIN 0
#define BUTTON_RED_PORT PINB
#define BUTTON_GREEN_PIN 1
#define BUTTON_GREEN_PORT PINB
#define BUTTON_BLUE_PIN 7
#define BUTTON_BLUE_PORT PIND
#define BUTTON_YELLOW_PIN 6
#define BUTTON_YELLOW_PORT PIND
/* Buzzer pin definitions */
#define BUZZER1 3
#define BUZZER1_PORT PORTD
#define BUZZER2 4
#define BUZZER2_PORT PORTD
#endif /* BOARD_REV_4_9_2009 */
/* Define game parameters */
// Define game parameters
#define MOVES_TO_WIN 13 #define MOVES_TO_WIN 13
#define TIME_LIMIT 3000 /* 3000ms = 3 sec */ #define TIME_LIMIT 3000 //3000ms = 3 sec
#define sbi(port_name, pin_number) (port_name |= 1<<pin_number) #define sbi(port_name, pin_number) (port_name |= 1<<pin_number)
#define cbi(port_name, pin_number) \ #define cbi(port_name, pin_number) ((port_name) &= (uint8_t)~(1 << pin_number))
((port_name) &= (uint8_t)~(1 << pin_number))
/* Declarations for static functions */
void delay_us(uint16_t delay);
void delay_ms(uint16_t delay);
uint8_t check_button(void);
void set_leds(uint8_t leds);
void buzz_sound(uint16_t buzz_length_ms, uint16_t buzz_delay);
void toner(uint8_t tone, uint16_t buzz_length_ms);
void add_to_moves(void);
void play_moves(void);
void play_loser(void);
void play_winner(void);
void ioinit(void);
int battle = 0; int battle = 0;
@ -189,60 +57,52 @@ int counter = 0; // for cycling through the LEDs during the beegees loop
int count = 20; // for keeping rhythm straight in the beegees loop int count = 20; // for keeping rhythm straight in the beegees loop
////////////// //////////////
/* Game state */ // Game state variables
uint8_t moves[32]; uint8_t moves[32];
uint8_t nmoves = 0; uint8_t nmoves = 0;
//Timer 2 overflow ISR
ISR (SIG_OVERFLOW2) ISR (SIG_OVERFLOW2)
{ {
/* // Prescalar of 1024, Clock = 16MHz, 15,625 clicks per second, 64us per click
* Prescalar of 1024
* Clock = 16MHz
* 15,625 clicks per second
* 64us per click
*/
/* Preload timer 2 for 125 clicks. Should be 8ms per ISR call */ // Preload timer 2 for 125 clicks. Should be 8ms per ISR call
TCNT2 = 131; /* 256 - 125 = 131 */ TCNT2 = 131; //256 - 125 = 131
} }
/* General short delays, using internal timer do a fairly accurate 1us */ //General short delays, using internal timer do a fairly accurate 1us
#ifdef CHIP_ATMEGA168 #ifdef CHIP_ATMEGA168
void delay_us(uint16_t delay) void delay_us(uint16_t delay)
{ {
while (delay > 256) while (delay > 256)
{ {
TIFR0 = (1<<TOV0); /* Clear any interrupt flags on Timer0 */ TIFR0 = (1<<TOV0); // Clear any interrupt flags on Timer0
TCNT0 = 0; TCNT0 = 0;
while ( (TIFR0 & (1<<TOV0)) == 0); while ( (TIFR0 & (1<<TOV0)) == 0);
delay -= 256; delay -= 256;
} }
TIFR0 = (1<<TOV0); /* Clear any interrupt flags on Timer0 */ TIFR0 = (1<<TOV0); // Clear any interrupt flags on Timer0
/*
* 256 - 125 = 131 : Preload timer 0 for x clicks. // 256 - 125 = 131 : Preload timer 0 for x clicks. Should be 1us per click
* Should be 1us per click
*/
TCNT0 = 256 - delay; TCNT0 = 256 - delay;
while ((TIFR0 & (1<<TOV0)) == 0) { while ((TIFR0 & (1<<TOV0)) == 0) {
/* do nothing */ // Do nothing
} }
} }
#endif #endif
/* General short delays */ //General short delays
void void delay_ms(uint16_t x)
delay_ms(uint16_t x)
{ {
while (x-- > 0) { while (x-- > 0) {
delay_us(1000); delay_us(1000);
} }
} }
/* Light the given set of LEDs */ //Light the given set of LEDs
void void set_leds(uint8_t leds)
set_leds(uint8_t leds)
{ {
if ((leds & LED_RED) != 0) { if ((leds & LED_RED) != 0) {
sbi(LED_RED_PORT, LED_RED_PIN); sbi(LED_RED_PORT, LED_RED_PIN);
@ -272,70 +132,67 @@ set_leds(uint8_t leds)
#ifdef BOARD_REV_6_25_08 #ifdef BOARD_REV_6_25_08
void void init_gpio(void)
init_gpio(void)
{ {
/* 1 = output, 0 = input */ // 1 = output, 0 = input
DDRB = 0b11111111; DDRB = 0b11111111;
DDRC = 0b00001001; /* LEDs and Buttons */ DDRC = 0b00001001; // LEDs and Buttons
DDRD = 0b00111110; /* LEDs, buttons, buzzer, TX/RX */ DDRD = 0b00111110; // LEDs, buttons, buzzer, TX/RX
PORTC = 0b00100110; /* Enable pull-ups on buttons 0,2,3 */ PORTC = 0b00100110; // Enable pull-ups on buttons 0, 2, 3
PORTD = 0b01000000; /* Enable pull-up on button 1 */ PORTD = 0b01000000; // Enable pull-up on button 1
} }
#endif /* BOARD_REV_6_25_08 */ #endif // End BOARD_REV_6_25_08
#ifdef BOARD_REV_4_9_2009 #ifdef BOARD_REV_4_9_2009
void void init_gpio(void)
init_gpio(void)
{ {
/* 1 = output, 0 = input */ // 1 = output, 0 = input
DDRB = 0b11111100; /* button 2,3 on PB0,1 */ DDRB = 0b11111100; // Button 2,3 on PB0,1
DDRD = 0b00111110; /* LEDs, buttons, buzzer, TX/RX */ DDRD = 0b00111110; // LEDs, buttons, buzzer, TX/RX
PORTB = 0b00000011; /* Enable pull-ups on buttons 2,3 */ PORTB = 0b00000011; // Enable pull-ups on buttons 2, 3
PORTD = 0b11000000; /* Enable pull-up on button 0,1 */ PORTD = 0b11000000; // Enable pull-up on button 0, 1
} }
#endif /* BOARD_REV_4_9_2009 */ #endif // End BOARD_REV_4_9_2009
#ifdef BOARD_REV_PTH #ifdef BOARD_REV_PTH
void void init_gpio(void)
init_gpio(void)
{ {
/* 1 = output, 0 = input */ // 1 = output, 0 = input
DDRB = 0b11101101; /* LEDs and Buttons */ DDRB = 0b11101101; // LEDs and Buttons
DDRC = 0b11111111; /* LEDs and Buttons */ DDRC = 0b11111111; // LEDs and Buttons
DDRD = 0b10111011; /* LEDs, buttons, buzzer, TX/RX */ DDRD = 0b10111011; // LEDs, buttons, buzzer, TX/RX
PORTB = 0b00010010; /* Enable pull-ups on buttons 1,4 */ PORTB = 0b00010010; // Enable pull-ups on buttons 1, 4
//PORTC = 0b00100110; /* Enable pull-ups on buttons 0,2,3 */ //PORTC = 0b00100110; // Enable pull-ups on buttons 0, 2, 3
PORTD = 0b01000100; /* Enable pull-up on button 1 */ PORTD = 0b01000100; // Enable pull-up on button 1
} }
#endif #endif
void void ioinit(void)
ioinit(void)
{ {
init_gpio(); init_gpio();
//Set Timer 0 Registers to Default Setting to over-ride the timer initialization made in the init() function of the Arduino Wiring libary (Wiring.c in the hardware/core/arduino folder) //Set Timer 0 Registers to Default Setting to over-ride the timer initialization made in the init() function of the \
//Arduino Wiring library (Wiring.c in the hardware/core/arduino folder)
TCCR0A = 0; TCCR0A = 0;
TIMSK0 = 0; TIMSK0 = 0;
/* Init timer 0 for delay_us timing (1,000,000 / 1 = 1,000,000) */ // Init timer 0 for delay_us timing (1,000,000 / 1 = 1,000,000)
//TCCR0B = (1<<CS00); /* Set Prescaler to 1. CS00=1 */ //TCCR0B = (1<<CS00); // Set Prescaler to 1. CS00=1
TCCR0B = (1<<CS01); /* Set Prescaler to 1. CS00=1 */ TCCR0B = (1<<CS01); // Set Prescaler to 1. CS00=1
/* Init timer 2 */ // Init timer 2
ASSR = 0; ASSR = 0;
/* Set Prescaler to 1024. CS22=1, CS21=1,CS20=1 */ // Set Prescaler to 1024. CS22=1, CS21=1, CS20=1
TCCR2B = (1<<CS22)|(1<<CS21)|(1<<CS20); TCCR2B = (1<<CS22)|(1<<CS21)|(1<<CS20);
TIMSK2 = (1<<TOIE2); /* Enable Timer 2 Interrupt */ TIMSK2 = (1<<TOIE2); // Enable Timer 2 Interrupt
cli(); //We don't use any interrupt functionality. Let's turn it off so Arduino doesn't screw around with it! cli(); //We don't use any interrupt functionality. Let's turn it off so Arduino doesn't screw around with it!
} }
/* Returns a '1' bit in the position corresponding to LED_RED, etc. */ // Returns a '1' bit in the position corresponding to LED_RED, etc.
uint8_t uint8_t check_button(void)
check_button(void)
{ {
uint8_t button_pressed = 0; uint8_t button_pressed = 0;
@ -351,9 +208,8 @@ check_button(void)
return button_pressed; return button_pressed;
} }
/* Play the loser sound/lights */ // Play the loser sound/lights
void void play_loser(void)
play_loser(void)
{ {
set_leds(LED_RED|LED_GREEN); set_leds(LED_RED|LED_GREEN);
buzz_sound(255, 1500); buzz_sound(255, 1500);
@ -368,13 +224,12 @@ play_loser(void)
buzz_sound(255, 1500); buzz_sound(255, 1500);
} }
/* Play the winner sound */ // Play the winner sound
void void winner_sound(void)
winner_sound(void)
{ {
uint8_t x, y; uint8_t x, y;
/* Toggle the buzzer at various speeds */ // Toggle the buzzer at various speeds
for (x = 250; x > 70; x--) { for (x = 250; x > 70; x--) {
for (y = 0; y < 3; y++) { for (y = 0; y < 3; y++) {
sbi(BUZZER2_PORT, BUZZER2); sbi(BUZZER2_PORT, BUZZER2);
@ -390,7 +245,7 @@ winner_sound(void)
} }
} }
/* Play the winner sound and lights */ // Play the winner sound and lights
void play_winner(void) void play_winner(void)
{ {
set_leds(LED_GREEN|LED_BLUE); set_leds(LED_GREEN|LED_BLUE);
@ -403,7 +258,7 @@ void play_winner(void)
winner_sound(); winner_sound();
} }
/* Plays the current contents of the game moves */ // Plays the current contents of the game moves
void play_moves(void) void play_moves(void)
{ {
uint8_t move; uint8_t move;
@ -414,23 +269,23 @@ void play_moves(void)
} }
} }
/* Adds a new random button to the game sequence, by sampling the timer */ // Adds a new random button to the game sequence, by sampling the timer
void add_to_moves(void) void add_to_moves(void)
{ {
uint8_t new_button; uint8_t new_button;
/* Use the lower 2 bits of the timer for the random value */ // Use the lower 2 bits of the timer for the random value
new_button = 1 << (TCNT2 & 0x3); new_button = 1 << (TCNT2 & 0x3);
moves[nmoves++] = new_button; moves[nmoves++] = new_button;
} }
/* Adds a user defined button to the game sequence, by waiting for their input */ // Adds a user defined button to the game sequence, by waiting for their input
void add_to_moves_battle(void) void add_to_moves_battle(void)
{ {
uint8_t new_button; uint8_t new_button;
/* wait for user to input next move */ // wait for user to input next move
new_button = wait_for_button(); new_button = wait_for_button();
toner(new_button, 150); toner(new_button, 150);
@ -438,7 +293,7 @@ void add_to_moves_battle(void)
moves[nmoves++] = new_button; moves[nmoves++] = new_button;
} }
/* Toggle buzzer every buzz_delay_us, for a duration of buzz_length_ms. */ // Toggle buzzer every buzz_delay_us, for a duration of buzz_length_ms.
void buzz_sound(uint16_t buzz_length_ms, uint16_t buzz_delay_us) void buzz_sound(uint16_t buzz_length_ms, uint16_t buzz_delay_us)
{ {
uint32_t buzz_length_us; uint32_t buzz_length_us;
@ -447,7 +302,7 @@ void buzz_sound(uint16_t buzz_length_ms, uint16_t buzz_delay_us)
while (buzz_length_us > buzz_delay_us*2) { while (buzz_length_us > buzz_delay_us*2) {
buzz_length_us -= buzz_delay_us*2; buzz_length_us -= buzz_delay_us*2;
/* toggle the buzzer at various speeds */ // Toggle the buzzer at various speeds
cbi(BUZZER1_PORT, BUZZER1); cbi(BUZZER1_PORT, BUZZER1);
sbi(BUZZER2_PORT, BUZZER2); sbi(BUZZER2_PORT, BUZZER2);
delay_us(buzz_delay_us); delay_us(buzz_delay_us);
@ -459,12 +314,11 @@ void buzz_sound(uint16_t buzz_length_ms, uint16_t buzz_delay_us)
} }
/* /*
* Light an LED and play tone Light an LED and play tone
* red, upper left: 440Hz - 2.272ms - 1.136ms pulse
* red, upper left: 440Hz - 2.272ms - 1.136ms pulse green, upper right: 880Hz - 1.136ms - 0.568ms pulse
* green, upper right: 880Hz - 1.136ms - 0.568ms pulse blue, lower left: 587.33Hz - 1.702ms - 0.851ms pulse
* blue, lower left: 587.33Hz - 1.702ms - 0.851ms pulse yellow, lower right: 784Hz - 1.276ms - 0.638ms pulse
* yellow, lower right: 784Hz - 1.276ms - 0.638ms pulse
*/ */
void toner(uint8_t which, uint16_t buzz_length_ms) void toner(uint8_t which, uint16_t buzz_length_ms)
{ {
@ -487,11 +341,11 @@ void toner(uint8_t which, uint16_t buzz_length_ms)
break; break;
} }
/* Turn off all LEDs */ // Turn off all LEDs
set_leds(0); set_leds(0);
} }
/* Show an "attract mode" display while waiting for user to press button. */ // Show an "attract mode" display while waiting for user to press button.
void attract_mode(void) void attract_mode(void)
{ {
while (1) { while (1) {
@ -517,9 +371,8 @@ void attract_mode(void)
} }
} }
// Wait for a button to be pressed.
/* Wait for a button to be pressed. Returns one of led colors (LED_RED, etc.) // Returns one of led colors (LED_RED, etc.) if successful, 0 if timed out
* if successful, 0 if timed out */
uint8_t wait_for_button(void) uint8_t wait_for_button(void)
{ {
uint16_t time_limit = TIME_LIMIT; uint16_t time_limit = TIME_LIMIT;
@ -529,18 +382,15 @@ uint8_t wait_for_button(void)
while (time_limit > 0) { while (time_limit > 0) {
uint8_t button; uint8_t button;
/* Implement a small bit of debouncing */ // Implement a small bit of debouncing
old_button = button; old_button = button;
button = check_button(); button = check_button();
/* // Make sure we've seen the previous button released before accepting new buttons
* Make sure we've seen the previous button
* released before accepting new buttons
*/
if (button == 0) if (button == 0)
released = 1; released = 1;
if (button == old_button && released == 1) { if (button == old_button && released == 1) {
/* Make sure just one button is pressed */ // Make sure just one button is pressed
if (button == LED_RED || if (button == LED_RED ||
button == LED_BLUE || button == LED_BLUE ||
button == LED_GREEN || button == LED_GREEN ||
@ -553,49 +403,60 @@ uint8_t wait_for_button(void)
time_limit--; time_limit--;
} }
return 0; /* Timed out */ return 0; //Timed out
} }
// Play the game. Returns 0 if player loses, or 1 if player wins.
/* Play the game. Returns 0 if player loses, or 1 if player wins. */
int game_mode(void) int game_mode(void)
{ {
nmoves = 0; nmoves = 0;
int moves_to_win_var = MOVES_TO_WIN; // If in normal mode, then allow the user to win after a #define varialb up top (default is 13). int moves_to_win_var = MOVES_TO_WIN; // If in normal mode, then allow the user to win after a #define varialb up top (default is 13).
if(battle) moves_to_win_var = 1000; // If in battle mode, allow the users to go up to 1000 moves! Like anyone could possibly do that :) if(battle) moves_to_win_var = 1000; // If in battle mode, allow the users to go up to 1000 moves! Like anyone could possibly do that :)
while (nmoves < moves_to_win_var) {
while (nmoves < moves_to_win_var)
{
uint8_t move; uint8_t move;
/* Add a button to the current moves, then play them back */ // Add a button to the current moves, then play them back
if(battle) add_to_moves_battle(); // If in battle mode, then listen for user input to choose the next step if(battle)
else add_to_moves(); add_to_moves_battle(); // If in battle mode, then listen for user input to choose the next step
else
add_to_moves();
if(battle) ; // If in battle mode, then don't play back the pattern, it's up the the users to remember it - then add on a move. if(battle)
else play_moves(); ; // If in battle mode, then don't play back the pattern, it's up the the users to remember it - then add on a move.
else
play_moves();
/* Then require the player to repeat the sequence. */ // Then require the player to repeat the sequence.
for (move = 0; move < nmoves; move++) { for (move = 0; move < nmoves; move++) {
uint8_t choice = wait_for_button(); uint8_t choice = wait_for_button();
/* If wait timed out, player loses. */ // If wait timed out, player loses.
if (choice == 0) if (choice == 0)
return 0; return 0;
toner(choice, 150); toner(choice, 150);
/* If the choice is incorect, player loses. */ // If the choice is incorect, player loses.
if (choice != moves[move]) { if (choice != moves[move]) {
return 0; return 0;
} }
} }
/* Player was correct, delay before playing moves */ // Player was correct, delay before playing moves
if(battle) delay_ms(100); // reduced wait time, because we want to allow the battle to go very fast! plus, if you use the delay(1000), then it may miss capturing the users next input. if(battle)
else delay_ms(1000); {
//reduced wait time, because we want to allow the battle to go very fast!
//plus, if you use the delay(1000), then it may miss capturing the users next input.
delay_ms(100);
}
else
delay_ms(1000);
} }
/* player wins */ // Player wins!
return 1; return 1;
} }
@ -607,10 +468,10 @@ void setup()
void loop() void loop()
{ {
/* Setup IO pins and defaults */ // Setup IO pins and defaults
ioinit(); ioinit();
/* Check to see if LOWER LEFT BUTTON is pressed */ // Check to see if LOWER LEFT BUTTON is pressed
if (check_button() == LED_YELLOW){ if (check_button() == LED_YELLOW){
while(1){ while(1){
buzz(5); buzz(5);
@ -621,7 +482,7 @@ void loop()
} }
} }
/* Check to see if LOWER RIGHT BUTTON is pressed */ // Check to see if LOWER RIGHT BUTTON is pressed
if (check_button() == LED_GREEN){ if (check_button() == LED_GREEN){
while(1){ while(1){
buzz(5); buzz(5);
@ -636,78 +497,77 @@ void loop()
play_winner(); play_winner();
/* Main loop */ // Main loop
while (1) { while (1) {
/* Wait for user to start game */ // Wait for user to start game
attract_mode(); attract_mode();
/* Indicate the start of game play */ // Indicate the start of game play
set_leds(LED_RED|LED_GREEN|LED_BLUE|LED_YELLOW); set_leds(LED_RED|LED_GREEN|LED_BLUE|LED_YELLOW);
delay_ms(1000); delay_ms(1000);
set_leds(0); set_leds(0);
delay_ms(250); delay_ms(250);
/* Play game and handle result */ // Play game and handle result
if (game_mode() != 0) { if (game_mode() != 0) {
/* Player won, play winner tones */ // Player won, play winner tones
play_winner(); play_winner();
} }
else { else {
/* Player lost, play loser tones */ // Player lost, play loser tones
play_loser(); play_loser();
} }
} }
} }
//
void beegees_loop()
////////////////////////////////////////////////////////////////////////////////////// {
void beegees_loop(){ buzz(3);
buzz(3); delay(400);
delay(400); buzz(4);
buzz(4); rest(1);
rest(1); delay(600);
delay(600); buzz(5);
buzz(5); rest(1);
rest(1); rest(1);
rest(1); delay(400);
delay(400); buzz(3);
buzz(3); rest(1);
rest(1); rest(1);
rest(1); rest(1);
rest(1); buzz(2);
buzz(2); rest(1);
rest(1); buzz(1);
buzz(1); buzz(2);
buzz(2); buzz(3);
buzz(3); rest(1);
rest(1); buzz(1);
buzz(1); buzz(2);
buzz(2); rest(1);
rest(1); buzz(3);
buzz(3); rest(1);
rest(1); rest(1);
rest(1); buzz(1);
buzz(1); rest(1);
rest(1); buzz(2);
buzz(2); rest(1);
rest(1); buzz(3);
buzz(3); rest(1);
rest(1); buzz(4);
buzz(4); rest(1);
rest(1); buzz(5);
buzz(5); rest(1);
rest(1); delay(700);
delay(700);
} }
////////////////////////////////////////////////////////////////////////////////////// //
void buzz(int tone){ void buzz(int tone){
///declare an integer, "freq", for frequency of the note to be played. //Declare an integer, "freq", for frequency of the note to be played.
int freq; int freq;
///5 different tones to select. Each tone is a different frequency. //5 different tones to select. Each tone is a different frequency.
if(tone == 1){ if(tone == 1){
freq = 2000; freq = 2000;
} }
@ -726,21 +586,21 @@ void buzz(int tone){
//freq = (freq/2); //freq = (freq/2);
/// Because frequency is determined by the wavelength (the time HIGH and the time LOW), // Because frequency is determined by the wavelength (the time HIGH and the time LOW),
/// you need to have "count" in order to keep a note the same length in time. // you need to have "count" in order to keep a note the same length in time.
/// "count" is the number of times this function will repeat the HIGH/LOW pattern - to create the sound of the note. // "count" is the number of times this function will repeat the HIGH/LOW pattern - to create the sound of the note.
count = 40; count = 40;
/// In order to keep all 5 notes the same length in time, you must compare them to the longest note (tonic) - aka the "1" note. // In order to keep all 5 notes the same length in time, you must compare them to the longest note (tonic) - aka the "1" note.
count = count*(2000/freq); count = count*(2000/freq);
/// this next function simply changes the next LED to turn on. // this next function simply changes the next LED to turn on.
change_led(); change_led();
/// this next for loop actually makes the buzzer pin move. // this next for loop actually makes the buzzer pin move.
/// it uses the "count" variable to know how many times to play the frequency. // it uses the "count" variable to know how many times to play the frequency.
/// -this keeps the timing correct. // -this keeps the timing correct.
for(int i = 0; i < count; i++){ for(int i = 0; i < count; i++){
digitalWrite(BUZZER1, HIGH); digitalWrite(BUZZER1, HIGH);
digitalWrite(BUZZER2, LOW); digitalWrite(BUZZER2, LOW);
@ -752,8 +612,8 @@ void buzz(int tone){
delay(60); delay(60);
} }
////////////////////////////////////////////////////////////////////////////////////// //
void rest(int tone){ void rest(int tone){
int freq; int freq;
if(tone == 1){ if(tone == 1){
freq = 2000; freq = 2000;
@ -773,8 +633,10 @@ void buzz(int tone){
//freq = (freq/2); //freq = (freq/2);
count = 40; count = 40;
count = count*(2000/freq); count = count*(2000/freq);
//change_led(); //change_led();
for(int i = 0; i < count; i++){
for(int i = 0 ; i < count ; i++)
{
digitalWrite(BUZZER1, LOW); digitalWrite(BUZZER1, LOW);
delayMicroseconds(freq); delayMicroseconds(freq);
digitalWrite(BUZZER1, LOW); digitalWrite(BUZZER1, LOW);
@ -783,9 +645,11 @@ void buzz(int tone){
delay(75); delay(75);
} }
////////////////////////////////////////////////////////////////////////////////////// //
void change_led(){ void change_led()
if(counter > 3){ {
if(counter > 3)
{
counter = 0; counter = 0;
} }
set_leds(1 << counter); set_leds(1 << counter);
@ -794,6 +658,3 @@ void change_led(){

View File

@ -0,0 +1,124 @@
/*
Started: 12-26-2012
Spark Fun Electronics
The SparkFun Simon Says game has been through dozens of revisions over the years. This header
file attempts to map and support all the different hardware versions.
*/
// Uncomment one of the following, corresponding to the board you have.
//#define BOARD_REV_6_25_08
//#define BOARD_REV_4_9_2009
#define BOARD_REV_PTH
#ifdef BOARD_REV_PTH
#define CHIP_ATMEGA168
#define LED_RED (1 << 0)
#define LED_GREEN (1 << 1)
#define LED_BLUE (1 << 2)
#define LED_YELLOW (1 << 3)
/* LED pin definitions */
#define LED_RED_PIN 2
#define LED_RED_PORT PORTB
#define LED_GREEN_PIN 3
#define LED_GREEN_PORT PORTD
#define LED_BLUE_PIN 5
#define LED_BLUE_PORT PORTB
#define LED_YELLOW_PIN 5
#define LED_YELLOW_PORT PORTD
/* Button pin definitions */
#define BUTTON_RED_PIN 1
#define BUTTON_RED_PORT PINB
#define BUTTON_GREEN_PIN 2
#define BUTTON_GREEN_PORT PIND
#define BUTTON_BLUE_PIN 4
#define BUTTON_BLUE_PORT PINB
#define BUTTON_YELLOW_PIN 6
#define BUTTON_YELLOW_PORT PIND
/* Buzzer pin definitions */
#define BUZZER1 4
#define BUZZER1_PORT PORTD
#define BUZZER2 7
#define BUZZER2_PORT PORTD
#endif /* BOARD_REV_PTH */
#ifdef BOARD_REV_6_25_08
#define CHIP_ATMEGA168
#define LED_RED (1 << 0)
#define LED_GREEN (1 << 1)
#define LED_BLUE (1 << 2)
#define LED_YELLOW (1 << 3)
/* LED pin definitions */
#define LED_RED_PIN 3
#define LED_RED_PORT PORTC
#define LED_GREEN_PIN 2
#define LED_GREEN_PORT PORTD
#define LED_BLUE_PIN 0
#define LED_BLUE_PORT PORTC
#define LED_YELLOW_PIN 5
#define LED_YELLOW_PORT PORTD
/* Button pin definitions */
#define BUTTON_RED_PIN 2
#define BUTTON_RED_PORT PINC
#define BUTTON_GREEN_PIN 5
#define BUTTON_GREEN_PORT PINC
#define BUTTON_BLUE_PIN 1
#define BUTTON_BLUE_PORT PINC
#define BUTTON_YELLOW_PIN 6
#define BUTTON_YELLOW_PORT PIND
/* Buzzer pin definitions */
#define BUZZER1 3
#define BUZZER1_PORT PORTD
#define BUZZER2 4
#define BUZZER2_PORT PORTD
#endif /* BOARD_REV_6_25_08 */
#ifdef BOARD_REV_4_9_2009
#define LED_RED (1 << 0)
#define LED_GREEN (1 << 1)
#define LED_BLUE (1 << 2)
#define LED_YELLOW (1 << 3)
#define CHIP_ATMEGA168
/* LED pin definitions */
#define LED_BLUE_PIN 5
#define LED_BLUE_PORT PORTB
#define LED_YELLOW_PIN 5
#define LED_YELLOW_PORT PORTD
#define LED_RED_PIN 2
#define LED_RED_PORT PORTB
#define LED_GREEN_PIN 2
#define LED_GREEN_PORT PORTD
/* Button pin definitions */
#define BUTTON_RED_PIN 0
#define BUTTON_RED_PORT PINB
#define BUTTON_GREEN_PIN 1
#define BUTTON_GREEN_PORT PINB
#define BUTTON_BLUE_PIN 7
#define BUTTON_BLUE_PORT PIND
#define BUTTON_YELLOW_PIN 6
#define BUTTON_YELLOW_PORT PIND
/* Buzzer pin definitions */
#define BUZZER1 3
#define BUZZER1_PORT PORTD
#define BUZZER2 4
#define BUZZER2_PORT PORTD
#endif /* BOARD_REV_4_9_2009 */