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.

Microcontroller Programming » If Statement in a State Machine

April 14, 2009
by n3ueaEMTP
n3ueaEMTP's Avatar

I trying to build a state machine that keeps my garage door open for a certain amount of time & then closes the door. I want to be able to have a slide switch that changes the timeout depending on the time of year (summer & winter). So, my question is: can I have an if/else statement inside one of my cases (the state machine does other things as well)? Thanks in advance for any help!

Here is the code:

case STATE_SHAKE:

 state = STATE_LIGHT;

 set_lights(0x20);

{if((PINC & (1<<PC3)) == 0)

    timeout = summer;

else    timeout = winter;}  //LIGHT TIME

    open = 0;

    break;

Chris B.

April 14, 2009
by wayward
wayward's Avatar

Hi Chris,

you certainly can do this in C, even without the braces. Any statement goes inside the switch statement. Here's another to write your if block to the same effect:

if (PINC & (1<<PC3)) {
  timeout = winter;    // winter timeout when PC3 is high
else
  timeout = summer;    // summer timeout when PC3 is low

Or even this -- some like it hot, some like it not :)

timeout = (PINC & (1<<PC3)) ? winter : summer;
April 14, 2009
by wayward
wayward's Avatar

I left out one word and had an extraneous brace in the previous post. Go figure.

April 15, 2009
by n3ueaEMTP
n3ueaEMTP's Avatar

Wayward, Thanks for your help. I used your changes in my project and it still didn't work. After looking over the code for about an hour, I realized that I had:

// enable internal pullup resistor on PC3 (the winter/summer switch)

PORTB |= (1<<PC3);

instead of:

// enable internal pullup resistor on PC3 (the winter/summer switch)

PORTC |= (1<<PC3);

One letter makes a big difference. I'm still using your suggestion for the code and after that change, it worked like a charm. Hopefully another n00b can learn from my mistake!

Chris B.

April 16, 2009
by ranger
ranger's Avatar

...speaking of learning from others' mistakes... while looking for that one little typo, DO NOT punch a wall. Walls have very hard, very solid studs that do not make good punching. I recommend getting up, getting a drink/snack, maybe go for a 5 minute walk.

April 29, 2009
by BobaMosfet
BobaMosfet's Avatar

I agree with Ranger. I find that sometimes, just printing out my code on paper and going outside or elsewhere can help free the mind. It also gives me a place to jot notes while I'm examining the code. Monitors can be confining (in some weird way) when debugging, sometimes.

Have fun!

May 04, 2009
by brian
brian's Avatar

I think a nice way to be able to test code would be to have a software emulator. Since we're writing in C, it would be a little more difficult than if it was a higher level language, but the ATmega is a simple chip, so it wouldn't be hard to do for anyone with the knowledge. I wonder if it has already been done... * goes to search online *

May 06, 2009
by TBNK
TBNK's Avatar

mikroElektronika has a C compiler for AVR with a full IDE (Integrated Development Environment), including a software simulator, debugging breakpoints, the full nine yards at http://www.mikroe.com/en/compilers/mikroc/avr/

The IDE runs on Windows only, but it cross-compiles for the ATmega168, among others.

A "fully functional free Demo version" is available (limited to a 2K program size).

Pricey at $249 for the full version, but you asked about one.

May 06, 2009
by TBNK
TBNK's Avatar

Hold on! At first glance, Atmel's IDE seemed to only have an Assembler, not a C compiler, but its brochure (PDF) says it has both.

And Atmel's IDE is free! Gotta be worth a look.

May 06, 2009
by wayward
wayward's Avatar

heh, MikroElektronika is a Serbian magazine :) Let me know if you need something translated.

Post a Reply

Please log in to post a reply.

Did you know that SPDT stands for "Single Pole, Double Throw"? Learn more...