Display delay

Hi, I have this code - i know its pretty long, most of its comments though, but what the situation is, is two jumpers 1 on pc5 the other ground, and the idea is when you touch them, then touch them again, it tells you the time between each touch. but the lcd is displaying the most random of numbers and i cant quite work it out. please can someone see if i’m making a stupid error. thanks

P.S most of the code outside of the main function is to initialise the LCD

// F_CPU tells util/delay.h our clock frequency
#define F_CPU 8000000UL	// Orangutan frequency (8MHz)

#include <avr/io.h>
#include <util/delay.h>

uint32_t timebetweenbuttonpresses;

#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))

//ABOVE HERE = STANDARD
    
//call delay function

void delayms( uint16_t millis ) {
	while ( millis ) {
		_delay_ms( 1 );
		millis--;
	}
}

//--------------------------------
//All to do with ADC
//--------------------------------

void analog_init(void) 
{

	ADMUX = (1 << REFS0);
	ADCSRA = (6 << ADPS0);
	ADCSRA |= (1 << ADEN);
}


// Read out the specified analog channel to 10 bits
unsigned int analog10(unsigned char channel) 
{
	// Clear the channel selection (low 5 bits in ADMUX)
	ADMUX &= ~0x1F;
	// Select the specified channel
	ADMUX |= channel;

	ADCSRA |= (1 << ADSC);

	while (bit_is_set(ADCSRA, ADSC));
	return (ADCL | ADCH << 8);	// read ADC (full 10 bits);
}

unsigned int analog8(unsigned char channel) 
{
	return(analog10(channel) >> 2);
}


//--------------------------------
//All to do with ADC
//--------------------------------

//-----------------------------------------
//All to do with LCD......................
//-----------------------------------------

#define PORTB_MASK		0x38	// PB3, PB4, PB5
#define PORTD_MASK		0x80	// PD7

#define PORTB_SHIFT		3
#define PORTD_SHIFT		1

#define LCD_RW			PD3
#define LCD_RS			PD2
#define LCD_E			PD4

#define	LCD_CLEAR		0x01
#define	LCD_LINE1		0x02
#define LCD_LINE2		0xC0
#define LCD_SHOW_BLINK	0x0F
#define LCD_SHOW_SOLID	0x0E		
#define LCD_HIDE		0x0C
#define LCD_CURSOR_L	0x10
#define LCD_CURSOR_R	0x14
#define LCD_SHIFT_L		0x18
#define LCD_SHIFT_R		0x1C


void lcd_nibble(unsigned char nibble)
{

	
	nibble &= 0x0F;

	// Shift our nibble so bits 0, 1, and 2 line up with PB3, PB4,
	// and PB5:

	nibble <<= PORTB_SHIFT;

	// Clear those bits out of PORTB so we can write into them:

	PORTB &= ~PORTB_MASK;

	// And load PORTB with those three bits:

	PORTB |= (nibble & PORTB_MASK);

	// Now shift our nibble so bit 3 lines up with PD7:

	nibble <<= PORTD_SHIFT;

	// Clear that bit out of PORTD so we can write into it:

	PORTD &= ~PORTD_MASK;

	// And load pORTD with that last bit:

	PORTD |= (nibble & PORTD_MASK);

	// Delay for 1ms so the LCD can register it's got the nibble:

	_delay_ms(1);



	PORTD |= (1 << LCD_E);
	asm(
		"nop" "\n\t"
		"nop" "\n\t"
		"nop" "\n\t"
		"nop" "\n\t"
		"nop" "\n\t"
		::);

	// Bring E low

	PORTD &= ~(1 << LCD_E);

	asm(
		"nop" "\n\t"
		"nop" "\n\t"
		"nop" "\n\t"
		"nop" "\n\t"
		"nop" "\n\t"
		::);

	// Our nibble has now been sent to the LCD.
}



void lcd_send(unsigned char data)
{
	unsigned char 	temp_ddrb, temp_portb,
					temp_ddrd, temp_portd;

	// Store our port settings

	temp_ddrb = DDRB;
	temp_portb = PORTB;
	temp_ddrd = DDRD;
	temp_portd = PORTD;

	// Set up port I/O to match what the LCD needs

	DDRB |= PORTB_MASK;
	DDRD |= PORTD_MASK;

	// Send the data

	lcd_nibble(data >> 4);	// High nibble first
	lcd_nibble(data);		// Low nibble second

	// Restore our port settings

	DDRD = temp_ddrd;
	PORTD = temp_portd;
	DDRB = temp_ddrb;
	PORTB = temp_portb;
}



void lcd_cmd(unsigned char cmd)
{
	// Hold RW and RS low

	PORTD &= !((1 << LCD_RW) | (1 << LCD_RS));

	// Send the command

	lcd_send(cmd);

	// Delay for 1ms to let the command process

	_delay_ms(1);
}

void lcd_data(unsigned char data)
{
	// Hold RW low

	PORTD &= ~(1 << LCD_RW);

	// Hold RS high

	PORTD |= (1 << LCD_RS);

	// Send the data.  No waits, so we can go right into sending more 
	// data.

	lcd_send(data);
}

// lcd_string sends a string to the LCD.  

void lcd_string(const unsigned char *str)
{
	while (*str != 0)
		lcd_data(*str++);
}

// lcd_int prints an integer.  

void lcd_int(unsigned char n)
{
	unsigned char st[4] = {0,0,0,0};

	// The 0x30 addition shifts the decimal number up
	// to the ASCII location of "0".

	// Hundreds place
	st[0] = (n / 100) + 0x30;
	n = n % 100;

	// Tens place
	st[1] = (n / 10) + 0x30;
	n = n % 10;

	// Ones place
	st[2] = n + 0x30;

	// Print it as a string
	lcd_string(st);
}

// lcd_init initializes the LCD and MUST be called prior to
// every other LCD command.

void lcd_init(void)
{

	
	DDRD |= (1 << LCD_RW) | (1 << LCD_RS) | (1 << LCD_E);

	

	_delay_ms(30);
	lcd_cmd(0x30);		// 8-bit mode (wake up!)
	_delay_ms(5);
	lcd_cmd(0x30);		// 8-bit mode (wake up!)
	_delay_ms(1);
	lcd_cmd(0x30);		// 8-bit mode (wake up!)
	_delay_ms(1);
	lcd_cmd(0x32);		// 4-bit mode

	//telling the lcd everything..

	lcd_cmd(0x20);
	lcd_cmd(0x28);		// 4-bit mode, 2-line, 5x8 dots/char
	lcd_cmd(0x08);		// Display off, cursor off, blink off
	lcd_cmd(0x01);		// Clear display
	lcd_cmd(0x0F);		// Display on, cursor on, blink on
	lcd_cmd(0x02);		// Return home
	lcd_cmd(0x01);		// Clear display

	// At this point we're good to go.

}



void lcd_moveto(unsigned char line, unsigned char pos)
{

  

  lcd_cmd((line == 1 ? 0x80 : 0xC0) + pos);
}



void lcd_moverel(unsigned char dir, unsigned char num)
{
	unsigned char cmd;

	cmd = dir ? LCD_CURSOR_R : LCD_CURSOR_L;
	while(num-- > 0)
		lcd_cmd(cmd);
}



void lcd_shift(unsigned char dir, unsigned char num)
{
	unsigned char cmd;

	cmd = dir ? LCD_SHIFT_R : LCD_SHIFT_L;
	while(num-- > 0)
		lcd_cmd(cmd);
}



#define lcd_clear()	lcd_cmd(LCD_CLEAR)

// Move the cursor to the beginning of line 1

#define lcd_line1() lcd_cmd(LCD_LINE1)

// Move the cursor to the beginning of line 2

#define lcd_line2() lcd_cmd(LCD_LINE2)

// Show the cursor as a blinking block.  (A non-blinking cursor is
// also available.)

#define lcd_show() lcd_cmd(LCD_SHOW_BLINK)

// Hide the cursor.

#define lcd_hide() lcd_cmd(LCD_HIDE)


void delay_sec(unsigned char sec)
{
	unsigned int cycles;


	for(cycles = 0; cycles < (sec * 15); cycles ++)
	{
		_delay_ms(15);
	}
}



//-----------------------------------------
//All to do with LCD......................
//-----------------------------------------


//STANDARD MAIN FUNCTION

int main( void ) 
{
	while(1)
	{
	//--------------------------------------
	//LCD STUFF TO GO IN MAIN FUNCTION
	//--------------------------------------
	int i;

        // Make sure all our registers are clear
        DDRB = 0;
        DDRC = 0;
        DDRD = 0;

        PORTB = 0;
        PORTC = 0;
        PORTD = 0;

	// Initialize the LCD
	lcd_init();
	//-----------------------------
	//END MAIN FUNCTION LCD STUFF
	//------------------------------

cbi(DDRC, DDC5);
sbi(PORTC, PC5);
if (bit_is_clear(PINC, PINC5))
_delay_ms(50);
while (bit_is_clear(PINC, PINC5))
_delay_ms(10);
while bit_is_set(PINC, PINC5)
{
_delay_ms(1);
timebetweenbuttonpresses++;

} 

// A variable to store our converted analog value in:
   //int pot;

   // Initialize the ADC:
  // analog_init();

	//SETTING PORT OUTPUTS INPUTS: REMEMBER CANT USE SWITCHES AND LCD...
	
//	DDRB |= (1 << PB0);		// set buzzer pin PB0 to output
//	DDRC |= (1<< PC5);// led breadboard
//	DDRB &= ~(1 << PB3);  // make PB3 (button SW3 pin) an input - cant use with LCD
	//DDRD |= (1<<PD1); //Led output

   //-----------------------------------------
   //COMMANDS FOR PRINTING WITH LCD
   //-----------------------------------------
   
   //for(;;)
//	{
	//pot = analog10(6);
//	if (pot > 100)
//{	
//PORTC |= (1<< PC5);// turn on breadboard (sonar transmitter)
		//PORTB |= ( 1 << PB0 );	// buzzer on
		//PORTD |= (1 << PD1);//LED ON
		//delayms( 90 );			// delay 50 ms
		//PORTB &=~ (1 << PB0); //BUZZER Off
		//PORTC &= ~(1 << PC5);//turn off bread board (sonar transmitter)
		
		//PORTD &= ~(1 << PD1); 		// LED OFF
		
	//	}

	//else
    
	//PORTD &= ~(1 << PD1);
	//PORTC |= (1<< PC5);
	//delayms( 45 );
	//PORTC &= ~(1 << PC5);
	
		// Startup Banner
		

	//}

	//	delay_sec(2);
	lcd_clear();
		lcd_line1();
		lcd_string("time:");
		lcd_line2();
		lcd_int(timebetweenbuttonpresses);

			delay_sec(2);
	}
	return 1;
	

}

I made a couple of minor changes to your code, which was pretty much good.

The big problem is feeding the number of milliseconds stored in timebetweenbuttonpresses to the lcd_int function. The function’s name is a little misleading, since it doesn’t actually use the 16-bit integer type. It casts the argument number as a character, immediately truncating it to only it’s lower eight bytes, then processes that number into a three character decimal string.

So, if timebetweenbuttonpresses=10347ms, you’ll see time: 347 on the screen. I added a similar function lcd_time, which will format up to 1000000ms into an eight character string with a decimal point and the unit ‘s’ at the end. With the new function you would see time: 010.347s on the screen.

I added a line that resets timebetweenbuttonpresses to zero after each display, before it just kept adding time to the previous value. I also fixed the delay_sec function to delay for actual seconds (it was only delaying for 0.225 seconds). If you’re using AVR Studio, make sure that you have compiler optimization turned on in the Project->Configuration Options menu (set it to something other than -O0). Otherwise the delay functions won’t compile reliably and you might suddenly just get garbage on your LCD.

Here’s the code:

    // F_CPU tells util/delay.h our clock frequency
    #define F_CPU 8000000UL   // Orangutan frequency (8MHz)

    #include <avr/io.h>
    #include <util/delay.h>

    uint32_t timebetweenbuttonpresses;

    #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
    #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))

    //ABOVE HERE = STANDARD
       
    //call delay function

    void delayms( uint16_t millis ) {
       while ( millis ) {
          _delay_ms( 1 );
          millis--;
       }
    }

    //--------------------------------
    //All to do with ADC
    //--------------------------------

    void analog_init(void)
    {

       ADMUX = (1 << REFS0);
       ADCSRA = (6 << ADPS0);
       ADCSRA |= (1 << ADEN);
    }


    // Read out the specified analog channel to 10 bits
    unsigned int analog10(unsigned char channel)
    {
       // Clear the channel selection (low 5 bits in ADMUX)
       ADMUX &= ~0x1F;
       // Select the specified channel
       ADMUX |= channel;

       ADCSRA |= (1 << ADSC);

       while (bit_is_set(ADCSRA, ADSC));
       return (ADCL | ADCH << 8);   // read ADC (full 10 bits);
    }

    unsigned int analog8(unsigned char channel)
    {
       return(analog10(channel) >> 2);
    }


    //--------------------------------
    //All to do with ADC
    //--------------------------------

    //-----------------------------------------
    //All to do with LCD......................
    //-----------------------------------------

    #define PORTB_MASK      0x38   // PB3, PB4, PB5
    #define PORTD_MASK      0x80   // PD7

    #define PORTB_SHIFT      3
    #define PORTD_SHIFT      1

    #define LCD_RW         PD3
    #define LCD_RS         PD2
    #define LCD_E         PD4

    #define   LCD_CLEAR      0x01
    #define   LCD_LINE1      0x02
    #define LCD_LINE2      0xC0
    #define LCD_SHOW_BLINK   0x0F
    #define LCD_SHOW_SOLID   0x0E      
    #define LCD_HIDE      0x0C
    #define LCD_CURSOR_L   0x10
    #define LCD_CURSOR_R   0x14
    #define LCD_SHIFT_L      0x18
    #define LCD_SHIFT_R      0x1C


    void lcd_nibble(unsigned char nibble)
    {

       
       nibble &= 0x0F;

       // Shift our nibble so bits 0, 1, and 2 line up with PB3, PB4,
       // and PB5:

       nibble <<= PORTB_SHIFT;

       // Clear those bits out of PORTB so we can write into them:

       PORTB &= ~PORTB_MASK;

       // And load PORTB with those three bits:

       PORTB |= (nibble & PORTB_MASK);

       // Now shift our nibble so bit 3 lines up with PD7:

       nibble <<= PORTD_SHIFT;

       // Clear that bit out of PORTD so we can write into it:

       PORTD &= ~PORTD_MASK;

       // And load pORTD with that last bit:

       PORTD |= (nibble & PORTD_MASK);

       // Delay for 1ms so the LCD can register it's got the nibble:

       _delay_ms(1);



       PORTD |= (1 << LCD_E);
       asm(
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          ::);

       // Bring E low

       PORTD &= ~(1 << LCD_E);

       asm(
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          ::);

       // Our nibble has now been sent to the LCD.
    }



    void lcd_send(unsigned char data)
    {
       unsigned char    temp_ddrb, temp_portb,
                   temp_ddrd, temp_portd;

       // Store our port settings

       temp_ddrb = DDRB;
       temp_portb = PORTB;
       temp_ddrd = DDRD;
       temp_portd = PORTD;

       // Set up port I/O to match what the LCD needs

       DDRB |= PORTB_MASK;
       DDRD |= PORTD_MASK;

       // Send the data

       lcd_nibble(data >> 4);   // High nibble first
       lcd_nibble(data);      // Low nibble second

       // Restore our port settings

       DDRD = temp_ddrd;
       PORTD = temp_portd;
       DDRB = temp_ddrb;
       PORTB = temp_portb;
    }



    void lcd_cmd(unsigned char cmd)
    {
       // Hold RW and RS low

       PORTD &= !((1 << LCD_RW) | (1 << LCD_RS));

       // Send the command

       lcd_send(cmd);

       // Delay for 1ms to let the command process

       _delay_ms(1);
    }

    void lcd_data(unsigned char data)
    {
       // Hold RW low

       PORTD &= ~(1 << LCD_RW);

       // Hold RS high

       PORTD |= (1 << LCD_RS);

       // Send the data.  No waits, so we can go right into sending more
       // data.

       lcd_send(data);
    }

    // lcd_string sends a string to the LCD. 

    void lcd_string(const unsigned char *str)
    {
       while (*str != 0)
          lcd_data(*str++);
    }

    // lcd_int prints an integer. 

    void lcd_int(unsigned char n)
    {
       unsigned char st[4] = {0,0,0,0};

       // The 0x30 addition shifts the decimal number up
       // to the ASCII location of "0".

       // Hundreds place
       st[0] = (n / 100) + 0x30;
       n = n % 100;

       // Tens place
       st[1] = (n / 10) + 0x30;
       n = n % 10;

       // Ones place
       st[2] = n + 0x30;

       // Print it as a string
       lcd_string(st);
    }


    void lcd_time(unsigned long int n)
    {
       unsigned char st[8] = {0,0,0,'.',0,0,0,'s'};

       // The 0x30 addition shifts the decimal number up
       // to the ASCII location of "0".
		
       //100s
       st[0] = (n / 100000) + 0x30;
       n = n % 100000;

       //10s
       st[1] = (n / 10000) + 0x30;
       n = n % 10000;

       // 1s
       st[2] = (n / 1000) + 0x30;
       n = n % 1000;


       // 0.1s
       st[4] = (n / 100) + 0x30;
       n = n % 100;

       // 0.01s
       st[5] = (n / 10) + 0x30;
       n = n % 10;

       // 0.001s
       st[6] = n + 0x30;

       // Print it as a string
       lcd_string(st);
    }


    // lcd_init initializes the LCD and MUST be called prior to
    // every other LCD command.

    void lcd_init(void)
    {

       
       DDRD |= (1 << LCD_RW) | (1 << LCD_RS) | (1 << LCD_E);

       

       _delay_ms(30);
       lcd_cmd(0x30);      // 8-bit mode (wake up!)
       _delay_ms(5);
       lcd_cmd(0x30);      // 8-bit mode (wake up!)
       _delay_ms(1);
       lcd_cmd(0x30);      // 8-bit mode (wake up!)
       _delay_ms(1);
       lcd_cmd(0x32);      // 4-bit mode

       //telling the lcd everything..

       lcd_cmd(0x20);
       lcd_cmd(0x28);      // 4-bit mode, 2-line, 5x8 dots/char
       lcd_cmd(0x08);      // Display off, cursor off, blink off
       lcd_cmd(0x01);      // Clear display
       lcd_cmd(0x0F);      // Display on, cursor on, blink on
       lcd_cmd(0x02);      // Return home
       lcd_cmd(0x01);      // Clear display

       // At this point we're good to go.

    }



    void lcd_moveto(unsigned char line, unsigned char pos)
    {

     

      lcd_cmd((line == 1 ? 0x80 : 0xC0) + pos);
    }



    void lcd_moverel(unsigned char dir, unsigned char num)
    {
       unsigned char cmd;

       cmd = dir ? LCD_CURSOR_R : LCD_CURSOR_L;
       while(num-- > 0)
          lcd_cmd(cmd);
    }



    void lcd_shift(unsigned char dir, unsigned char num)
    {
       unsigned char cmd;

       cmd = dir ? LCD_SHIFT_R : LCD_SHIFT_L;
       while(num-- > 0)
          lcd_cmd(cmd);
    }



    #define lcd_clear()   lcd_cmd(LCD_CLEAR)

    // Move the cursor to the beginning of line 1

    #define lcd_line1() lcd_cmd(LCD_LINE1)

    // Move the cursor to the beginning of line 2

    #define lcd_line2() lcd_cmd(LCD_LINE2)

    // Show the cursor as a blinking block.  (A non-blinking cursor is
    // also available.)

    #define lcd_show() lcd_cmd(LCD_SHOW_BLINK)

    // Hide the cursor.

    #define lcd_hide() lcd_cmd(LCD_HIDE)


    void delay_sec(unsigned char sec)
    {
       unsigned int cycles;


       for(cycles = 0; cycles < (sec * 100); cycles ++)
       {
          _delay_ms(10);
       }
    }



    //-----------------------------------------
    //All to do with LCD......................
    //-----------------------------------------


    //STANDARD MAIN FUNCTION

    int main( void )
    {
       while(1)
       {
       //--------------------------------------
       //LCD STUFF TO GO IN MAIN FUNCTION
       //--------------------------------------
       int i;

            // Make sure all our registers are clear
            DDRB = 0;
            DDRC = 0;
            DDRD = 0;

            PORTB = 0;
            PORTC = 0;
            PORTD = 0;

       // Initialize the LCD
       lcd_init();
       //-----------------------------
       //END MAIN FUNCTION LCD STUFF
       //------------------------------

    cbi(DDRC, DDC5);
    sbi(PORTC, PC5);
    if (bit_is_clear(PINC, PINC5))
    _delay_ms(50);
    while (bit_is_clear(PINC, PINC5))
    _delay_ms(10);
    while bit_is_set(PINC, PINC5)
    {
    _delay_ms(1);
    timebetweenbuttonpresses++;

    }

    // A variable to store our converted analog value in:
       //int pot;

       // Initialize the ADC:
      // analog_init();

       //SETTING PORT OUTPUTS INPUTS: REMEMBER CANT USE SWITCHES AND LCD...
       
    //   DDRB |= (1 << PB0);      // set buzzer pin PB0 to output
    //   DDRC |= (1<< PC5);// led breadboard
    //   DDRB &= ~(1 << PB3);  // make PB3 (button SW3 pin) an input - cant use with LCD
       //DDRD |= (1<<PD1); //Led output

       //-----------------------------------------
       //COMMANDS FOR PRINTING WITH LCD
       //-----------------------------------------
       
       //for(;;)
    //   {
       //pot = analog10(6);
    //   if (pot > 100)
    //{   
    //PORTC |= (1<< PC5);// turn on breadboard (sonar transmitter)
          //PORTB |= ( 1 << PB0 );   // buzzer on
          //PORTD |= (1 << PD1);//LED ON
          //delayms( 90 );         // delay 50 ms
          //PORTB &=~ (1 << PB0); //BUZZER Off
          //PORTC &= ~(1 << PC5);//turn off bread board (sonar transmitter)
          
          //PORTD &= ~(1 << PD1);       // LED OFF
          
       //   }

       //else
       
       //PORTD &= ~(1 << PD1);
       //PORTC |= (1<< PC5);
       //delayms( 45 );
       //PORTC &= ~(1 << PC5);
       
          // Startup Banner
          

       //}

       //   delay_sec(2);
       lcd_clear();
          lcd_line1();
          lcd_string("time:");
          lcd_line2();
          lcd_time(timebetweenbuttonpresses);

		  timebetweenbuttonpresses=0;

             delay_sec(2);
       }
       return 1;
       

    }

So, what are your plans for this program?

-Adam

Thanks Adam, I want to be able to time how long it takes between the pushbutton being pressed twice - its really nothing at the moment.

Thanks for changing those errors, but it still doesnt seem to be working to well, unless i’m miss interpreting it.
it is now on, and i havent touched it for over the last minute, i am now going to touch the two jumper wires (there is nothing on the screen)…
it says the time is 014.094 seconds. I only touched the wires once together. as far as i understand that is 14 seconds… it was definately more than 14 seconds since last touch…

sorry forgot to say in last post, is there a way i can get it to ask for the first and second touch on the screen, then i knowwhere i am in the sequence

thanks

alex

That’s very odd, it’s working perfectly for me.

I would check your fuses and see if the “Divide Clock By 8 Internally” fuse is set. If that was the case and you left it sitting for about two minutes you would see that time displayed. That would also explain why your short delay_sec function could have produced a more reasonable seeming delay.

You can write anything you want to the first line to let yourself know what’s going on. The way you wrote the code now it’s always counting, so there really aren’t first and second touches. When you start up the Orangutan your code starts counting time, and then as soon as you touch your wires together it displays the time since startup. As soon as you release the wires the counter resets itself and starts counting again, and so on.

-Adam

sorry adam should the divide by 8 fuse be on or off…

secondly can i get it so that the clock doesnt start until the wires are touched (and until that point it says “touch wires to start clock”) then lcd changes to “touch wires to stop clock”, and when you touch wires t repeats the sequence? i have only used the lcd to display one thing so far, how would i get it to do that… i’ll check my fuses in the morning, i hope thats the problem…

a seperate-ish question. if there is the number 1 displayed on the lcd as a menu option for instance, and you turn the pot, can you increase that number 1, then press a button, which would select the menu option that was on the lcd then?? (did that make sense)

alex

Either the divide by 8 fuse needs to be off (unchecked) or you need to change the frequency defined in your code from:
#define F_CPU 8000000UL // Orangutan frequency (8MHz)
to
#define F_CPU 1000000UL // Orangutan frequency (8MHz/8)

One way or the other, they just have to agree. I would turn off (uncheck) the fuse myself, why not let your Orangutan run at full speed!

As for push to start/push to stop, you can do it by adding a:
while(bit_is_set(PINC, PINC5));//do nothing
pause in your main while loop, like this:

    // F_CPU tells util/delay.h our clock frequency
    #define F_CPU 8000000UL   // Orangutan frequency (8MHz)

    #include <avr/io.h>
    #include <util/delay.h>

    uint32_t timebetweenbuttonpresses;

    #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
    #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))

    //ABOVE HERE = STANDARD
       
    //call delay function

    void delayms( uint16_t millis ) {
       while ( millis ) {
          _delay_ms( 1 );
          millis--;
       }
    }

    //--------------------------------
    //All to do with ADC
    //--------------------------------

    void analog_init(void)
    {

       ADMUX = (1 << REFS0);
       ADCSRA = (6 << ADPS0);
       ADCSRA |= (1 << ADEN);
    }


    // Read out the specified analog channel to 10 bits
    unsigned int analog10(unsigned char channel)
    {
       // Clear the channel selection (low 5 bits in ADMUX)
       ADMUX &= ~0x1F;
       // Select the specified channel
       ADMUX |= channel;

       ADCSRA |= (1 << ADSC);

       while (bit_is_set(ADCSRA, ADSC));
       return (ADCL | ADCH << 8);   // read ADC (full 10 bits);
    }

    unsigned int analog8(unsigned char channel)
    {
       return(analog10(channel) >> 2);
    }


    //--------------------------------
    //All to do with ADC
    //--------------------------------

    //-----------------------------------------
    //All to do with LCD......................
    //-----------------------------------------

    #define PORTB_MASK      0x38   // PB3, PB4, PB5
    #define PORTD_MASK      0x80   // PD7

    #define PORTB_SHIFT      3
    #define PORTD_SHIFT      1

    #define LCD_RW         PD3
    #define LCD_RS         PD2
    #define LCD_E         PD4

    #define   LCD_CLEAR      0x01
    #define   LCD_LINE1      0x02
    #define LCD_LINE2      0xC0
    #define LCD_SHOW_BLINK   0x0F
    #define LCD_SHOW_SOLID   0x0E      
    #define LCD_HIDE      0x0C
    #define LCD_CURSOR_L   0x10
    #define LCD_CURSOR_R   0x14
    #define LCD_SHIFT_L      0x18
    #define LCD_SHIFT_R      0x1C


    void lcd_nibble(unsigned char nibble)
    {

       
       nibble &= 0x0F;

       // Shift our nibble so bits 0, 1, and 2 line up with PB3, PB4,
       // and PB5:

       nibble <<= PORTB_SHIFT;

       // Clear those bits out of PORTB so we can write into them:

       PORTB &= ~PORTB_MASK;

       // And load PORTB with those three bits:

       PORTB |= (nibble & PORTB_MASK);

       // Now shift our nibble so bit 3 lines up with PD7:

       nibble <<= PORTD_SHIFT;

       // Clear that bit out of PORTD so we can write into it:

       PORTD &= ~PORTD_MASK;

       // And load pORTD with that last bit:

       PORTD |= (nibble & PORTD_MASK);

       // Delay for 1ms so the LCD can register it's got the nibble:

       _delay_ms(1);



       PORTD |= (1 << LCD_E);
       asm(
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          ::);

       // Bring E low

       PORTD &= ~(1 << LCD_E);

       asm(
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          ::);

       // Our nibble has now been sent to the LCD.
    }



    void lcd_send(unsigned char data)
    {
       unsigned char    temp_ddrb, temp_portb,
                   temp_ddrd, temp_portd;

       // Store our port settings

       temp_ddrb = DDRB;
       temp_portb = PORTB;
       temp_ddrd = DDRD;
       temp_portd = PORTD;

       // Set up port I/O to match what the LCD needs

       DDRB |= PORTB_MASK;
       DDRD |= PORTD_MASK;

       // Send the data

       lcd_nibble(data >> 4);   // High nibble first
       lcd_nibble(data);      // Low nibble second

       // Restore our port settings

       DDRD = temp_ddrd;
       PORTD = temp_portd;
       DDRB = temp_ddrb;
       PORTB = temp_portb;
    }



    void lcd_cmd(unsigned char cmd)
    {
       // Hold RW and RS low

       PORTD &= !((1 << LCD_RW) | (1 << LCD_RS));

       // Send the command

       lcd_send(cmd);

       // Delay for 1ms to let the command process

       _delay_ms(1);
    }

    void lcd_data(unsigned char data)
    {
       // Hold RW low

       PORTD &= ~(1 << LCD_RW);

       // Hold RS high

       PORTD |= (1 << LCD_RS);

       // Send the data.  No waits, so we can go right into sending more
       // data.

       lcd_send(data);
    }

    // lcd_string sends a string to the LCD. 

    void lcd_string(const unsigned char *str)
    {
       while (*str != 0)
          lcd_data(*str++);
    }

    // lcd_int prints an integer. 

    void lcd_int(unsigned char n)
    {
       unsigned char st[4] = {0,0,0,0};

       // The 0x30 addition shifts the decimal number up
       // to the ASCII location of "0".

       // Hundreds place
       st[0] = (n / 100) + 0x30;
       n = n % 100;

       // Tens place
       st[1] = (n / 10) + 0x30;
       n = n % 10;

       // Ones place
       st[2] = n + 0x30;

       // Print it as a string
       lcd_string(st);
    }


    void lcd_time(unsigned long int n)
    {
       unsigned char st[8] = {0,0,0,'.',0,0,0,'s'};

       // The 0x30 addition shifts the decimal number up
       // to the ASCII location of "0".
		
       //100s
       st[0] = (n / 100000) + 0x30;
       n = n % 100000;

       //10s
       st[1] = (n / 10000) + 0x30;
       n = n % 10000;

       // 1s
       st[2] = (n / 1000) + 0x30;
       n = n % 1000;


       // 0.1s
       st[4] = (n / 100) + 0x30;
       n = n % 100;

       // 0.01s
       st[5] = (n / 10) + 0x30;
       n = n % 10;

       // 0.001s
       st[6] = n + 0x30;

       // Print it as a string
       lcd_string(st);
    }


    // lcd_init initializes the LCD and MUST be called prior to
    // every other LCD command.

    void lcd_init(void)
    {

       
       DDRD |= (1 << LCD_RW) | (1 << LCD_RS) | (1 << LCD_E);

       

       _delay_ms(30);
       lcd_cmd(0x30);      // 8-bit mode (wake up!)
       _delay_ms(5);
       lcd_cmd(0x30);      // 8-bit mode (wake up!)
       _delay_ms(1);
       lcd_cmd(0x30);      // 8-bit mode (wake up!)
       _delay_ms(1);
       lcd_cmd(0x32);      // 4-bit mode

       //telling the lcd everything..

       lcd_cmd(0x20);
       lcd_cmd(0x28);      // 4-bit mode, 2-line, 5x8 dots/char
       lcd_cmd(0x08);      // Display off, cursor off, blink off
       lcd_cmd(0x01);      // Clear display
       lcd_cmd(0x0F);      // Display on, cursor on, blink on
       lcd_cmd(0x02);      // Return home
       lcd_cmd(0x01);      // Clear display

       // At this point we're good to go.

    }



    void lcd_moveto(unsigned char line, unsigned char pos)
    {

     

      lcd_cmd((line == 1 ? 0x80 : 0xC0) + pos);
    }



    void lcd_moverel(unsigned char dir, unsigned char num)
    {
       unsigned char cmd;

       cmd = dir ? LCD_CURSOR_R : LCD_CURSOR_L;
       while(num-- > 0)
          lcd_cmd(cmd);
    }



    void lcd_shift(unsigned char dir, unsigned char num)
    {
       unsigned char cmd;

       cmd = dir ? LCD_SHIFT_R : LCD_SHIFT_L;
       while(num-- > 0)
          lcd_cmd(cmd);
    }



    #define lcd_clear()   lcd_cmd(LCD_CLEAR)

    // Move the cursor to the beginning of line 1

    #define lcd_line1() lcd_cmd(LCD_LINE1)

    // Move the cursor to the beginning of line 2

    #define lcd_line2() lcd_cmd(LCD_LINE2)

    // Show the cursor as a blinking block.  (A non-blinking cursor is
    // also available.)

    #define lcd_show() lcd_cmd(LCD_SHOW_BLINK)

    // Hide the cursor.

    #define lcd_hide() lcd_cmd(LCD_HIDE)


    void delay_sec(unsigned char sec)
    {
       unsigned int cycles;


       for(cycles = 0; cycles < (sec * 100); cycles ++)
       {
          _delay_ms(10);
       }
    }



    //-----------------------------------------
    //All to do with LCD......................
    //-----------------------------------------


    //STANDARD MAIN FUNCTION

    int main( void )
    {

	   //--------------------------------------
	   //LCD STUFF TO GO IN MAIN FUNCTION
	   //--------------------------------------
	   int i;

	        // Make sure all our registers are clear
	        DDRB = 0;
	        DDRC = 0;
	        DDRD = 0;

	        PORTB = 0;
	        PORTC = 0;
	        PORTD = 0;

	   // Initialize the LCD
	   lcd_init();
	   //-----------------------------
	   //END MAIN FUNCTION LCD STUFF
	   //------------------------------

	cbi(DDRC, DDC5);
	sbi(PORTC, PC5);


       while(1)
       {
		lcd_line1();
        lcd_string("Psh2Strt");

		while(bit_is_set(PINC, PINC5));//do nothing

	    if (bit_is_clear(PINC, PINC5))
	    _delay_ms(50);
	    while (bit_is_clear(PINC, PINC5))
	    _delay_ms(50);

		lcd_line1();
        lcd_string("Psh2Stop");
		lcd_line2();
        lcd_string("Counting");
	    timebetweenbuttonpresses=0;

	    while bit_is_set(PINC, PINC5)
	    {
		    _delay_ms(1);
		    timebetweenbuttonpresses++;
	    }

	    // A variable to store our converted analog value in:
	       //int pot;

	       // Initialize the ADC:
	      // analog_init();

	       //SETTING PORT OUTPUTS INPUTS: REMEMBER CANT USE SWITCHES AND LCD...
       
	    //   DDRB |= (1 << PB0);      // set buzzer pin PB0 to output
	    //   DDRC |= (1<< PC5);// led breadboard
	    //   DDRB &= ~(1 << PB3);  // make PB3 (button SW3 pin) an input - cant use with LCD
	       //DDRD |= (1<<PD1); //Led output

	       //-----------------------------------------
	       //COMMANDS FOR PRINTING WITH LCD
	       //-----------------------------------------
       
	       //for(;;)
	    //   {
	       //pot = analog10(6);
	    //   if (pot > 100)
	    //{   
	    //PORTC |= (1<< PC5);// turn on breadboard (sonar transmitter)
	          //PORTB |= ( 1 << PB0 );   // buzzer on
	          //PORTD |= (1 << PD1);//LED ON
	          //delayms( 90 );         // delay 50 ms
	          //PORTB &=~ (1 << PB0); //BUZZER Off
	          //PORTC &= ~(1 << PC5);//turn off bread board (sonar transmitter)
          
	          //PORTD &= ~(1 << PD1);       // LED OFF
          
	       //   }

	       //else
       
	       //PORTD &= ~(1 << PD1);
	       //PORTC |= (1<< PC5);
	       //delayms( 45 );
	       //PORTC &= ~(1 << PC5);
       
	          // Startup Banner
          

	       //}

	       //   delay_sec(2);
	       lcd_clear();
	          lcd_line1();
	          lcd_string("time:");
	          lcd_line2();
	          lcd_time(timebetweenbuttonpresses);

	             delay_sec(2);
	       }
       return 1;

    }

I also moved some initialization commands that only need to be called once outside of the main while loop.

As for using the pot to scroll through a menu, yes, it’s absolutely possible. You should have a look at the analog-test example in Orangutan-Lib.

-Adam

very helpful thanks adam, i tried my best to do those things but you can only go so far without a little help…!

Adam, i cant see a fuse in the menu of the config of the microsontroller that even says divide by 8 let alone is selected…

Nope, sorted it! Thats great timed, it against my watch and its accurate! Thank you very much. So now if i want to make that a function to call, from pressing to other jumpers together i can just use standard c syntax, and put it in.

Now just got to get some real push switches…

sorry to post again adam, but i thought i would be able to move the code into a function of its own so that later, when i build a menu i could call it, but the moment i did that the whole thing has stopped working…

    // F_CPU tells util/delay.h our clock frequency
    #define F_CPU 8000000UL   // Orangutan frequency (8MHz)

    #include <avr/io.h>
    #include <util/delay.h>

    uint32_t timebetweenbuttonpresses;

    #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
    #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))

    //ABOVE HERE = STANDARD
       
    //call delay function

    void delayms( uint16_t millis ) {
       while ( millis ) {
          _delay_ms( 1 );
          millis--;
       }
    }

    //--------------------------------
    //All to do with ADC
    //--------------------------------

    void analog_init(void)
    {

       ADMUX = (1 << REFS0);
       ADCSRA = (6 << ADPS0);
       ADCSRA |= (1 << ADEN);
    }


    // Read out the specified analog channel to 10 bits
    unsigned int analog10(unsigned char channel)
    {
       // Clear the channel selection (low 5 bits in ADMUX)
       ADMUX &= ~0x1F;
       // Select the specified channel
       ADMUX |= channel;

       ADCSRA |= (1 << ADSC);

       while (bit_is_set(ADCSRA, ADSC));
       return (ADCL | ADCH << 8);   // read ADC (full 10 bits);
    }

    unsigned int analog8(unsigned char channel)
    {
       return(analog10(channel) >> 2);
    }


    //--------------------------------
    //All to do with ADC
    //--------------------------------

    //-----------------------------------------
    //All to do with LCD......................
    //-----------------------------------------

    #define PORTB_MASK      0x38   // PB3, PB4, PB5
    #define PORTD_MASK      0x80   // PD7

    #define PORTB_SHIFT      3
    #define PORTD_SHIFT      1

    #define LCD_RW         PD3
    #define LCD_RS         PD2
    #define LCD_E         PD4

    #define   LCD_CLEAR      0x01
    #define   LCD_LINE1      0x02
    #define LCD_LINE2      0xC0
    #define LCD_SHOW_BLINK   0x0F
    #define LCD_SHOW_SOLID   0x0E      
    #define LCD_HIDE      0x0C
    #define LCD_CURSOR_L   0x10
    #define LCD_CURSOR_R   0x14
    #define LCD_SHIFT_L      0x18
    #define LCD_SHIFT_R      0x1C


    void lcd_nibble(unsigned char nibble)
    {

       
       nibble &= 0x0F;

       // Shift our nibble so bits 0, 1, and 2 line up with PB3, PB4,
       // and PB5:

       nibble <<= PORTB_SHIFT;

       // Clear those bits out of PORTB so we can write into them:

       PORTB &= ~PORTB_MASK;

       // And load PORTB with those three bits:

       PORTB |= (nibble & PORTB_MASK);

       // Now shift our nibble so bit 3 lines up with PD7:

       nibble <<= PORTD_SHIFT;

       // Clear that bit out of PORTD so we can write into it:

       PORTD &= ~PORTD_MASK;

       // And load pORTD with that last bit:

       PORTD |= (nibble & PORTD_MASK);

       // Delay for 1ms so the LCD can register it's got the nibble:

       _delay_ms(1);



       PORTD |= (1 << LCD_E);
       asm(
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          ::);

       // Bring E low

       PORTD &= ~(1 << LCD_E);

       asm(
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          "nop" "\n\t"
          ::);

       // Our nibble has now been sent to the LCD.
    }



    void lcd_send(unsigned char data)
    {
       unsigned char    temp_ddrb, temp_portb,
                   temp_ddrd, temp_portd;

       // Store our port settings

       temp_ddrb = DDRB;
       temp_portb = PORTB;
       temp_ddrd = DDRD;
       temp_portd = PORTD;

       // Set up port I/O to match what the LCD needs

       DDRB |= PORTB_MASK;
       DDRD |= PORTD_MASK;

       // Send the data

       lcd_nibble(data >> 4);   // High nibble first
       lcd_nibble(data);      // Low nibble second

       // Restore our port settings

       DDRD = temp_ddrd;
       PORTD = temp_portd;
       DDRB = temp_ddrb;
       PORTB = temp_portb;
    }



    void lcd_cmd(unsigned char cmd)
    {
       // Hold RW and RS low

       PORTD &= !((1 << LCD_RW) | (1 << LCD_RS));

       // Send the command

       lcd_send(cmd);

       // Delay for 1ms to let the command process

       _delay_ms(1);
    }

    void lcd_data(unsigned char data)
    {
       // Hold RW low

       PORTD &= ~(1 << LCD_RW);

       // Hold RS high

       PORTD |= (1 << LCD_RS);

       // Send the data.  No waits, so we can go right into sending more
       // data.

       lcd_send(data);
    }

    // lcd_string sends a string to the LCD. 

    void lcd_string(const unsigned char *str)
    {
       while (*str != 0)
          lcd_data(*str++);
    }

    // lcd_int prints an integer. 

    void lcd_int(unsigned char n)
    {
       unsigned char st[4] = {0,0,0,0};

       // The 0x30 addition shifts the decimal number up
       // to the ASCII location of "0".

       // Hundreds place
       st[0] = (n / 100) + 0x30;
       n = n % 100;

       // Tens place
       st[1] = (n / 10) + 0x30;
       n = n % 10;

       // Ones place
       st[2] = n + 0x30;

       // Print it as a string
       lcd_string(st);
    }


    void lcd_time(unsigned long int n)
    {
       unsigned char st[8] = {0,0,0,'.',0,0,0,'s'};

       // The 0x30 addition shifts the decimal number up
       // to the ASCII location of "0".
      
       //100s
       st[0] = (n / 100000) + 0x30;
       n = n % 100000;

       //10s
       st[1] = (n / 10000) + 0x30;
       n = n % 10000;

       // 1s
       st[2] = (n / 1000) + 0x30;
       n = n % 1000;


       // 0.1s
       st[4] = (n / 100) + 0x30;
       n = n % 100;

       // 0.01s
       st[5] = (n / 10) + 0x30;
       n = n % 10;

       // 0.001s
       st[6] = n + 0x30;

       // Print it as a string
       lcd_string(st);
    }


    // lcd_init initializes the LCD and MUST be called prior to
    // every other LCD command.

    void lcd_init(void)
    {

       
       DDRD |= (1 << LCD_RW) | (1 << LCD_RS) | (1 << LCD_E);

       

       _delay_ms(30);
       lcd_cmd(0x30);      // 8-bit mode (wake up!)
       _delay_ms(5);
       lcd_cmd(0x30);      // 8-bit mode (wake up!)
       _delay_ms(1);
       lcd_cmd(0x30);      // 8-bit mode (wake up!)
       _delay_ms(1);
       lcd_cmd(0x32);      // 4-bit mode

       //telling the lcd everything..

       lcd_cmd(0x20);
       lcd_cmd(0x28);      // 4-bit mode, 2-line, 5x8 dots/char
       lcd_cmd(0x08);      // Display off, cursor off, blink off
       lcd_cmd(0x01);      // Clear display
       lcd_cmd(0x0F);      // Display on, cursor on, blink on
       lcd_cmd(0x02);      // Return home
       lcd_cmd(0x01);      // Clear display

       // At this point we're good to go.

    }



    void lcd_moveto(unsigned char line, unsigned char pos)
    {

     

      lcd_cmd((line == 1 ? 0x80 : 0xC0) + pos);
    }



    void lcd_moverel(unsigned char dir, unsigned char num)
    {
       unsigned char cmd;

       cmd = dir ? LCD_CURSOR_R : LCD_CURSOR_L;
       while(num-- > 0)
          lcd_cmd(cmd);
    }



    void lcd_shift(unsigned char dir, unsigned char num)
    {
       unsigned char cmd;

       cmd = dir ? LCD_SHIFT_R : LCD_SHIFT_L;
       while(num-- > 0)
          lcd_cmd(cmd);
    }



    #define lcd_clear()   lcd_cmd(LCD_CLEAR)

    // Move the cursor to the beginning of line 1

    #define lcd_line1() lcd_cmd(LCD_LINE1)

    // Move the cursor to the beginning of line 2

    #define lcd_line2() lcd_cmd(LCD_LINE2)

    // Show the cursor as a blinking block.  (A non-blinking cursor is
    // also available.)

    #define lcd_show() lcd_cmd(LCD_SHOW_BLINK)

    // Hide the cursor.

    #define lcd_hide() lcd_cmd(LCD_HIDE)


    void delay_sec(unsigned char sec)
    {
       unsigned int cycles;


       for(cycles = 0; cycles < (sec * 100); cycles ++)
       {
          _delay_ms(10);
       }
    }



    //-----------------------------------------
    //All to do with LCD......................
    //-----------------------------------------


    //STANDARD MAIN FUNCTION

    int main( void )
    {

      //--------------------------------------
      //LCD STUFF TO GO IN MAIN FUNCTION
      //--------------------------------------
      int i;

           // Make sure all our registers are clear
           DDRB = 0;
           DDRC = 0;
           DDRD = 0;

           PORTB = 0;
           PORTC = 0;
           PORTD = 0;

      // Initialize the LCD
      lcd_init();
      //-----------------------------
      //END MAIN FUNCTION LCD STUFF
      //------------------------------

   timer();

       // A variable to store our converted analog value in:
          //int pot;

          // Initialize the ADC:
         // analog_init();

          //SETTING PORT OUTPUTS INPUTS: REMEMBER CANT USE SWITCHES AND LCD...
       
       //   DDRB |= (1 << PB0);      // set buzzer pin PB0 to output
       //   DDRC |= (1<< PC5);// led breadboard
       //   DDRB &= ~(1 << PB3);  // make PB3 (button SW3 pin) an input - cant use with LCD
          //DDRD |= (1<<PD1); //Led output

          //-----------------------------------------
          //COMMANDS FOR PRINTING WITH LCD
          //-----------------------------------------
       
          //for(;;)
       //   {
          //pot = analog10(6);
       //   if (pot > 100)
       //{   
       //PORTC |= (1<< PC5);// turn on breadboard (sonar transmitter)
             //PORTB |= ( 1 << PB0 );   // buzzer on
             //PORTD |= (1 << PD1);//LED ON
             //delayms( 90 );         // delay 50 ms
             //PORTB &=~ (1 << PB0); //BUZZER Off
             //PORTC &= ~(1 << PC5);//turn off bread board (sonar transmitter)
          
             //PORTD &= ~(1 << PD1);       // LED OFF
          
          //   }

          //else
       
          //PORTD &= ~(1 << PD1);
          //PORTC |= (1<< PC5);
          //delayms( 45 );
          //PORTC &= ~(1 << PC5);
       
             // Startup Banner
          

          //}

          //   delay_sec(2);
          
          
       return 1;

    }




//FUNCTION TO INITITIATE TIMER SEQUENCE
	int timer(void)
	{
	cbi(DDRC, DDC5);
   sbi(PORTC, PC5);


       while(1)
       {
      lcd_line1();
        lcd_string("Psh2Strt");

      while(bit_is_set(PINC, PINC5));//connection not made

       if (bit_is_clear(PINC, PINC5))//connection made
       _delay_ms(50);
       while (bit_is_clear(PINC, PINC5))
       _delay_ms(50);

      lcd_line1();
        lcd_string("Psh2Stop");
      lcd_line2();
        lcd_string("Counting");
       timebetweenbuttonpresses=0;

       while bit_is_set(PINC, PINC5)
       {
          _delay_ms(1);
          timebetweenbuttonpresses++;
       }
	   }

	   lcd_clear();
             lcd_line1();
             lcd_string("time:");
             lcd_line2();
             lcd_time(timebetweenbuttonpresses);

                delay_sec(4);
}

if i want to build a menu in, using the switch statement and a couple more jumper wires, should i do that in the main function, and use the pot to change which option is going to be selected? is that easy enough?

sorry for ignorant questions, this is proving a bigger jump from standard C than i expected, i think i’m learning though :wink:

hey i have been fiddling, and still havent managed to get the whole thing in a seperate function yet but have managed to get it so that it can do two timings in a row, so you touch the wires three times. the second touch stops the last timer and starts a new one - so you could time someone running around a track or something… the only issue i am having is at the end of the second timer i want to display the first lap time on the top line and the second lap time on the bottom line, it works for the bottom line but the first line outputs complete junk litterally.

havent looked at the menu idea anymore yet until i sort this function thing…

I see your function problem, you just need to move the lines that clear and print out the time on the LCD inside the while(1) loop. Right now they’re sitting after the loop’s end brace, so they’re never reached!

-Adam

sorry here’s my latest code WITH an attempt at a function…

        // F_CPU tells util/delay.h our clock frequency
        #define F_CPU 8000000UL   // Orangutan frequency (8MHz)

        #include <avr/io.h>
        #include <util/delay.h>

        uint32_t timebetweenbuttonpresses;
		
		uint32_t secondtimebetweenbuttonpresses;

        #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
        #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))

        //ABOVE HERE = STANDARD
           
        //call delay function

        void delayms( uint16_t millis ) {
           while ( millis ) {
              _delay_ms( 1 );
              millis--;
           }
        }

        //--------------------------------
        //All to do with ADC
        //--------------------------------

        void analog_init(void)
        {

           ADMUX = (1 << REFS0);
           ADCSRA = (6 << ADPS0);
           ADCSRA |= (1 << ADEN);
        }


        // Read out the specified analog channel to 10 bits
        unsigned int analog10(unsigned char channel)
        {
           // Clear the channel selection (low 5 bits in ADMUX)
           ADMUX &= ~0x1F;
           // Select the specified channel
           ADMUX |= channel;

           ADCSRA |= (1 << ADSC);

           while (bit_is_set(ADCSRA, ADSC));
           return (ADCL | ADCH << 8);   // read ADC (full 10 bits);
        }

        unsigned int analog8(unsigned char channel)
        {
           return(analog10(channel) >> 2);
        }


        //--------------------------------
        //All to do with ADC
        //--------------------------------

        //-----------------------------------------
        //All to do with LCD......................
        //-----------------------------------------

        #define PORTB_MASK      0x38   // PB3, PB4, PB5
        #define PORTD_MASK      0x80   // PD7

        #define PORTB_SHIFT      3
        #define PORTD_SHIFT      1

        #define LCD_RW         PD3
        #define LCD_RS         PD2
        #define LCD_E         PD4

        #define   LCD_CLEAR      0x01
        #define   LCD_LINE1      0x02
        #define LCD_LINE2      0xC0
        #define LCD_SHOW_BLINK   0x0F
        #define LCD_SHOW_SOLID   0x0E     
        #define LCD_HIDE      0x0C
        #define LCD_CURSOR_L   0x10
        #define LCD_CURSOR_R   0x14
        #define LCD_SHIFT_L      0x18
        #define LCD_SHIFT_R      0x1C


        void lcd_nibble(unsigned char nibble)
        {

           
           nibble &= 0x0F;

           // Shift our nibble so bits 0, 1, and 2 line up with PB3, PB4,
           // and PB5:

           nibble <<= PORTB_SHIFT;

           // Clear those bits out of PORTB so we can write into them:

           PORTB &= ~PORTB_MASK;

           // And load PORTB with those three bits:

           PORTB |= (nibble & PORTB_MASK);

           // Now shift our nibble so bit 3 lines up with PD7:

           nibble <<= PORTD_SHIFT;

           // Clear that bit out of PORTD so we can write into it:

           PORTD &= ~PORTD_MASK;

           // And load pORTD with that last bit:

           PORTD |= (nibble & PORTD_MASK);

           // Delay for 1ms so the LCD can register it's got the nibble:

           _delay_ms(1);



           PORTD |= (1 << LCD_E);
           asm(
              "nop" "\n\t"
              "nop" "\n\t"
              "nop" "\n\t"
              "nop" "\n\t"
              "nop" "\n\t"
              ::);

           // Bring E low

           PORTD &= ~(1 << LCD_E);

           asm(
              "nop" "\n\t"
              "nop" "\n\t"
              "nop" "\n\t"
              "nop" "\n\t"
              "nop" "\n\t"
              ::);

           // Our nibble has now been sent to the LCD.
        }



        void lcd_send(unsigned char data)
        {
           unsigned char    temp_ddrb, temp_portb,
                       temp_ddrd, temp_portd;

           // Store our port settings

           temp_ddrb = DDRB;
           temp_portb = PORTB;
           temp_ddrd = DDRD;
           temp_portd = PORTD;

           // Set up port I/O to match what the LCD needs

           DDRB |= PORTB_MASK;
           DDRD |= PORTD_MASK;

           // Send the data

           lcd_nibble(data >> 4);   // High nibble first
           lcd_nibble(data);      // Low nibble second

           // Restore our port settings

           DDRD = temp_ddrd;
           PORTD = temp_portd;
           DDRB = temp_ddrb;
           PORTB = temp_portb;
        }



        void lcd_cmd(unsigned char cmd)
        {
           // Hold RW and RS low

           PORTD &= !((1 << LCD_RW) | (1 << LCD_RS));

           // Send the command

           lcd_send(cmd);

           // Delay for 1ms to let the command process

           _delay_ms(1);
        }

        void lcd_data(unsigned char data)
        {
           // Hold RW low

           PORTD &= ~(1 << LCD_RW);

           // Hold RS high

           PORTD |= (1 << LCD_RS);

           // Send the data.  No waits, so we can go right into sending more
           // data.

           lcd_send(data);
        }

        // lcd_string sends a string to the LCD.

        void lcd_string(const unsigned char *str)
        {
           while (*str != 0)
              lcd_data(*str++);
        }

        // lcd_int prints an integer.

        void lcd_int(unsigned char n)
        {
           unsigned char st[4] = {0,0,0,0};

           // The 0x30 addition shifts the decimal number up
           // to the ASCII location of "0".

           // Hundreds place
           st[0] = (n / 100) + 0x30;
           n = n % 100;

           // Tens place
           st[1] = (n / 10) + 0x30;
           n = n % 10;

           // Ones place
           st[2] = n + 0x30;

           // Print it as a string
           lcd_string(st);
        }


        void lcd_time(unsigned long int n)
        {
           unsigned char st[8] = {0,0,0,'.',0,0,0,'s'};

           // The 0x30 addition shifts the decimal number up
           // to the ASCII location of "0".
          
           //100s
           st[0] = (n / 100000) + 0x30;
           n = n % 100000;

           //10s
           st[1] = (n / 10000) + 0x30;
           n = n % 10000;

           // 1s
           st[2] = (n / 1000) + 0x30;
           n = n % 1000;


           // 0.1s
           st[4] = (n / 100) + 0x30;
           n = n % 100;

           // 0.01s
           st[5] = (n / 10) + 0x30;
           n = n % 10;

           // 0.001s
           st[6] = n + 0x30;

           // Print it as a string
           lcd_string(st);
        }


        // lcd_init initializes the LCD and MUST be called prior to
        // every other LCD command.

        void lcd_init(void)
        {

           
           DDRD |= (1 << LCD_RW) | (1 << LCD_RS) | (1 << LCD_E);

           

           _delay_ms(30);
           lcd_cmd(0x30);      // 8-bit mode (wake up!)
           _delay_ms(5);
           lcd_cmd(0x30);      // 8-bit mode (wake up!)
           _delay_ms(1);
           lcd_cmd(0x30);      // 8-bit mode (wake up!)
           _delay_ms(1);
           lcd_cmd(0x32);      // 4-bit mode

           //telling the lcd everything..

           lcd_cmd(0x20);
           lcd_cmd(0x28);      // 4-bit mode, 2-line, 5x8 dots/char
           lcd_cmd(0x08);      // Display off, cursor off, blink off
           lcd_cmd(0x01);      // Clear display
           lcd_cmd(0x0F);      // Display on, cursor on, blink on
           lcd_cmd(0x02);      // Return home
           lcd_cmd(0x01);      // Clear display

           // At this point we're good to go.

        }



        void lcd_moveto(unsigned char line, unsigned char pos)
        {

         

          lcd_cmd((line == 1 ? 0x80 : 0xC0) + pos);
        }



        void lcd_moverel(unsigned char dir, unsigned char num)
        {
           unsigned char cmd;

           cmd = dir ? LCD_CURSOR_R : LCD_CURSOR_L;
           while(num-- > 0)
              lcd_cmd(cmd);
        }



        void lcd_shift(unsigned char dir, unsigned char num)
        {
           unsigned char cmd;

           cmd = dir ? LCD_SHIFT_R : LCD_SHIFT_L;
           while(num-- > 0)
              lcd_cmd(cmd);
        }



        #define lcd_clear()   lcd_cmd(LCD_CLEAR)

        // Move the cursor to the beginning of line 1

        #define lcd_line1() lcd_cmd(LCD_LINE1)

        // Move the cursor to the beginning of line 2

        #define lcd_line2() lcd_cmd(LCD_LINE2)

        // Show the cursor as a blinking block.  (A non-blinking cursor is
        // also available.)

        #define lcd_show() lcd_cmd(LCD_SHOW_BLINK)

        // Hide the cursor.

        #define lcd_hide() lcd_cmd(LCD_HIDE)


        void delay_sec(unsigned char sec)
        {
           unsigned int cycles;


           for(cycles = 0; cycles < (sec * 100); cycles ++)
           {
              _delay_ms(10);
           }
        }



        //-----------------------------------------
        //All to do with LCD......................
        //-----------------------------------------


        //STANDARD MAIN FUNCTION

        int main( void )
        {

          //--------------------------------------
          //LCD STUFF TO GO IN MAIN FUNCTION
          //--------------------------------------
          int i;

               // Make sure all our registers are clear
               DDRB = 0;
               DDRC = 0;
               DDRD = 0;

               PORTB = 0;
               PORTC = 0;
               PORTD = 0;

          // Initialize the LCD
          lcd_init();
          //-----------------------------
          //END MAIN FUNCTION LCD STUFF
          //------------------------------

       cbi(DDRC, DDC5);
       sbi(PORTC, PC5);


           while(1)
           {
          timer();			  

			   } 
				    //delay_sec(2);

           // A variable to store our converted analog value in:
              //int pot;

              // Initialize the ADC:
             // analog_init();

              //SETTING PORT OUTPUTS INPUTS: REMEMBER CANT USE SWITCHES AND LCD...
           
           //   DDRB |= (1 << PB0);      // set buzzer pin PB0 to output
           //   DDRC |= (1<< PC5);// led breadboard
           //   DDRB &= ~(1 << PB3);  // make PB3 (button SW3 pin) an input - cant use with LCD
              //DDRD |= (1<<PD1); //Led output

              //-----------------------------------------
              //COMMANDS FOR PRINTING WITH LCD
              //-----------------------------------------
           
              //for(;;)
           //   {
              //pot = analog10(6);
           //   if (pot > 100)
           //{   
           //PORTC |= (1<< PC5);// turn on breadboard (sonar transmitter)
                 //PORTB |= ( 1 << PB0 );   // buzzer on
                 //PORTD |= (1 << PD1);//LED ON
                 //delayms( 90 );         // delay 50 ms
                 //PORTB &=~ (1 << PB0); //BUZZER Off
                 //PORTC &= ~(1 << PC5);//turn off bread board (sonar transmitter)
             
                 //PORTD &= ~(1 << PD1);       // LED OFF
             
              //   }

              //else
           
              //PORTD &= ~(1 << PD1);
              //PORTC |= (1<< PC5);
              //delayms( 45 );
              //PORTC &= ~(1 << PC5);
           
                 // Startup Banner
             

              //}

              //   delay_sec(2);
              
           return 1;

        }

int timer(void)
{
while(1)
{
lcd_line1();
            lcd_string("Psh2Strt");

		lcd_line2();
            lcd_string("");
//first lap time
          while(bit_is_set(PINC, PINC5));//do nothing

           if (bit_is_clear(PINC, PINC5))
           _delay_ms(50);
           while (bit_is_clear(PINC, PINC5))
           _delay_ms(50);

          lcd_line1();
            lcd_string("1st lap");
          lcd_line2();
            lcd_string("Counting");
           timebetweenbuttonpresses=0;

           while bit_is_set(PINC, PINC5)
           {
              _delay_ms(1);
              timebetweenbuttonpresses++;
           }

			
			lcd_clear();
                 lcd_line1();
                 lcd_string("1sttime:");
                 lcd_line2();
                 lcd_time(timebetweenbuttonpresses);

                    //delay_sec(2);
              

//attempt at second lap

			  
           while (bit_is_clear(PINC, PINC5))
           _delay_ms(50);

          lcd_line1();
            lcd_string("2nd lap");
          lcd_line2();
            lcd_string("Counting");
           secondtimebetweenbuttonpresses=0;

           while bit_is_set(PINC, PINC5)
           {
              _delay_ms(1);
              secondtimebetweenbuttonpresses++;
           }

		   lcd_clear();
                 lcd_line1();
                 lcd_string("2nd time:");
                 lcd_line2();
                 lcd_time(secondtimebetweenbuttonpresses);

              delay_sec(2);
			  
			  lcd_clear();
                 lcd_line1();
                 lcd_string(timebetweenbuttonpresses);
                 lcd_line2();
                 lcd_time(secondtimebetweenbuttonpresses);
			delay_sec(6);
			}
			}

When you try to display both times at the end you’re using:
lcd_string(timebetweenbuttonpresses);
When you should be using:
lcd_time(timebetweenbuttonpresses);

Other than that it seems like you’ve got it working great, did you spot the earlier message about what’s wrong with the function version of your code?

-Adam

thanks adam, sorry about another question, but i cant get this case menu thing working, is it just the same as normal C?

i never noticed the difference there between time and string! cheers there. If i put another couple of my crude push button jumpers on PC4, could i, along side that timer have another timer going… so that even if you start one first you could finish the other first… i dont know what this is called - interupts or something, because i know C flows down the code, so would it struggle with two things happening spontaneously? Would i put the two pieces of code together, or as seperate functions?

HI Thanks for help.

here is my code again, with me trying to put a witch case menu in, i;m struggling with how the user selects an option, i havent got to the pot changing the option yet just how to select it. if you do a find for this is where i’m struggling" you’ll see:

        // F_CPU tells util/delay.h our clock frequency
        #define F_CPU 8000000UL   // Orangutan frequency (8MHz)

        #include <avr/io.h>
        #include <util/delay.h>

        uint32_t timebetweenbuttonpresses;
		
		uint32_t secondtimebetweenbuttonpresses;

        #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
        #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))

        //ABOVE HERE = STANDARD
           
        //call delay function

        void delayms( uint16_t millis ) {
           while ( millis ) {
              _delay_ms( 1 );
              millis--;
           }
        }

        //--------------------------------
        //All to do with ADC
        //--------------------------------

        void analog_init(void)
        {

           ADMUX = (1 << REFS0);
           ADCSRA = (6 << ADPS0);
           ADCSRA |= (1 << ADEN);
        }


        // Read out the specified analog channel to 10 bits
        unsigned int analog10(unsigned char channel)
        {
           // Clear the channel selection (low 5 bits in ADMUX)
           ADMUX &= ~0x1F;
           // Select the specified channel
           ADMUX |= channel;

           ADCSRA |= (1 << ADSC);

           while (bit_is_set(ADCSRA, ADSC));
           return (ADCL | ADCH << 8);   // read ADC (full 10 bits);
        }

        unsigned int analog8(unsigned char channel)
        {
           return(analog10(channel) >> 2);
        }


        //--------------------------------
        //All to do with ADC
        //--------------------------------

        //-----------------------------------------
        //All to do with LCD......................
        //-----------------------------------------

        #define PORTB_MASK      0x38   // PB3, PB4, PB5
        #define PORTD_MASK      0x80   // PD7

        #define PORTB_SHIFT      3
        #define PORTD_SHIFT      1

        #define LCD_RW         PD3
        #define LCD_RS         PD2
        #define LCD_E         PD4

        #define   LCD_CLEAR      0x01
        #define   LCD_LINE1      0x02
        #define LCD_LINE2      0xC0
        #define LCD_SHOW_BLINK   0x0F
        #define LCD_SHOW_SOLID   0x0E     
        #define LCD_HIDE      0x0C
        #define LCD_CURSOR_L   0x10
        #define LCD_CURSOR_R   0x14
        #define LCD_SHIFT_L      0x18
        #define LCD_SHIFT_R      0x1C


        void lcd_nibble(unsigned char nibble)
        {

           
           nibble &= 0x0F;

           // Shift our nibble so bits 0, 1, and 2 line up with PB3, PB4,
           // and PB5:

           nibble <<= PORTB_SHIFT;

           // Clear those bits out of PORTB so we can write into them:

           PORTB &= ~PORTB_MASK;

           // And load PORTB with those three bits:

           PORTB |= (nibble & PORTB_MASK);

           // Now shift our nibble so bit 3 lines up with PD7:

           nibble <<= PORTD_SHIFT;

           // Clear that bit out of PORTD so we can write into it:

           PORTD &= ~PORTD_MASK;

           // And load pORTD with that last bit:

           PORTD |= (nibble & PORTD_MASK);

           // Delay for 1ms so the LCD can register it's got the nibble:

           _delay_ms(1);



           PORTD |= (1 << LCD_E);
           asm(
              "nop" "\n\t"
              "nop" "\n\t"
              "nop" "\n\t"
              "nop" "\n\t"
              "nop" "\n\t"
              ::);

           // Bring E low

           PORTD &= ~(1 << LCD_E);

           asm(
              "nop" "\n\t"
              "nop" "\n\t"
              "nop" "\n\t"
              "nop" "\n\t"
              "nop" "\n\t"
              ::);

           // Our nibble has now been sent to the LCD.
        }



        void lcd_send(unsigned char data)
        {
           unsigned char    temp_ddrb, temp_portb,
                       temp_ddrd, temp_portd;

           // Store our port settings

           temp_ddrb = DDRB;
           temp_portb = PORTB;
           temp_ddrd = DDRD;
           temp_portd = PORTD;

           // Set up port I/O to match what the LCD needs

           DDRB |= PORTB_MASK;
           DDRD |= PORTD_MASK;

           // Send the data

           lcd_nibble(data >> 4);   // High nibble first
           lcd_nibble(data);      // Low nibble second

           // Restore our port settings

           DDRD = temp_ddrd;
           PORTD = temp_portd;
           DDRB = temp_ddrb;
           PORTB = temp_portb;
        }



        void lcd_cmd(unsigned char cmd)
        {
           // Hold RW and RS low

           PORTD &= !((1 << LCD_RW) | (1 << LCD_RS));

           // Send the command

           lcd_send(cmd);

           // Delay for 1ms to let the command process

           _delay_ms(1);
        }

        void lcd_data(unsigned char data)
        {
           // Hold RW low

           PORTD &= ~(1 << LCD_RW);

           // Hold RS high

           PORTD |= (1 << LCD_RS);

           // Send the data.  No waits, so we can go right into sending more
           // data.

           lcd_send(data);
        }

        // lcd_string sends a string to the LCD.

        void lcd_string(const unsigned char *str)
        {
           while (*str != 0)
              lcd_data(*str++);
        }

        // lcd_int prints an integer.

        void lcd_int(unsigned char n)
        {
           unsigned char st[4] = {0,0,0,0};

           // The 0x30 addition shifts the decimal number up
           // to the ASCII location of "0".

           // Hundreds place
           st[0] = (n / 100) + 0x30;
           n = n % 100;

           // Tens place
           st[1] = (n / 10) + 0x30;
           n = n % 10;

           // Ones place
           st[2] = n + 0x30;

           // Print it as a string
           lcd_string(st);
        }


        void lcd_time(unsigned long int n)
        {
           unsigned char st[8] = {0,0,0,'.',0,0,0,'s'};

           // The 0x30 addition shifts the decimal number up
           // to the ASCII location of "0".
          
           //100s
           st[0] = (n / 100000) + 0x30;
           n = n % 100000;

           //10s
           st[1] = (n / 10000) + 0x30;
           n = n % 10000;

           // 1s
           st[2] = (n / 1000) + 0x30;
           n = n % 1000;


           // 0.1s
           st[4] = (n / 100) + 0x30;
           n = n % 100;

           // 0.01s
           st[5] = (n / 10) + 0x30;
           n = n % 10;

           // 0.001s
           st[6] = n + 0x30;

           // Print it as a string
           lcd_string(st);
        }


        // lcd_init initializes the LCD and MUST be called prior to
        // every other LCD command.

        void lcd_init(void)
        {

           
           DDRD |= (1 << LCD_RW) | (1 << LCD_RS) | (1 << LCD_E);

           

           _delay_ms(30);
           lcd_cmd(0x30);      // 8-bit mode (wake up!)
           _delay_ms(5);
           lcd_cmd(0x30);      // 8-bit mode (wake up!)
           _delay_ms(1);
           lcd_cmd(0x30);      // 8-bit mode (wake up!)
           _delay_ms(1);
           lcd_cmd(0x32);      // 4-bit mode

           //telling the lcd everything..

           lcd_cmd(0x20);
           lcd_cmd(0x28);      // 4-bit mode, 2-line, 5x8 dots/char
           lcd_cmd(0x08);      // Display off, cursor off, blink off
           lcd_cmd(0x01);      // Clear display
           lcd_cmd(0x0F);      // Display on, cursor on, blink on
           lcd_cmd(0x02);      // Return home
           lcd_cmd(0x01);      // Clear display

           // At this point we're good to go.

        }



        void lcd_moveto(unsigned char line, unsigned char pos)
        {

         

          lcd_cmd((line == 1 ? 0x80 : 0xC0) + pos);
        }



        void lcd_moverel(unsigned char dir, unsigned char num)
        {
           unsigned char cmd;

           cmd = dir ? LCD_CURSOR_R : LCD_CURSOR_L;
           while(num-- > 0)
              lcd_cmd(cmd);
        }



        void lcd_shift(unsigned char dir, unsigned char num)
        {
           unsigned char cmd;

           cmd = dir ? LCD_SHIFT_R : LCD_SHIFT_L;
           while(num-- > 0)
              lcd_cmd(cmd);
        }



        #define lcd_clear()   lcd_cmd(LCD_CLEAR)

        // Move the cursor to the beginning of line 1

        #define lcd_line1() lcd_cmd(LCD_LINE1)

        // Move the cursor to the beginning of line 2

        #define lcd_line2() lcd_cmd(LCD_LINE2)

        // Show the cursor as a blinking block.  (A non-blinking cursor is
        // also available.)

        #define lcd_show() lcd_cmd(LCD_SHOW_BLINK)

        // Hide the cursor.

        #define lcd_hide() lcd_cmd(LCD_HIDE)


        void delay_sec(unsigned char sec)
        {
           unsigned int cycles;


           for(cycles = 0; cycles < (sec * 100); cycles ++)
           {
              _delay_ms(10);
           }
        }



        //-----------------------------------------
        //All to do with LCD......................
        //-----------------------------------------


        //STANDARD MAIN FUNCTION

        int main( void )
        {

          //--------------------------------------
          //LCD STUFF TO GO IN MAIN FUNCTION
          //--------------------------------------
          int i;

               // Make sure all our registers are clear
               DDRB = 0;
               DDRC = 0;
               DDRD = 0;

               PORTB = 0;
               PORTC = 0;
               PORTD = 0;

          // Initialize the LCD
          lcd_init();
          //-----------------------------
          //END MAIN FUNCTION LCD STUFF
          //------------------------------

       cbi(DDRC, DDC5);
       sbi(PORTC, PC5);


           while(1)
           {
		   switch(menu())
         {
            case 1: /*Adding the data to the program*/
                        {
                            
                            
                            lcd_line1();
            				lcd_string("strttmr");
                            timer();
                            break;
                        }
            
         }  /* End of switch */
    }       /* End of while  */

          

			   
				    //delay_sec(2);

           // A variable to store our converted analog value in:
              //int pot;

              // Initialize the ADC:
             // analog_init();

              //SETTING PORT OUTPUTS INPUTS: REMEMBER CANT USE SWITCHES AND LCD...
           
           //   DDRB |= (1 << PB0);      // set buzzer pin PB0 to output
           //   DDRC |= (1<< PC5);// led breadboard
           //   DDRB &= ~(1 << PB3);  // make PB3 (button SW3 pin) an input - cant use with LCD
              //DDRD |= (1<<PD1); //Led output

              //-----------------------------------------
              //COMMANDS FOR PRINTING WITH LCD
              //-----------------------------------------
           
              //for(;;)
           //   {
              //pot = analog10(6);
           //   if (pot > 100)
           //{   
           //PORTC |= (1<< PC5);// turn on breadboard (sonar transmitter)
                 //PORTB |= ( 1 << PB0 );   // buzzer on
                 //PORTD |= (1 << PD1);//LED ON
                 //delayms( 90 );         // delay 50 ms
                 //PORTB &=~ (1 << PB0); //BUZZER Off
                 //PORTC &= ~(1 << PC5);//turn off bread board (sonar transmitter)
             
                 //PORTD &= ~(1 << PD1);       // LED OFF
             
              //   }

              //else
           
              //PORTD &= ~(1 << PD1);
              //PORTC |= (1<< PC5);
              //delayms( 45 );
              //PORTC &= ~(1 << PC5);
           
                 // Startup Banner
             

              //}

              //   delay_sec(2);
              
           return 1;

        }

int timer(void)
{
while(1)
{
		
	
		lcd_line1();
            lcd_string("Psh2Strt");

	
//first lap time
          while(bit_is_set(PINC, PINC5));//do nothing

           if (bit_is_clear(PINC, PINC5))
           _delay_ms(50);
           while (bit_is_clear(PINC, PINC5))
           _delay_ms(50);

          lcd_clear();
		  lcd_line1();
            lcd_string("1");
          lcd_line2();
            lcd_string("Counting");
           timebetweenbuttonpresses=0;

           while bit_is_set(PINC, PINC5)
           {
              _delay_ms(1);
              timebetweenbuttonpresses++;
           }

			
			lcd_clear();
                

//attempt at second lap

			  
           while (bit_is_clear(PINC, PINC5))
           _delay_ms(50);

          lcd_clear();
		  lcd_line1();
            lcd_string("2");
          lcd_line2();
            lcd_string("Counting");
           secondtimebetweenbuttonpresses=0;

           while bit_is_set(PINC, PINC5)
           {
              _delay_ms(1);
              secondtimebetweenbuttonpresses++;
           }

		   lcd_clear();
                
			  
		//final results
		
			  lcd_clear();
                 lcd_line1();
                 lcd_time(timebetweenbuttonpresses);
                 lcd_line2();
                 lcd_time(secondtimebetweenbuttonpresses);
			delay_sec(6);
		lcd_clear();
		
		/*while end*/}

		/* funct end*/	}





/* Displays a menu and inputs user's selection back into the main function */
int menu(void)
{
    int reply;

//this is where i'm struggling... what do i do when the user presses jumpers on PC5 to choose the option

    lcd_line1();
     lcd_string("option 1");

    scanf("%d", &reply);
    delay1();
    return (reply); /*Sends the reply to the main function*/
}

Aah, I see. Your AVR has no standard input stream (i.e. a command line listening for characters from a PS2 keyboard), so the scanf statement isn’t really applicable. Sine WinAVR is built on top of GCC, it still recognizes the command, and tries to compile it into something meaningful in AVR machine code, but there’s nothing useful for it to do.

It’s trying really hard though! If you comment out that one command and recompile the code the resulting hex file is just about cut in half!

-Adam

hey adam, i dont know whats going on, often when i post it says succesful then check later and its not there. anyway. thanks for sorting that, i never noticed the string/time difference…

my code is below and is now working, thanks to you. Lets say now i wanted to control two timers, doing the same thing. set them both off together, but one might end the first lap before the other, so touch its set of jumpers and the microcontroller would stop and restart its timer, then touch the other set and the microcontroller would stop and reset that one, but then reverse it for the second time.

basically can i set two timers, and when touch their individual jumpers it controls its timer regardless of wheter the other timer seqence has finished???

alex