WHILE() in a SWITCH/CASE

Go To Last Post
34 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Question is simply....Can I put a WHILE(){}  in a CASE?

 

Heres the scope.  I have to monitor a charging IC whose status is given on two outputs pins.  I want to do the following:

 

uint8_t charger_status = (pind & 0x03);

switch(charger_status)
   {
       case 0x00:
       {
           while((pind & 0x03) == 0)
           {
               //do something
           }
       }break;
       
       case 0x01:
       {
           while((pind & 0x03) == 1)
           {
               //do something
           }
       }break;
       
       case 0x02:
       {
           while((pind & 0x03) == 2)
           {
               //do something
           }
       }break;
       
       case 0x03:
       {
           while((pind & 0x03) == 3)
           {
               //do something
           }
       }break;
       
       default:
       {
           
       }break;
   }

Reasoning for this is that I would think that this would be far more efficient than running the switch over and over to hit the same case until the inputs change.  The way I see the above is the code sits in the while() and when the inputs change the code exits the switch and then starts over on the next pass through.  There is a Timer Interrupt involved that is common to all the cases but that will not affect the operation from what I see.

 

Any thoughts on this?

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I do it. Not frequently. But, one situation is a finite state machine managed with a switch/case list. 

 

In some of those, I check the status of a received or transmitted async serial character using while( ). I've not seen any evidence that it does not work.

 

Why would you suspect that it is problematic?

 

West Coast Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
Why would you suspect that it is problematic?

I didn't think it would be problematic, I am just asking if any of the C gurus here can see any potential pitfall. 

 

Good to see someone else has done this before and with success!

 

East Coast Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Seems like a simple switch inside of a while(1) will achieve the same result.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

/pedantic-on

joeymorin wrote:
... while(1) ...
apparently one can be confused by 1 and l per K&R.

https://barrgroup.com/Embedded-Systems/Books/Embedded-C-Coding-Standard/Statement-Rules/For-While-Loops#footnote15

/pedantic-off

cheeky

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
Can I put a WHILE(){}  in a CASE?

Yes. 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
      }break;

It doesn't matter but why is the break outside the statement block? Personally I would have gone for:

       case 0x00:
       {
           while((pind & 0x03) == 0)
           {
               //do something
           }
           break;
       }
etc.

 

PS why "pind" and not "PIND" ??

Last Edited: Mon. Sep 3, 2018 - 08:56 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In fact, why are the outer braces there at all?

 

It just needs to be

       case 0x00:
           while((pind & 0x03) == 0)
           {
               //do something
           }
           break;

etc.

 

Again, it doesn't actually matter here.

 

 

EDIT - also doesn't matter to the question at hand, but ...

 

Presumably, the code shown is just for illustration, but I'd prefer those pin & state values to be symbolically named - would also help to highlight that you're looking for the status to stay the same:

#define CHARGER_STATUS (pind & 0x03)

switch( CHARGER_STATUS )
   {
       case STATUS_X:
           while( CHARGER_STATUS  == STATUS_X )
           {
               //do STATUS_X stuff
           }
           break;

       case STATUS_Y:
           while( CHARGER_STATUS  == STATUS_Y )
           {
               //do STATUS_Y stuff
           }
           break;  

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. Sep 3, 2018 - 08:39 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The 'C' syntax/semantics is fine.

 

Whether it's a good way to handle the charger is another question ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joeymorin wrote:
Seems like a simple switch inside of a while(1) will achieve the same result.

Yes, but

in the OP, jgmdesign wrote:
I would think that this would be far more efficient than running the switch over and over to hit the same case until the inputs change

 

But that's another question - see #9 ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Why is there a switch/case at all? Couldn't you just have a series of while loops?

 

           while((pind & 0x03) == 0)
           {
               //do something
           }

           while((pind & 0x03) == 1)
           {
               //do something
           }

           while((pind & 0x03) == 2)
           {
               //do something
           }

           while((pind & 0x03) == 3)
           {
               //do something
           }

 

note: I'm aware the code is not equivalent, so it may or may not be possible to do this.

Last Edited: Mon. Sep 3, 2018 - 12:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How is that equivalent? In the switch() structure only one case: so one while() loop will be executed while your code has all four consecutively.

 

However I do wonder why a switch() is needed too. Presumably:

uint8_t charger_status = (pind & 0x03);

while((pind & 0x03) == charger_status)
{
   //do something
}

would cater for all the cases in simpler code?

 

EDIT: ah ha - you edited in your additional "not equivalent" comment while I was typing that ;-)

Last Edited: Mon. Sep 3, 2018 - 12:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
EDIT: ah ha - you edited in your additional "not equivalent" comment while I was typing that ;-)

 

Yeah, I had a feeling someone was typing a comment along those lines...

My thinking was that maybe this was all inside an infinite loop so it would not make much difference, since you would be constantly checking charger_status.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So now we're well off the original "is it allowed" question and into the (unrelated, but obvious) follow-on: "is it a good way to do it" question ...

 

See #9

 

laugh

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is it legal? Yes. Should you do it? I wouod only do it for a test scenario, or for a really simple application. Having the switch hit the same case repeatedly until the input changes is probably not going to cause any noticeable speed reduction, but tying up the processor in a while loop doesn't make much sense to me. I alway try to write non-blocking code if possible, although I confess that I sometimes use blocking UART send or I2C functions.

Quebracho seems to be the hardest wood.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

John_A_Brown wrote:
tying up the processor in a while loop doesn't make much sense to me. 

Indeed. Especially for a battery status - which does not tend to change frequently or rapidly.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the feedback.  Lemme answer a few of the posts:

 

joeymorin wrote:
Seems like a simple switch inside of a while(1) will achieve the same result.

Yes, but there is more to this than I am letting on.  So as to not make this a bigger thread then need be I kept with what is always asked for which is compact code relevant to the question.  Which is can I do this, or is there a possible problem.  You are correct I can/could just keep scanning with a while(1)/switch, and I may end up doing just that, but for now this is what i am looking at doing.

 

awneil wrote:

jgmdesign wrote:
Can I put a WHILE(){}  in a CASE?

Yes. 

Perfect!!

 

clawson wrote:

      }break;

It doesn't matter but why is the break outside the statement block? Personally I would have gone for:

       case 0x00:
       {
           while((pind & 0x03) == 0)
           {
               //do something
           }
           break;
       }
etc.

 

PS why "pind" and not "PIND" ??

I wrote this at midnight last night and I was pretty tired.  You are correct on both points.

 

awneil wrote:
EDIT - also doesn't matter to the question at hand, but ... Presumably, the code shown is just for illustration, but I'd prefer those pin & state values to be symbolically named - would also help to highlight that you're looking for the status to stay the same:

You're right it doesn't matter.  And the numbers 0..3 are pretty obvious that they are not going to change.  And if I had used 'symbols' then more questions as to what those symbols are/mean/etc....makes for long thread with posts for nothing.

 

awneil wrote:

The 'C' syntax/semantics is fine.

 

Whether it's a good way to handle the charger is another question ...

Yup, as noted in #6, Thanks. the Charger is not the concern.  The syntax is.  I should have kept the application out of the OP.  OOPS!

 

awneil wrote:

joeymorin wrote:
Seems like a simple switch inside of a while(1) will achieve the same result.

Yes, but

in the OP, jgmdesign wrote:
I would think that this would be far more efficient than running the switch over and over to hit the same case until the inputs change

 

But that's another question - see #9 ...

Ahh yes! BUT!!  IF the inputs change during the time that the WHILE in the CASE is running then the case will BREAK and the whole thing runs again.  Which then brings us back to joeys comment about just running the switch over and over anyway....Sort of like the Chicken or the egg first.

 

clawson wrote:

How is that equivalent? In the switch() structure only one case: so one while() loop will be executed while your code has all four consecutively.

 

However I do wonder why a switch() is needed too. Presumably:

uint8_t charger_status = (pind & 0x03);

while((pind & 0x03) == charger_status)
{
   //do something
}

would cater for all the cases in simpler code?

 

EDIT: ah ha - you edited in your additional "not equivalent" comment while I was typing that ;-)

This is for both Cliff and El Tangas...The charger IC outputs on two pins four binary states - 0..3 to indicate what is going on.  For each one of those states I plan on flashing an RGB LED in some color pattern or whatever.  Hence the four cases and in each case - which corresponds to the condition reported by the IC the task for the AVR to perform.

 

awneil wrote:

So now we're well off the original "is it allowed" question and into the (unrelated, but obvious) follow-on: "is it a good way to do it" question ...

 

See #9

 

laugh

Actually #6, and if West Coast Jim's ego feels bruised he did chimein first in #2,  but thats for discussion in another thread.wink

 

John_A_Brown wrote:
Is it legal? Yes. Should you do it? I wouod only do it for a test scenario, or for a really simple application. Having the switch hit the same case repeatedly until the input changes is probably not going to cause any noticeable speed reduction, but tying up the processor in a while loop doesn't make much sense to me. I alway try to write non-blocking code if possible, although I confess that I sometimes use blocking UART send or I2C functions.

It is actually a very simple application.  In fact that code posted is just about all of it, sans the //do something, and the init.  While the unit is charging ALL the other functions are not used for the device.

 

Thanks all!

 

jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

Last Edited: Tue. Sep 4, 2018 - 02:05 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Go for legibility.

Methinks efficiency will not be much of an issue here.

"SCSI is NOT magic. There are *fundamental technical
reasons* why it is necessary to sacrifice a young
goat to your SCSI chain now and then." -- John Woods

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Go for legibility.

Methinks efficiency will not be much of an issue here.

+1

 

While the unit is charging ALL the other functions are not used for the device.

Now.  What about later?  Future-proof your code, I always say ;-)

 

Yes, but there is more to this than I am letting on.  So as to not make this a bigger thread then need be I kept with what is always asked for which is compact code relevant to the question

Fair enough.  You know us, though... we can't help ourselves blush

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joeymorin wrote:

You know us, though... we can't help ourselves blush

 

I certainly can't. So I'm going to suggest that maybe a timer ISR could sample the 2 bits regularly and set the status LED accordingly. This way, only a switch...case would be needed.

If this is just status information for humans, an ISR running at a few Hz would be enough.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joeymorin wrote:
Fair enough. You know us, though... we can't help ourselves blush

 

Yeah I noticed that it does happen once in a while.....wink

 

El Tangas wrote:
I certainly can't. So I'm going to suggest that maybe a timer ISR could sample the 2 bits regularly and set the status LED accordingly. This way, only a switch...case would be needed. If this is just status information for humans, an ISR running at a few Hz would be enough.

 

Ok, ok, ok....enough.

 

When the unit is plugged in, the device enters 'Charger" mode and there is NOTHING else going on so no need for fancy, fancy, code stuff here. LOL! laugh

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
However I do wonder why a switch() is needed too. Presumably:

uint8_t charger_status = (pind & 0x03); 

while((pind & 0x03) == charger_status) 
{ //do something } 

would cater for all the cases in simpler code?

 

Cliff,

I think I may be reading your code wrong, but How would that work for all four states as in each state something different is going to happen? 

 

Confused.....

 

JIm

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

Last Edited: Tue. Sep 4, 2018 - 02:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which is typical for a Finite State Machine.

 

West Coast Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
but How would that work for all four states as in each state something different is going to happen?
Oh I see - they all contained "// do something". I thought it was the same "something".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
.The charger IC outputs on two pins four binary states - 0..3 to indicate what is going on.

 

Just curious what charger IC we are talking about?   Always looking for cool charging circuits and ideas!

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
Just curious what charger IC we are talking about? Always looking for cool charging circuits and ideas!

 

MCP73871.  Its a nice part.  Oh, theres actually a third status line, that I am not using for reasons I am not bringing up here.

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
reasons I am not bringing up here.

Spoilsport!

 

cheeky

 

Ready for Tip #5 ... ?

 

wink

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:

reasons I am not bringing up here.

 

Spoilsport!

 

cheeky

Yeah, someone has to try and keep the topic on topic....

 

JIm

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Back in 1983 Tom Duff invented something which became known as "Duff's Device":

send(to, from, count)
register short *to, *from;
register count;
{
    register n = (count + 7) / 8;
    switch (count % 8) {
    case 0: do { *to = *from++;
    case 7:      *to = *from++;
    case 6:      *to = *from++;
    case 5:      *to = *from++;
    case 4:      *to = *from++;
    case 3:      *to = *from++;
    case 2:      *to = *from++;
    case 1:      *to = *from++;
            } while (--n > 0);
    }
}

Explanation on:

https://en.wikipedia.org/wiki/Duff%27s_device

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah, someone has to try and keep the topic on topic....

 

Quebracho seems to be the hardest wood.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If "do something" is always the same thing, than I would prefer

 

uint8_t initial_charger_status = (pind & 0x03);

while((pind & 0x03) == initial_charger_status)
{
    //do something
}

 

If "do something" is different, than I would prefer

 

void DoSomething0() { ... }
void DoSomething1() { ... }
void DoSomething2() { ... }
void DoSomething3() { ... }

typedef void (*ToDo)();
static ToDo ToDoList[4] = { DoSomething0, DoSomething1, DoSomething2, DoSomething3 };

...

    uint8_t initial_charger_status = (pind & 0x03);
    while((pind & 0x03) == initial_charger_status)
    {
        ToDoList[initial_charger_status]();
    }
    

 

 

 

In the beginning was the Word, and the Word was with God, and the Word was God.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In the end I am not using the while() concept that started this. Just a simple SWITCH/CASE as was suggested. The while loop idea will be explored some more, but for now I need to get this thing done.

So, the charger section of the code is functioning properly and I thank everyone for heir contributions. I'll add another post later with the solution(s) pointed out as there were a few.

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you want to re- visit this later:

From my understanding the "case" statements are not more than labels (which have to resolve into an int) and are simply ignored by the rest of the code (Such as the while loop in Duff's device #29)

 

But the best place to get reliable info in this is probably the good ol K&R or the official C standard.

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The Standard does, in fact, call them "case labels" (and the "default label") - there is no such thing as a "case statement".

 

EDIT

 

K&R likewise.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Sep 6, 2018 - 02:36 PM