void loop() // run over and over again { // wait here for one of the three buttons to be pushed unsigned char button = wait_for_button(ANY_BUTTON); clear(); if (button == TOP_BUTTON) { play_from_program_space(fugue); print("Fugue!"); lcd_goto_xy(0, 1); print("flash ->"); } if (button == MIDDLE_BUTTON) { play("! V8 cdefgab>cbagfedc"); print("C Major"); lcd_goto_xy(0, 1); print("RAM ->"); } if (button == BOTTOM_BUTTON) { if (is_playing()) { stop_playing(); print("stopped"); } else { play_note(A(5), 200, 15); print("note A5"); } } }
//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); }
int main() { // Initialize the encoders and specify the four input pins. encoders_init(IO_C2, IO_C3, IO_C4, IO_C5); while(1) { // Read the counts for motor 1 and print to LCD. lcd_goto_xy(0,0); print_long(encoders_get_counts_m1()); print(" "); // Read the counts for motor 2 and print to LCD. lcd_goto_xy(4,0); print_long(encoders_get_counts_m2()); print(" "); // Print encoder errors, if there are any. if(encoders_check_error_m1()) { lcd_goto_xy(0,1); print("Error 1"); } if(encoders_check_error_m2()) { lcd_goto_xy(0,1); print("Error 2"); } delay_ms(50); } }
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); } }
int main() // run over and over again { while(1) { // note that the following line could also be accomplished with: // int pot = analogRead(7); int pot = read_trimpot(); // determine the trimpot position // avoid clearing the LCD to reduce flicker lcd_goto_xy(0, 0); print("pot="); print_long(pot); // print the trim pot position (0 - 1023) print(" "); // overwrite any left over digits int motorSpeed = (512 - pot) / 2; lcd_goto_xy(0, 1); print("spd="); print_long(motorSpeed); // print the resulting motor speed (-255 - 255) print(" "); set_motors(motorSpeed, motorSpeed); // set speeds of motors 1 and 2 // all LEDs off red_led(0); green_led(0); // turn green LED on when motors are spinning forward if (motorSpeed > 0) green_led(1); // turn red LED on when motors are spinning in reverse if (motorSpeed < 0) red_led(1); delay_ms(100); } }
unsigned char motor_wait(unsigned int time_ms) { unsigned char button; unsigned long time = get_ms(); while (get_ms() - time < time_ms) { // check if top or bottom buttons have been pressed button = button_is_pressed(TOP_BUTTON | MIDDLE_BUTTON); if (button != 0) // if so, turn off motors and LEDs, return button ID { set_motors(0, 0); return button; } lcd_goto_xy(10, 0); print("cur: "); print_unsigned_long(x2_get_motor_current(MOTOR1)); print(" "); lcd_goto_xy(10, 1); print("cur: "); print_unsigned_long(x2_get_motor_current(MOTOR2)); print(" "); } return 0; }
/* * TASK: Update LCD * * Periodically update the LCD with information about system state */ static void update_lcd(void) { char buf[128]; char tmp[8]; clear(); lcd_goto_xy(0, 0); /* Print target/actual degrees */ sprintf(buf, "(%-5ld , %-5ld )", interpolator_get_absolute_target_position(), interpolator_get_current_position()); print(buf); sprintf(tmp, "%ld", interpolator_get_target_position()); lcd_goto_xy(1 + strlen(tmp), 0); print_character(CUSTOM_SYMBOL_DEGREE); sprintf(tmp, "%ld", interpolator_get_current_position()); lcd_goto_xy(9 + strlen(tmp), 0); print_character(CUSTOM_SYMBOL_DEGREE); /* Print last torque value */ lcd_goto_xy(0, 1); sprintf(buf, "torque: %d", motor_get_last_torque()); print(buf); }
/*----------------------------------------------------------------------------- * 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); }
int main() { TCCR0A = 0; // configure timer0 to run at 78 kHz TCCR0B = 0x04; // and overflow when TCNT0 = 256 (~3 ms) play_from_program_space(rhapsody); while(1) { // allow the sequence to keep playing automatically through the following delays #ifndef ALWAYS_CHECK play_mode(PLAY_AUTOMATIC); #else play_mode(PLAY_CHECK); #endif lcd_goto_xy(0, 0); print("blink!"); int i; for (i = 0; i < 8; i++) { #ifdef ALWAYS_CHECK play_check(); #endif red_led(1); delay_ms(500); red_led(0); delay_ms(500); } lcd_goto_xy(0, 0); print("timing"); lcd_goto_xy(0, 1); print(" "); // clear bottom LCD line // turn off automatic playing so that our time-critical code won't be interrupted by // the buzzer's long timer1 interrupt. Otherwise, this interrupt could throw off our // timing measurements. Instead, we will now use playCheck() to keep the sequence // playing in a way that won't throw off our measurements. #ifndef ALWAYS_AUTOMATIC play_mode(PLAY_CHECK); #endif unsigned char maxTime = 0; for (i = 0; i < 8000; i++) { TCNT0 = 0; while (TCNT0 < 20) // time for ~250 us ; if (TCNT0 > maxTime) maxTime = TCNT0; // if the elapsed time is greater than the previous max, save it #ifndef ALWAYS_AUTOMATIC play_check(); // check if it's time to play the next note and play it if so #endif } lcd_goto_xy(0, 1); print("max="); print_long((unsigned int)maxTime); print(" "); // overwrite any left over characters } }
void test() { unsigned char button; clear(); delay(200); print("Orangutn"); // print to the top line of the LCD delay_ms(400); // delay 200 ms lcd_goto_xy(0, 1); // go to the start of the second LCD line #if defined __AVR_ATmega328P__ print(" LV-328"); // print to the bottom line of the LCD #elif defined __AVR_ATmega168__ print(" LV-168"); // print to the bottom line of the LCD #else #error "Unrecognized device type" #endif delay_ms(1000); // delay 700 ms clear(); // clear the LCD, move cursor to start of top line print(" Temp."); do { // Perform 10-bit analog-to-digital conversions on ADC channel 6. // Average ten readings and return the result, which will be one // third of the battery voltage when the "ADC6 = VBAT/3" solder // bridge is in place on the bottom of the Orangutan PCB int Tf = read_temperature_f(); // read temp sensor on ADC6 in 0.1°F lcd_goto_xy(1, 1); // second character of the second LCD line print_long(Tf/10); // display temperature in °F print("."); // print the decimal point print_long(Tf - 10*(Tf/10)); // display the tenths digit print_character(223); // display the degree symbol character (°) print("F "); // display the units delay_ms(50); // delay for 50 ms button = button_is_pressed(ALL_BUTTONS); // check for button press } while (button == 0); // loop if no buttons are being pressed // *** MAIN LOOP *** while (1) // loop forever { if (button & TOP_BUTTON) // if the top button is pressed button = melodyTest(); // this func. loops until next button press else if (button & MIDDLE_BUTTON) // if the middle button is pressed button = IOTest(); // this func. loops until next button press else if (button & BOTTOM_BUTTON) // if the bottom button is pressed button = motorTest(); // this func. loops until next button press } }
int main() { clear(); // clear the LCD print("Send serial"); lcd_goto_xy(0, 1); // go to start of second LCD row print("or press B"); // Set the baud rate to 9600 bits per second. Each byte takes ten bit // times, so you can get at most 960 bytes per second at this speed. serial_set_baud_rate(USB_COMM, 9600); // Start receiving bytes in the ring buffer. serial_receive_ring(USB_COMM, receive_buffer, sizeof(receive_buffer)); while(1) { // USB_COMM is always in SERIAL_CHECK mode, so we need to call this // function often to make sure serial receptions and transmissions // occur. serial_check(); // Deal with any new bytes received. check_for_new_bytes_received(); // If the user presses the middle button, send "Hi there!" // and wait until the user releases the button. if (button_is_pressed(MIDDLE_BUTTON)) { wait_for_sending_to_finish(); memcpy_P(send_buffer, PSTR("Hi there!\r\n"), 11); serial_send(USB_COMM, send_buffer, 11); send_buffer[11] = 0; // terminate the string clear(); // clear the LCD lcd_goto_xy(0, 1); // go to start of second LCD row print("TX: "); print(send_buffer); // Wait for the user to release the button. While the processor is // waiting, the OrangutanSerial library will not be able to receive // bytes from the USB_COMM port since this requires calls to the // serial_check() function, which could cause serial bytes to be // lost. It will also not be able to send any bytes, so the bytes // bytes we just queued for transmission will not be sent until // after the following blocking function exits once the button is // released. If any of this is a concern, you can replace the // following line with: // do // { // while (button_is_pressed(MIDDLE_BUTTON)) // serial_check(); // receive and transmit as needed // delay_ms(10); // debounce the button press/release // } // while (button_is_pressed(MIDDLE_BUTTON)); wait_for_button_release(MIDDLE_BUTTON); } } }
// Blinks the LEDs void led_test() { play("c32"); print("Red 1 "); red_led(1); if(wait_for_250_ms_or_button_b()) return; red_led(0); if(wait_for_250_ms_or_button_b()) return; play(">c32"); lcd_goto_xy(0,0); print("Green 1 "); green_led(1); if(wait_for_250_ms_or_button_b()) return; green_led(0); if(wait_for_250_ms_or_button_b()) return; play("d32"); lcd_goto_xy(0,0); print("Red 2 "); red_led2(1); if(wait_for_250_ms_or_button_b()) return; red_led2(0); if(wait_for_250_ms_or_button_b()) return; play(">d32"); lcd_goto_xy(0,0); print("Green 2 "); green_led2(1); if(wait_for_250_ms_or_button_b()) return; green_led2(0); if(wait_for_250_ms_or_button_b()) return; play("e32>e32"); lcd_goto_xy(0,0); print("Yellow "); yellow_led(1); if(wait_for_250_ms_or_button_b()) return; yellow_led(0); if(wait_for_250_ms_or_button_b()) return; }
void lcd_screen(char* firsth_row_text,char* second_row_text) { cli(); lcd_clear(); lcd_goto_xy(0,0); lcd_write_str(firsth_row_text); lcd_goto_xy(0,1); lcd_write_str(second_row_text); sei(); }
void test() { unsigned char button; clear(); delay(200); print("Orangutn"); // print to the top line of the LCD delay_ms(400); // delay 200 ms lcd_goto_xy(0, 1); // go to the start of the second LCD line #if defined __AVR_ATmega328P__ print(" SV-328"); // print to the bottom line of the LCD #elif defined __AVR_ATmega168__ print(" SV-168"); // print to the bottom line of the LCD #else #error "Unrecognized device type" #endif delay_ms(1000); // delay 700 ms clear(); // clear the LCD, move cursor to start of top line print(" VBAT"); do { // Perform 10-bit analog-to-digital conversions on ADC channel 6. // Average ten readings and return the result, which will be one // third of the battery voltage when the "ADC6 = VBAT/3" solder // bridge is in place on the bottom of the Orangutan PCB int vbat = analog_read_average(6, 10); // 10-sample avg of ADC6 vbat = to_millivolts(vbat) * 3; // convert reading to bat. voltage (mV) lcd_goto_xy(0, 1); // go to the start of the second LCD line print_long(vbat); // display battery voltage in millivolts print(" mV "); // display the units delay_ms(50); // delay for 50 ms button = button_is_pressed(ANY_BUTTON); // check for button press } while (button == 0); // loop if no buttons are being pressed // *** MAIN LOOP *** while (1) // loop forever { if (button & TOP_BUTTON) // if the top button is pressed button = melodyTest(); // this func. loops until next button press else if (button & MIDDLE_BUTTON) // if the middle button is pressed button = IOTest(); // this func. loops until next button press else if (button & BOTTOM_BUTTON) // if the bottom button is pressed button = motorTest(); // this func. loops until next button press } }
int main() { // Make SSbar be an output so it does not interfere with SPI communication. set_digital_output(IO_B4, LOW); // Set the mode to SVP_MODE_ANALOG so we can get analog readings on line D/RX. svp_set_mode(SVP_MODE_ANALOG); while(1) { clear(); // Erase the LCD. if (usb_configured()) { // Connected to USB and the computer recognizes the device. print("USB"); } else if (usb_power_present()) { // Connected to USB. print("usb"); } if (usb_suspend()) { // Connected to USB, in the Suspend state. lcd_goto_xy(4,0); print("SUS"); } if (dtr_enabled()) { // The DTR virtual handshaking line is 1. // This often means that a terminal program is conencted to the // Pololu Orangutan SVP USB Communication Port. lcd_goto_xy(8,0); print("DTR"); } if (rts_enabled()) { // The RTS virtual handshaking line is 1. lcd_goto_xy(12,0); print("RTS"); } // Display an analog reading from channel D, in millivolts. lcd_goto_xy(0,1); print("Channel D: "); print_long(analog_read_millivolts(CHANNEL_D)); // Wait for 100 ms, otherwise the LCD would flicker. delay_ms(100); } }
void turn_in_place() { if (TIME_TO_DISPLAY) { clear(); lcd_goto_xy(0,0); print("Front"); lcd_goto_xy(0,1); print("Obstacle"); } // Turn to the right in place set_motors(50, -50); }
void back_up() { if (TIME_TO_DISPLAY) { clear(); lcd_goto_xy(0,0); print("Backing"); lcd_goto_xy(0,1); print("Up"); } // Back up slightly to the left set_motors(-50,-90); }
// *** triggered by middle button *** // This function tests the motors by first ramping motor1 speed from zero // to full speed "forward", to full speed "reverse", and finally back to zero. // It then does the same for motor2 before repeating all over again. // While motor1 is running, the red user LED is on, otherwise it is off. // While the currently active motor is moving "forward", the green user LED // is on, otherwise it is off. The LCD gives you feedback as to which motor // is currently moving in which direction (F = "forward", R = "reverse", and // - = inactive). unsigned char motorTest() { unsigned char button; int speed; unsigned char motor = 0; clear(); // clear the LCD, go to the start of the first LCD line print("motor2"); // print to the first line of the LCD lcd_goto_xy(0, 1); // go to the start of the second LCD line print("motor1"); // print to the second line of the LCD while (1) { red_led(!motor); // turn red LED on when m1 is active, off for m2 lcd_goto_xy(7, !motor); // go to end of LCD line for active motor print("F"); // print "F" for "forward" lcd_goto_xy(7, motor); // go to end of LCD line for inactive motor print("-"); // print "-" for "inactive" green_led(1); // turn green LED on when motor is moving "forward" for (speed = 0; speed < 255; speed++) { button = motorUpdate(motor, speed); // ramp up motor speed if (button != 0) // from 0 to 255 return button; } for (speed = 255; speed > -255; speed--) { if (speed == -1) // motor starts moving in "reverse" { green_led(0); // green LED off when motor going "reverse" lcd_goto_xy(7, !motor); // go to end of active motor's LCD line print("R"); // print "R" for "reverse" } button = motorUpdate(motor, speed); // ramp down motor speed if (button != 0) // from 255 to -255 return button; } for (speed = -255; speed <= 0; speed++) { button = motorUpdate(motor, speed); // ramp up motor speed if (button != 0) // from -255 to 0 return button; } motor = !motor; // alternate between m1 and m2 } }
int main() { // set up the 3pi initialize(); int last_proximity = 0; const int base_speed = 200; const int set_point = 100; // what is the set point for? // This is the "main loop" - it will run forever. while(1) { // In case it gets stuck: for 1 second every 15 seconds back up if (get_ms() % 15000 > 14000) { back_up(); continue; } // If something is directly in front turn to the right in place int front_proximity = analog_read(5); if (front_proximity > 200) { turn_in_place(); continue; } int proximity = analog_read(1); // 0 (far away) - 650 (close) int proportional = proximity - set_point; int derivative = proximity - last_proximity; // Proportional-Derivative Control Signal int pd = proportional / 3 + derivative * 20; int left_set = base_speed + pd; int right_set = base_speed - pd; set_motors(left_set, right_set); if (TIME_TO_DISPLAY) { clear(); lcd_goto_xy(0,0); print_long(proximity); lcd_goto_xy(5,0); print_long(pd); lcd_goto_xy(0,1); print_long(left_set); lcd_goto_xy(4,1); print_long(right_set); } last_proximity = proximity; // remember last proximity for derivative } // This part of the code is never reached. while(1) { set_motors(0,0) } }
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 } }
void check_for_qtr_shorts() { // Test for shorts unsigned int sensor_pins[] = {IO_C0,IO_C1,IO_C2,IO_C3,IO_C4}; unsigned char shorts = 1; clear(); print("qtr"); lcd_goto_xy(0,1); print("shorts"); while (shorts) { unsigned char i; shorts = 0; for(i=0;i<5;i++) { unsigned char j; for (j=0;j<5;j++) { set_digital_input(sensor_pins[j],PULL_UP_ENABLED); } set_digital_output(sensor_pins[i],LOW); delay_ms(2); for (j=0;j<5;j++) { if (i == j && is_digital_input_high(sensor_pins[j])) shorts = 1; if (i != j && !is_digital_input_high(sensor_pins[j])) shorts = 1; } } } }
void print_two_lines_delay_1s(const char *line1, const char *line2) { clear(); print_from_program_space(line1); lcd_goto_xy(0,1); print_from_program_space(line2); delay_ms(1000); }
int main() { setup(); //displayWelcome(); displayBattery(); unsigned char b = lineCalibration(); int numTurns = 0; const int MAX_TURNS = 8; char turns[MAX_TURNS+1]; if (b == BUTTON_B) { numTurns = followTrack(turns, MAX_TURNS); clear(); if (numTurns <= MAX_TURNS) { //play_from_program_space(success); turns[numTurns] = '\0'; print(turns); } else { print_long(numTurns); print(" turns."); } } else if (b == BUTTON_A) { bool l = false; bool r = false; bool s = false; followSegment(l, r, s); clear(); if (l) { lcd_goto_xy(0,1); print("L"); } if (s) { lcd_goto_xy(4,0); print("S"); } if (r) { lcd_goto_xy(7,1); print("R"); } } while (true); // prevent exit from main return 0; // dead code }
/** displayWelcome ****************************************** * Output welcome message, play tune and delay 2s. * @modifies - lcd displays welcome message, tune is played */ void displayWelcome() { clear(); print_from_program_space(welcome_line1); lcd_goto_xy(0,1); print_from_program_space(welcome_line2); play_from_program_space(welcome); delay_ms(2000); }
int main() // run once, when the program starts { print("Press a"); lcd_goto_xy(0, 1); print("button.."); while(1) loop(); }
void print_two_lines_delay_1s(const char *line1, const char *line2) { // Play welcome music and display a message clear(); print_from_program_space(line1); lcd_goto_xy(0,1); print_from_program_space(line2); delay_ms(1000); }
void check_emitter_jumper() { unsigned int values[5]; clear(); // off values while(!button_is_pressed(BUTTON_C)) { read_line_sensors(values,IR_EMITTERS_OFF); lcd_goto_xy(0,0); print("IR- "); display_values(values,TEST_LINE_SENSOR_TIMEOUT); lcd_goto_xy(6,1); print("C"); delay_ms(50); } while(button_is_pressed(ALL_BUTTONS)); }
void log_update_lcd() { clear(); lcd_goto_xy(0,0); print("V:"); print_long(g_velocity); print(" T:"); print_long(g_motor_output); print(" P:"); print_long(g_prop_gain); print(" D:"); print_long(g_der_gain); lcd_goto_xy(0,1); print("P: "); print_long(g_current_position); print(" G: "); print_long(g_target_position); }
int main() { print("Hello"); lcd_goto_xy(0, 1); print("World!"); while(1); }
// Prints on LCD a hex based picture, A hex picture can be produced from the "LCDAssistant.exe" windows based software. void printPictureOnLCD ( const unsigned char *data ) { int pixel_cols = LCD_WIDTH * (LCD_HEIGHT / CHAR_HEIGHT); //<6> means 6 lines on LCD. lcd_goto_xy(0, 0); for(int i=0;i<pixel_cols;i++) lcd_col(pgm_read_byte(data++)); //_delay_ms(100); }