void initialize() { play_from_program_space(welcome); #ifdef DEBUG // start receiving data at 9600 baud. serial_set_baud_rate(9600); serial_receive_ring(buffer, 100); #endif // initialize your QTR sensors // unsigned char qtr_rc_pins[] = {IO_C0, IO_C1, IO_C2}; // unsigned char qtr_rc_pins[] = {IO_C0, IO_C1, IO_C2, IO_C3, IO_C4, IO_C5, IO_D7, IO_D4}; unsigned char qtr_rc_pins[] = {IO_D4, IO_D7, IO_C5, IO_C4, IO_C3, IO_C2, IO_C1, IO_C0}; qtr_rc_init(qtr_rc_pins, 8, 2000, IO_D2); // 800 us timeout, emitter pin PD2 #ifdef DEBUG serial_send_blocking("Press Button A to start calibrating...\n", 39); #endif wait_for_button_press(BUTTON_A); // Always wait for the button to be released so that the robot doesn't // start moving until your hand is away from it. wait_for_button_release(BUTTON_A); delay_ms(800); // then start calibration phase and move the sensors over both // reflectance extremes they will encounter in your application: // We use a value of 2000 for the timeout, which // corresponds to 2000*0.4 us = 0.8 ms on our 20 MHz processor. unsigned int counter; // used as a simple timer for(counter = 0; counter < 82; counter++) { if(counter < 20 || counter >= 60) set_motors(60,-60); else set_motors(-60,60); qtr_calibrate(QTR_EMITTERS_ON); // Since our counter runs to 80, the total delay will be // 80*20 = 1600 ms. delay_ms(20); } set_motors(0,0); #ifdef DEBUG serial_send_blocking("Press Button A to start line following...\n", 42); #endif wait_for_button_press(BUTTON_A); wait_for_button_release(BUTTON_A); }
int main() { lcd_load_custom_character(happy, 0); lcd_load_custom_character(sad, 1); lcd_load_custom_character(indifferent, 2); lcd_load_custom_character(surprised, 3); lcd_load_custom_character(mocking, 4); clear(); // this must be called before we can use the custom characters print("mood: ?"); // initialize the random number generator based on how long we hold the button the first time wait_for_button_press(ANY_BUTTON); long seed = 0; while(button_is_pressed(ANY_BUTTON)) seed++; srandom(seed); while(1) { lcd_goto_xy(6, 0); // move cursor to the correct position char mood; do { mood = random()%5; } while (mood == prevMood); // ensure we get a new mood that differs from the previous prevMood = mood; print_character(mood); // print a random mood character wait_for_button(ANY_BUTTON); // wait for any button to be pressed } }
int main( void ) { /* perform battery check */ bat_check(); /* display welcome message and */ /* seed random number generator */ clear(); lcd_goto_xy(0,0); print("Welcome!"); lcd_goto_xy(0,1); print("Press B"); wait_for_button_press(BUTTON_B); /* button down */ long seed = 0; while(button_is_pressed(BUTTON_B)) /* while button not released */ seed++; srandom(seed); while(1) { clear(); /* obtain random number between 0-9 */ int val = random() % 10; /* display number */ lcd_goto_xy(0,0); print_long(val); lcd_goto_xy(0,1); print("Press B"); /* wait for user to press/release B */ wait_for_button(BUTTON_B); } }
//Calibrates the sensor void calibrate(unsigned int *sensors, unsigned int *minv, unsigned int *maxv) { //say something to the user clear(); lcd_goto_xy(0, 0); print(" Fluffy"); lcd_goto_xy(0, 1); print("A=Go!"); //wait on the calibration button wait_for_button_press(BUTTON_A); //wait for the user to move his hand delay_ms(500); //activate the motors set_motors(40, -40); //take 165 readings from the sensors...why not? int i; for (i = 0; i < 165; i++) { read_line_sensors(sensors, IR_EMITTERS_ON); update_bounds(sensors, minv, maxv); delay_ms(10); } //and turn the motors off, we're done set_motors(0, 0); delay_ms(750); }
// waits for a button, plays the appropriate beep, and returns the // button or buttons that were pressed char wait_for_button_and_beep() { char button = wait_for_button_press(ANY_BUTTON); if(button & BUTTON_A) play_from_program_space(beep_button_a); else if(button & BUTTON_B) play_from_program_space(beep_button_b); else play_from_program_space(beep_button_c); wait_for_button_release(button); return button; }
int main() { while(1) { clear(); print("Waiting"); // wait for either the top or bottom buttons to be pressed // store the value of the pressed button in the variable 'button' unsigned char button = wait_for_button_press(TOP_BUTTON | BOTTOM_BUTTON); clear(); if (button == TOP_BUTTON) // display the button that was pressed print("top down"); else print("bot down"); wait_for_button_release(button); // wait for that button to be released clear(); print("released"); // display that the button was released delay_ms(1000); } }
void menu_select() { static int menu_index = 0; print_two_lines_delay_1s(main_menu_intro_line1,main_menu_intro_line2); while(1) { clear(); lcd_goto_xy(0,1); print_from_program_space(menu_line2); lcd_goto_xy(0,0); print_from_program_space(main_menu_options[menu_index]); lcd_show_cursor(CURSOR_BLINKING); // the cursor will be blinking at the end of the option name // wait for all buttons to be released, then a press while(button_is_pressed(ANY_BUTTON)); char button = wait_for_button_press(ANY_BUTTON); if(button & BUTTON_A) { play_from_program_space(beep_button_a); menu_index --; } else if(button & BUTTON_B) { lcd_hide_cursor(); clear(); play_from_program_space(beep_button_b); wait_for_button_release(button); while(!button_is_pressed(BUTTON_B)) { lcd_goto_xy(0,1); print_from_program_space(back_line2); lcd_goto_xy(0,0); main_menu_functions[menu_index](); } set_motors(0,0); stop_playing(); m1_speed = 0; m2_speed = 0; red_led(0); green_led(0); play_from_program_space(beep_button_b); return; } else if(button & BUTTON_C) { play_from_program_space(beep_button_c); menu_index ++; } if(menu_index < 0) menu_index = main_menu_length-1; if(menu_index >= main_menu_length) menu_index = 0; } }