NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » indexing code with a button push help

October 18, 2012
by jfreethy
jfreethy's Avatar

Hello everyone, In my project I would like to index each line of code every time I press a button.i.e press the button execute the first output, press it again then more to the second output, and so on. Here is the section of code in question;

if (fire() == 0){

PORTC |= (1<<PC4); //TURN LED's ON
    delay_ms(30);   
    PORTC &= ~(1<<PC4);
    // turn off led

    {PORTC |= (1<<PC3);
    delay_ms(30);
    PORTC &= ~(1<<PC3);

    PORTC |= (1<<PC2);
        delay_ms(30);
    PORTC &= ~(1<<PC2);

    PORTC |= (1<<PC1);
    delay_ms(30);   
    PORTC &= ~(1<<PC1);

    PORTC |= (1<<PC0);
        delay_ms(30);
    PORTC &= ~(1<<PC0);

    PORTB |= (1<<PB5);
        delay_ms(30);
    PORTB &= ~(1<<PB5);

    PORTB |= (1<<PB4);
        delay_ms(30);
    PORTB &= ~(1<<PB4);

    PORTB |= (1<<PB3);
        delay_ms(30);
    PORTB &= ~(1<<PB3);

    PORTB |= (1<<PB2);
        delay_ms(30);
    PORTB &= ~(1<<PB2);

    PORTB |= (1<<PB1);
        delay_ms(30);
    PORTB &= ~(1<<PB1);

    PORTB |= (1<<PB0);
        delay_ms(30);
    PORTB &= ~(1<<PB0);

prusmably I think feel that I should be using some sorta switch function but I do not know how :), and have not seen an example that I could understand.

please let me know if you need the full code or if you might have a soulution.

thank you, Jason

October 18, 2012
by pcbolt
pcbolt's Avatar

Jason -

Yes a switch would be the way to go. Here is an example:

switch (test_variable){  // "test_variable" would be incremented by button push
  case 1:                // if "test_variable" == 1
    PORTB |= (1<<PB1);
    break;               // jump to the end of switch code block
  case 2:                // if "test_variable" == 2
    PORTB |= (1<<PB2);
    break;               // jump to the end of switch code block
  default:               // if "test_variable" is does not match any other case
    PORTB |= (1<<PB3);
}                        // end of switch code block

You could use "if" statements but that can get messy. Or if you are really brave you could use function pointers.

October 18, 2012
by jfreethy
jfreethy's Avatar

would I have to turn off the previous function or would the break function do that for me? p.s. thank you for your help!

jason

October 18, 2012
by pcbolt
pcbolt's Avatar

Jason -

The "break" statement just takes you to the end of the code block. If you want to execute your code like in your first post each "case" statement would look like:

case 1:
  PORTC |= (1<<PC4);
  delay_ms(30);
  PORTC &= ~(1<<PC4);
  break;
case 2: // etc
October 18, 2012
by jfreethy
jfreethy's Avatar

thank you again. I finaly got it wourking :) cheers

Post a Reply

Please log in to post a reply.

Did you know that NerdKits make a great gift? Learn more...