void pot_test() { long start = get_ms(); char elapsed_ms; int value; set_analog_mode(MODE_10_BIT); print_long(read_trimpot()); print(" "); // to clear the display while((elapsed_ms = get_ms() - start) < 100) { value = read_trimpot(); play_frequency(value, 200, 15); if(value < elapsed_ms*10) { red_led(0); green_led(1); } else { red_led(1); green_led(0); } } }
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); } }
int main() { lcd_init_printf(); // required if we want to use printf() clear(); // clear the LCD delay_ms(200); // wait for 200 ms printf("hello"); // print "hello" on the first line of the LCD delay_ms(200); // wait for 200 ms printf("\nworld"); // print "world" on the second line (because of '\n') delay_ms(2000); // wait for 2 seconds clear(); // clear the LCD unsigned char ch; for (ch = 'A'; ch <= 'z'; ch++) // demonstrate LCD wrapping { printf("%c", ch); // print a string of characters that wraps when delay_ms(50); // it reaches the end of the LCD } delay_ms(2000); // wait for 2 seconds clear(); // clear the LCD int i; printf("Hex Dec"); for(i = 0; i <= 500; i += 50) // demonstrate LCD scrolling { delay_ms(800 - i); // the delay gets shorter as i gets bigger printf("\n%03X %3d", i, i); // print i as 3-digit, zero-padded hex // and a space-padded 3-digit decimal } delay_ms(2000); // wait for 2 seconds clear(); // clear the LCD printf("Trimpot:"); while (1) // continuously display the trimpot voltage in mV { lcd_goto_xy(0, 1); // go to start of second LCD row printf("%4u mV", to_millivolts(read_trimpot())); // print trimpot voltage delay_ms(50); // wait for 50 ms to reduce LCD flicker } }
int main() { set_analog_mode(MODE_10_BIT); // 10-bit analog-to-digital conversions while(1) // run over and over again { lcd_goto_xy(0,0); // LCD cursor to home position (upper-left) print_long(to_millivolts(read_trimpot())); // trimpot output in mV print(" mV "); // added spaces are to overwrite left over chars lcd_goto_xy(0, 1); // LCD cursor to start of the second line unsigned int temp = read_temperature_f(); // get temp in tenths of a degree F print_long(temp/10); // get the whole number of degrees print_character('.'); // print the decimal point print_long(temp - (temp/10)*10); // print the tenths digit print_character(223); // print a degree symbol print("F "); // added spaces are to overwrite left over chars delay_ms(100); // wait for 100 ms (otherwise LCD flickers too much) } }
// *** triggered by middle button *** // This function tests the eight user I/O pins and the trimmer potentiometer. // At any given time, one pin is being driven low while the rest are weakly // pulled high (to 5 V). At the same time, the LCD is displaying the input // values on the eight user I/O pins. If you short a pin to *GROUND* (note: // do not short the pin to power, only short it to one of the ground pads // along the top edge of the board), you will see the corresponding bit on // the LCD go to zero. The PD1 bit will always read zero as it is being // pulled down through the red user LED. unsigned char IOTest() { // the bits of the "outputs" byte will correspond to the pin states of // the user I/O lines as follows: // outputs: b7 b6 b5 b4 b3 b2 b1 b0 // user I/O: PC5 PC4 PC3 PC2 PC1 PC0 PD1 PD0 // Only one bit of "outputs" will ever be set (1) at a time; the rest will // be cleared (0). The user I/O pin that corresponds to the set bit will // be an output that is driven low while all of the other user I/O pins // will be inputs with internal pull-ups enabled (i.e. they will be weakly // pulled to 5V and will read as high). unsigned char outputs = 0x80; // binary: 10000000 unsigned char direction = 0; unsigned char button; red_led(0); // turn red and green LEDs off green_led(0); clear(); // clear the LCD print("User I/O"); set_analog_mode(MODE_8_BIT); // configure ADC for 8-bit readings while (1) // loop here until we detect a button press and return { time_reset(); // reset millisecond timer count to zero DDRC = 0; // make PC0 - PC5 inputs PORTC = 0; // PC0 - PC5 -> high impedance inputs DDRD &= ~0x03; // clear PD0 & PD1 bits (make them inputs) PORTD &= ~0x03; // PD0 & PD1 -> high impedance inputs PORTC |= ~outputs >> 2; // set the outputs states of PC0 - PC5 DDRC |= outputs >> 2; // make low pin an output (inputs for rest) PORTD |= ~outputs & 0x03; // set the output states of PD0 and PD1 DDRD |= outputs & 0x03; // make low pin an output (inputs for rest) // The following loop will execute for an amount of time determined // by the position of the user trimmer potentiometer (trimpot). // When the trimpot is at one extreme, the loop will take 256*2 = 512 // milliseconds. When the trimpot is at the other extreme, the // loop will only execute once, which takes slightly more than 20 ms. // In this way, the trimpot controls the speed at which the output // byte changes. do { // The bits of the "inputs" byte reflect the input values of pins // PD0, PD1, and PC0 - PC5. Bit 0 corresponds to PD0, bit 1 to // PD1, and bits 2 - 7 to PC0 - PC5, respectively. unsigned char inputs = PIND & 0x03; // store PD0 and PD1 input vals inputs |= PINC << 2; // store PC0 - PC5 input values lcd_goto_xy(0, 1); // go to the start of the second LCD line print_binary(inputs); // print the "input" byte in binary delay_ms(20); // delay here for 20 milliseconds // check if top or bottom buttons have been pressed button = button_is_pressed(TOP_BUTTON | BOTTOM_BUTTON); if (button != 0) // if so, reset I/O states, return button ID { DDRC = 0; // make PC0 - PC5 inputs PORTC = 0; // disable pull-ups on PC0 - PC5 DDRD &= ~0x03; // make PD0 and PD1 inputs PORTD &= ~0x03; // disable pull-ups on PD0 and PD1 return button; } } while (get_ms() < read_trimpot() * 2); if (direction) outputs <<= 1; // bit-shift our output byte left by one bit else outputs >>= 1; // bit-shift our output byte right by one bit if (outputs == 1) // if we have reached the right edge direction = 1; // switch direction to "left" if (outputs == 0x80) // if we have reached the left edge direction = 0; // switch direction to "right" } }
// Sends the trimpot value, 0-1023. void send_trimpot() { int message[1]; message[0] = read_trimpot(); serial_send_blocking((char *)message, 2); }
// *** triggered by middle button *** // This function tests the eight user I/O pins and the trimmer potentiometer. // At any given time, one pin is being driven low while the rest are weakly // pulled high (to 5 V). At the same time, the LCD is displaying the input // values on the eight user I/O pins. If you short a pin to *GROUND* (note: // do not short the pin to power, only short it to one of the ground pads // along the top edge of the board), you will see the corresponding bit on // the LCD go to zero. The PD1 bit will always read zero as it is being // pulled down through the red user LED. unsigned char IOTest() { // the bits of the "outputs" byte will correspond to the pin states of // the user I/O lines as follows: // outputs: b7 b6 b5 b4 b3 b2 b1 b0 // user I/O: PC5 PC4 PC3 PC2 PC1 PC0 PD1 PD0 // Only one bit of "outputs" will ever be set (1) at a time; the rest will // be cleared (0). The user I/O pin that corresponds to the set bit will // be an output that is driven low while all of the other user I/O pins // will be inputs with internal pull-ups enabled (i.e. they will be weakly // pulled to 5V and will read as high). unsigned int outputs = 0x8000; // binary: 10000000 00000000 unsigned char *outputsA = (unsigned char *)&outputs; // pointer to low byte of outputs unsigned char *outputsD = outputsA + 1; // pointer to high byte of outputs unsigned char direction = 0; unsigned char button; red_led(1); green_led(1); clear(); // clear the LCD print("User I/O"); lcd_goto_xy(0, 1); print("DDDDDDDD AAAAAAAA"); lcd_goto_xy(0, 2); print("76543210 76543210"); set_analog_mode(MODE_8_BIT); // configure ADC for 8-bit readings while (1) // loop here until we detect a button press and return { time_reset(); // reset millisecond timer count to zero DDRA = 0; // make PA0 - PA7 inputs PORTA = 0; // PA0 - PA7 -> high impedance inputs DDRD = 0; // make PD0 - PD7 inputs PORTD = 0; // PD0 - PD7 -> high impedance inputs PORTD |= ~(*outputsD); DDRD |= *outputsD; PORTA |= (~(*outputsA)) & 0x3F; DDRA |= *outputsA & 0x3F; // never make PA6 and PA7 outputs // The following loop will execute for an amount of time determined // by the position of the user trimmer potentiometer (trimpot). // When the trimpot is at one extreme, the loop will take 256*2 = 512 // milliseconds. When the trimpot is at the other extreme, the // loop will only execute once, which takes slightly more than 20 ms. // In this way, the trimpot controls the speed at which the output // byte changes. do { // The bits of the "inputs" byte reflect the input values of pins // PD0, PD1, and PC0 - PC5. Bit 0 corresponds to PD0, bit 1 to // PD1, and bits 2 - 7 to PC0 - PC5, respectively. unsigned char inputsA = PINA; unsigned char inputsD = PIND; lcd_goto_xy(0, 3); // go to the start of the fourth LCD line print_binary(inputsD); // print the "input" byte in binary print(" "); print_binary(inputsA); delay_ms(20); // delay here for 20 milliseconds // check if top or bottom buttons have been pressed button = button_is_pressed(TOP_BUTTON | BOTTOM_BUTTON); if (button != 0) // if so, reset I/O states, return button ID { DDRA = 0; // make PA0 - PA7 inputs PORTA = 0; // PA0 - PA7 -> high impedance inputs DDRD = 0; // make PD0 - PD7 inputs PORTD = 0; // PD0 - PD7 -> high impedance inputs return button; } } while (get_ms() < read_trimpot() * 2); if (direction) outputs <<= 1; // bit-shift our output byte left by one bit else outputs >>= 1; // bit-shift our output byte right by one bit if (outputs == 1) // if we have reached the right edge direction = 1; // switch direction to "left" if (outputs == 0x8000) // if we have reached the left edge direction = 0; // switch direction to "right" } }