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 ![]() |
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:
Chris B. |
---|---|
April 14, 2009 by wayward ![]() |
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:
Or even this -- some like it hot, some like it not :)
|
April 14, 2009 by wayward ![]() |
I left out one word and had an extraneous brace in the previous post. Go figure. |
April 15, 2009 by n3ueaEMTP ![]() |
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 ![]() |
...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 ![]() |
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 ![]() |
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 ![]() |
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 ![]() |
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 ![]() |
heh, MikroElektronika is a Serbian magazine :) Let me know if you need something translated. |
Please log in to post a reply.
Did you know that a microcontroller can measure an RC time constant? Learn more...
|