/*----------------------------------------------------------------------------- * Function name: bat_check * Description: This function checks the voltage on the batteries and * displays a message on the LCD until the user presses B. * The message on the first line cycles between the following: * Bat Chk [-> descriptive message] * xxxxmV [-> the battery voltage] * Okay/Replace [-> whether the batteries should be replaced] ----------------------------------------------------------------------------*/ void bat_check( void ) { int firstLineType = 0; /* what should be displayed on line 1 */ /* 0-19: Bat Chk */ /* 20-39: xxxxmV */ /* 40-59: Okay/Replace */ int bat = 0; /* last read battery voltage */ /* wait for user to press button B */ while(!button_is_pressed(BUTTON_B)) { /* clear the lcd */ clear(); /* FIRST LINE */ /* set lcd position to beginning of first line */ lcd_goto_xy(0,0); /* for first line, alternate between displaying: Bat Check xxxxmV Okay/Replace */ if (firstLineType < 20) { print("Bat Chk"); } else if (firstLineType < 40) { bat = read_battery_millivolts(); print_long(bat); print("mV"); } else if (firstLineType < 60) { if (bat >= 4500) { print("Okay"); /* okay */ } else { print("Replace"); /* replace */ } } firstLineType++; firstLineType = firstLineType % 60; /* SECOND LINE */ /* set lcd position to beginning of second line */ lcd_goto_xy(0,1); print("Press B"); /* small delay */ delay_ms(50); } /* once pressed, wait a little bit */ delay_ms(500); }
// Displays the battery voltage. void bat_test() { int bat = read_battery_millivolts(); print_long(bat); print("mV"); delay_ms(100); }
/** displayBattery **************************************** * Output battery level, wait for button B * @modifies lcd display has battery level output. */ void displayBattery() { int bat; const int LOW_BATTERY = 3500; const int FULL_BATTERY = 6000; double batPer; while (!button_is_pressed(BUTTON_B)) {// as long as B is not pressed bat = read_battery_millivolts();// read battery voltage batPer = (double)(bat - LOW_BATTERY)/(FULL_BATTERY-LOW_BATTERY)*100; // compute percent of full value clear(); print_long(batPer); // display value print("%"); lcd_goto_xy(0,1); print("Press B"); delay_ms(100); // wait a little bit } play_from_program_space(beep_button_b); while (button_is_pressed(BUTTON_B)) ; // wait for button up delay_ms(200); }
int main( void ) { /* main loop that runs forever */ while(1) { /* clear the lcd */ clear(); /* print name of program */ lcd_goto_xy(0,0); /* set lcd position to beginning of first line */ print("Battery"); lcd_goto_xy(0,1); /* set lcd position to beginning of second line */ print("Check"); /* delay 2 s */ delay_ms(2000); /* clear the lcd */ clear(); /* obtain battery voltage and display on lcd */ int bat = read_battery_millivolts(); lcd_goto_xy(0,0); /* set lcd position to beginning of first line */ print_long(bat); print("mV"); /* print whether voltage seems okay */ lcd_goto_xy(0,1); /* set lcd position to beginning of second line */ if (bat >= 4500) { print("Okay"); /* okay */ } else { print("Replace"); /* replace */ } /* delay 2 s */ delay_ms(2000); } }
void displayBattery() { // get the millivolt value from the 3pi, convert to floating volts value double volts = read_battery_millivolts() / 1000.0; // calculate the percent decimal voltage double percent = (volts - MIN_VOLTS) / (MAX_VOLTS - MIN_VOLTS); // convert to a percentage percent *= 100; // display the percentage on the screen clear(); print_long(percent); print("%"); lcd_goto_xy(0,1); print("Press B"); // shall we wait for B? waitForB(); }
void initialize() { // Set PC5 as an input with internal pull-up disabled DDRC5 &= ~(1<< PORTC5); //port 5 is an input PORTC &= ~(1<< PORTC5); // Play welcome music and display a message print_from_program_space(welcome_line1); lcd_goto_xy(0,1); print_from_program_space(welcome_line2); //play_from_program_space(welcome); delay_ms(1000); clear(); print_from_program_space(name_line1); lcd_goto_xy(0,1); print_from_program_space(name_line2); delay_ms(1000); // Display battery voltage and wait for button press while(!button_is_pressed(BUTTON_B)) { clear(); print_long(read_battery_millivolts()); print("mV"); lcd_goto_xy(0,1); print("Press B"); delay_ms(100); } // Always wait for the button to be released so that 3pi doesn't // start moving until your hand is away from it. wait_for_button_release(BUTTON_B); clear(); print("Go!"); // Play music and wait for it to finish before we start driving. play_from_program_space(go); while(is_playing()); }
void initialize() { //unsigned int counter; // used as a simple timer //unsigned int sensors[5]; // an array to hold sensor values // This must be called at the beginning of 3pi code, to set up the // sensors. We use a value of 2000 for the timeout, which // corresponds to 2000*0.4 us = 0.8 ms on our 20 MHz processor. pololu_3pi_init(2000); //load_custom_characters(); // load the custom characters // Play welcome music and display a message print_from_program_space(welcome_line1); lcd_goto_xy(0,1); print_from_program_space(welcome_line2); //play_from_program_space(welcome); delay_ms(1000); clear(); print_from_program_space(demo_name_line1); lcd_goto_xy(0,1); print_from_program_space(demo_name_line2); delay_ms(1000); // Display battery voltage and wait for button press while(!button_is_pressed(BUTTON_B)) { int bat = read_battery_millivolts(); clear(); print_long(bat); print("mV"); lcd_goto_xy(0,1); print("Press B"); delay_ms(100); } // Always wait for the button to be released so that 3pi doesn't // start moving until your hand is away from it. wait_for_button_release(BUTTON_B); delay_ms(1000); clear(); }
// Initializes the 3pi, displays a welcome message, calibrates, and // plays the initial music. void initialize() { unsigned int counter; // used as a simple timer unsigned int sensors[5]; // an array to hold sensor values // This must be called at the beginning of 3pi code, to set up the // sensors. We use a value of 2000 for the timeout, which // corresponds to 2000*0.4 us = 0.8 ms on our 20 MHz processor. pololu_3pi_init(2000); load_custom_characters(); // load the custom characters // Play welcome music and display a message print_from_program_space(welcome_line1); lcd_goto_xy(0,1); print_from_program_space(welcome_line2); play_from_program_space(welcome); delay_ms(1000); clear(); print_from_program_space(demo_name_line1); lcd_goto_xy(0,1); print_from_program_space(demo_name_line2); delay_ms(1000); // Display battery voltage and wait for button press while(!button_is_pressed(BUTTON_B)) { int bat = read_battery_millivolts(); clear(); print_long(bat); print("mV"); lcd_goto_xy(0,1); print("Press B"); delay_ms(100); } // Always wait for the button to be released so that 3pi doesn't // start moving until your hand is away from it. wait_for_button_release(BUTTON_B); delay_ms(1000); // Auto-calibration: turn right and left while calibrating the // sensors. for(counter=0;counter<80;counter++) { if(counter < 20 || counter >= 60) set_motors(40,-40); else set_motors(-40,40); // This function records a set of sensor readings and keeps // track of the minimum and maximum values encountered. The // IR_EMITTERS_ON argument means that the IR LEDs will be // turned on during the reading, which is usually what you // want. calibrate_line_sensors(IR_EMITTERS_ON); // Since our counter runs to 80, the total delay will be // 80*20 = 1600 ms. delay_ms(20); } set_motors(0,0); // Display calibrated values as a bar graph. while(!button_is_pressed(BUTTON_B)) { // Read the sensor values and get the position measurement. unsigned int position = read_line(sensors,IR_EMITTERS_ON); // Display the position measurement, which will go from 0 // (when the leftmost sensor is over the line) to 4000 (when // the rightmost sensor is over the line) on the 3pi, along // with a bar graph of the sensor readings. This allows you // to make sure the robot is ready to go. clear(); print_long(position); lcd_goto_xy(0,1); display_readings(sensors); delay_ms(100); } wait_for_button_release(BUTTON_B); clear(); print("Go!"); // Play music and wait for it to finish before we start driving. play_from_program_space(go); while(is_playing()); }
// Sends the batter voltage in millivolts void send_battery_millivolts() { int message[1]; message[0] = read_battery_millivolts(); serial_send_blocking((char *)message, 2); }