Thread: Switch function problem

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    25

    Switch function problem

    Below is the code for my clock.

    The 2nd switch function doesn't operate properly. After it goes through all the cases from 1-5, it doesn't loop back. I think I've misunderstood the purpose of default. I thought it switches into default whenever it doesn't match the case.

    I've tried inserting my countersec2=0; at the end of the 2nd switch function but it doesn't work. any ideas?

    Well to be more precise, it doesn't display 1-5 again on the hardware im using, but it still counts up. So its reaching countersec2++; but i don't have a way of resetting the countersec2 to be able to loop the 2nd switch function. btw i haven't put the countersec2=0; in the code below..

    Code:
    while(1)
            {
                //if(IORD_ALTERA_AVALON_PIO_DATA(TICK_PIO_BASE) ^ 1)
                //{/*this checks to see if you have pressed hte button or not IORD_ALTER... is the button*/
                    countersec++;
                
                    switch (countersec)
                    {  
                    case 1:
                        led = hex_num[1];
                        IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS_PIO_BASE, led);
                        break;
                    case 2:
                        led = hex_num[2];
                        IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS_PIO_BASE, led);
                        break;
                    case 3:
                        led = hex_num[3];
                        IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS_PIO_BASE, led);
                        break;
                    case 4:
                        led = hex_num[4];
                        IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS_PIO_BASE, led);
                        break;
                    case 5:
                        led = hex_num[5];
                        IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS_PIO_BASE, led);
                        break;
                    case 6:
                        led = hex_num[6];
                        IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS_PIO_BASE, led);
                        break;
                    case 7:
                        led = hex_num[7];
                        IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS_PIO_BASE, led);
                        break;
                    case 8:
                        led = hex_num[8];
                        IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS_PIO_BASE, led);
                        break;
                    case 9:
                        led = hex_num[9];
                        IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS_PIO_BASE, led);
                        break;
                    default: 
                        led = hex_num[0];
                        IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS_PIO_BASE, led);
                        countersec2++;
                        printf("countersec2:\n%d\n", countersec2);
                        switch (countersec2)
                            {
                                case 1:
                                    led = hex_num[1];
                                    IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS2_PIO_BASE, led);
                                    break;
                                case 2:
                                    led = hex_num[2];
                                    IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS2_PIO_BASE, led);
                                    break;
                                case 3:
                                    led = hex_num[3];
                                    IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS2_PIO_BASE, led);
                                    break;
                                case 4:
                                    led = hex_num[4];
                                    IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS2_PIO_BASE, led);
                                    break;
                                case 5:
                                    led = hex_num[5];
                                    IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS2_PIO_BASE, led);
                                    break;
                                default:
                                    led = hex_num[0];
                                    IOWR_ALTERA_AVALON_PIO_DATA(LED_10HRS2_PIO_BASE, led);
                                    break;
                            }//second switch case ends.
                         
                            
                            printf("countersec:\n%d\n", countersec);
                            countersec=0;
                    
                   
                    
                    }//switch case ends.        
                
                    //while(IORD_ALTERA_AVALON_PIO_DATA(TICK_PIO_BASE) ^ 1)
                    //    {
                    //        random++;
                    //    } /*this makes it pause after you press the button*/ 
                
                    
            i =0;
            while(i<300000)
            i++;
               
            }
    oops nevermind i just did it lol. i put the countersec2=0; at the end of default.

    though when i tried that for the 1st switch case it didn't work.
    Last edited by ahming; 04-01-2008 at 12:13 AM. Reason: partially solved.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think I've misunderstood the purpose of default. I thought it switches into default whenever it doesn't match the case.
    That's true, though in this case a switch does not look appropriate. You could simplify by checking that countersec is within a range using if and else and assign to led accordingly. The same applies to countersec2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    while(i<300000)
            i++;
    Don't ever do this.
    It's a horrible way to burn processor time and a very inefficient one. Sleep-type APIs is what you want to use. For Windows, it's called Sleep.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you're trying to output to two different sets of LEDs, then you need both switches to happen all the time (one after the other), not make one dependent on the default inside the other.

    > Don't ever do this.
    I think they're on a PIC or something, without an operating system.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM