Newb needs help with loop issues

This is a discussion on Newb needs help with loop issues within the C Programming forums, part of the General Programming Boards category; I am sure that this will be completely easy for most of you but I haven't got a clue. I ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    6

    Newb needs help with loop issues

    I am sure that this will be completely easy for most of you but I haven't got a clue. I am writing code for use with a pic microcontroller and I am having problems with my while statements. My program uses hex values but it shouldn't be any different. I am assigning a char (pinout) to the port that I am recieving inputs from.

    Here is my problem:
    I need to read the port and hold the value to keep the while loop repeating until a different certain input is given through the port.


    Here is some code, maybe seeing it will help more.
    Code:
    switch (pinout)  
    	{
    	case 0x01:			
    		message911();	
    		break;
    	case 0x02:
    		urgent();	
    		break;
    	case 0x04:			
    		MoveL();
    		break;
    	case 0x08:
    		MoveR();
    		break;
    	}
    This switch statement works and will run the correct void.
    My problem is with the loop statement for the void.
    Code:
    void message911()
    {
    while((pinout == 0x00) || (pinout == 0x01))
      {
      .....
       pinout = portD;
      }
    My problem is that "pinout' has to return to 0x00. "Pinout" is 0x01 for a milisecond. Just enough time to enter the void. After that it goes to 0x00 and remains there.

    I need the loop to continue until "pinout" is 0x02, 0x04, or 0x08. Any suggestions would help alot. Also, this is my first post so no flaming, pls.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    try:

    Code:
    while((pinout == 0x00) || (pinout == 0x01))  {
      continue; 
      }
    pinout = portD;
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    No, I don't think that will work. Because he needs to update pinout. otherwise the while loop will continue forever. of course, I only no what not to do, not what to do. just like in everything i do.
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> No, I don't think that will work. Because he needs to update pinout. otherwise the while loop will continue forever.

    I think 'pinout' is a global variable that receives it's input elsewhere in the code and he's just trying to poll it in the loop...correct me if I'm wrong. also, it may be a good idea to set a certain time limit for the loop to run - otherwise, if something unexpected happens that causes 'pinout' to remain unchanged, the program (or the thread the function is being called from) will freeze up.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    6
    I knew this would be a little confusing. The varible pinout has to be able to change throughout the entire program. I am not sure if that makes it a global varible though.
    Code:
    while ((pinout != 0x02) || (pinout != 0x04) || (pinout != 0x08))
    {
    pinout = portD;
    }
    I was thinking this might be able to keep the loop repeating. This way it is always in the loop until pinout becomes one of the values
    needed to go into another void. Any suggestions?

    For those with some electronics background, I am using "momentary on" push switches to set my values for portD. I need the void to continue to run until the value of portD changes to the correct values. I need it to act like I am using a standard switch; in the on position it stays on.

    As for a time limit, the project I am working on is 90% hardware and 10% programming. It really needs to continue forever. I have a clear (reset) on the pic microcontroller if I need it.

    Thanks.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That would be:
    Code:
    while( (pinout != 0x02) && (pinout !=0x04) && (pinout != 0x08 )
    {
        pinout = portD;
    }
    Or perhaps...
    Code:
    while( pinout > 0x01 )
    {
        pinout = portD;
    }
    On another note, the term global refers to the scope of the variable. A variable declared outside of all functions is a global variable. It can be modified by virutally everything. Consider:
    Code:
    #include <stdio.h>
    
    int x; /* x is a global variable */
    
    void foo( void )
    {
        x = 11; /* modifying it in foo */
    }
    
    
    int main( void )
    {
        foo( );
        printf( "x is %d\n", x );
    
        x = 5; /* modifying it in main */
        printf("x is %d\n", x );
    
        return 0;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    it would probably simplify the job if you made pinout a global, but I can't say that for sure since I haven't seen all of your code. Also I'm no expert on microcontrollers, but there should be no problems in using globals.

    [edit]
    beaten! And with a more complete answer too
    [/edit]

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    6
    the way you explain it, I would say that it is a global varible. I declared it at the beginning and change it throughout the program; in main, and in about 5 different voids. my code is extremely long and I don't think it would be all that helpfull. I will post the main part of it.

    Code:
    #include "choices.c"
    
    main()
    {
      initl();	             /*initilize MainBoard using void initl*/
      pinout = PortD;
      while(1)
        {
          choose();      /*choose is the void with the switch*/	
        }
    }
    this is the main code, the rest is in the choices.c
    here is the setup with the code needed.
    Code:
    char pinout;
    
    void initl();
    void choose();
    void message911();
    void urgent();
    void MoveL();
    void MoveR();
    
    void choose()
    {
    	switch (pinout)  
    	{
    	case 0x01:			
    		message911();	
    		break;
    	case 0x02:
    		urgent();	
    		break;
    	case 0x04:			
    		MoveL();
    		break;
    	case 0x08:
    		MoveR();
    		break;
    	}
    }
    
    void message911()
    {
    while((pinout == 0x00) || (pinout == 0x01))
      {
         pinout = portD;
      }
    void urgent()
    {
    while((pinout == 0x00) || (pinout == 0x02))
      {
         pinout = portD;
      }
    sorry for the long post

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You usually don't want to include ".c" files. That's usually left for headers. You can, it's just bad form. Usually you compile all your ".c" files, and include header files to make sure each each piece that needs to see the other can do so. Example:
    Code:
    /* foo.h */
    void foo( void );
    
    /* foo.c */
    #include <stdio.h>
    void foo( void )
    {
        printf( "Foo!\n" );
    }
    
    /* bar.c */
    #include "foo.h"
    
    int main( void )
    {
        foo( );
    }
    
    prompt> gcc -o bar bar.c foo.c
    prompt> ./bar
    "Foo!"
    
    prompt>
    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    pinout isn't a global variable because it is in the main() function. put it outside, then that might solve your problem.
    Code:
    pinout = PortD;
    
    main()
    {
       initl();
       while(1)
       {
          choose();	
       }
    }
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by FatalError
    I am writing code for use with a pic microcontroller and I am having problems with my while statements.
    Quote Originally Posted by FatalError
    For those with some electronics background, I am using "momentary on" push switches to set my values for portD. I need the void to continue to run until the value of portD changes to the correct values. I need it to act like I am using a standard switch; in the on position it stays on.
    Are you doing a purely software debounce? Here are some articles, from a very quick search, that describe a couple ways. Maybe they can be helpful.Are you intending to trigger on the press or the release of the button?

    And lastly, have you tried to find any application notes from Microchip? Here are some other possible hits.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    6
    I tried the new while statement quzah posted and it still comes out of the loop when pinout is 0x00. Not sure why?

    when I put "pinout=portD;" before the main it gives me an error saying "redefinition of pinout". I think that pinout is already a global varible because it is defined before the main (it is defined in the choices.c - char pinout "pinout = portD;" is just assigning portD to the char pinout.

    Let me see if I can simply a little. I need this program to:
    read port D, go through switch statement, go to correct void, stay in void until port D is changed to a value that will change to a different void, then it needs to go back to the switch, and go to the correct void,....

    I got it to do everything but stay in the void.

    Here's a thought, what if I setup another character, and constantly compare it to pinout.
    Code:
    char pincomp;
    
    void message911()
    {
    pincomp = y;
      while (pincomp = y)
       {
          pinout = portD;
          if (pinout = 0x02)
          {
             pincomp = n;
          }
          else if (pinout = 0x04)
          {
             pincomp = n;
          }
          else if (pinout = 0x08)
          {
             pincomp = n;
          }
          else
             pincomp = y;
        }
    }
    what do you think? will it work?

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    6
    I didn't see the previous post. Actually, the debouncing has nothing to do with it. I have a filter that keeps my signal clean. I am triggering on the press. Its a momentary on, so while I am pressing it, it is sending the signal.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by FatalError
    Code:
    void message911()
    {
    while((pinout == 0x00) || (pinout == 0x01))
      {
      .....
       pinout = portD;
      }
    My problem is that "pinout' has to return to 0x00. "Pinout" is 0x01 for a milisecond. Just enough time to enter the void. After that it goes to 0x00 and remains there.

    I need the loop to continue until "pinout" is 0x02, 0x04, or 0x08. Any suggestions would help alot.
    Just a shot in the dark: I wonder if the compiler has optimized away the port read. If the read were being optimized out, declaring pinout as volatile might fix that. (I would have imagined that the register was declared as volatile, but thay may not be so.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    6
    My statement comparing another varible didn't work. It does the same thing.

    What do you mean by declaring it as volatile? I am not familiar with the term.

    I did figure out one thing, if I hold the button down the program works. So if I constantly give pinout the correct value it will continue in the loop. Now I just need to figure out how to make pinout hold the value until it becomes one of the other values needed by the switch.

    I know I could just use standard switches but that will throw off my hardware and I need the momentary switches for other functions. I am running more than one pic microcontroller.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 01:13 AM
  2. Replies: 8
    Last Post: 12-01-2008, 09:09 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 06:19 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21