Thread: Help finding bug.

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    15

    Help finding bug.

    Thanks for taking a look at my code. Below is code for a motor controller. Everything looks just fine to me. It works for one cycle, and then will NOT return to the neutral state and I have no idea why. Anyone see something I am missing?

    flash_param[] array is previously entered data from usb by user. This works great and the values entered are correctly stored in flash. When starting the program, only M1_Forward_onFlag is set, so the program always starts with this. Next neutral will always come, then reverse. Next I am SUPPOSED to go back to neutral but I do not make it back to neutral.

    Thanks much.

    (in case you are wondering, all counters increment every 100ms)

    Code:
    void motor1_stateControl(void)
    {
            if(M1_Forward_onFlag == SET)
                {
                 M1_Forward_Counter++;
                 GPIO_SetBits(GPIOG, GPIO_Pin_2);
                 if(M1_Forward_Counter >= flash_param[2])
                   {
                    M1_Forward_Counter  = 0;
                    M1_Forward_onFlag   = RESET;
                    Neutral_Flag        = SET;
                    Back_or_ForwardNext = BACKWARD;
                   }
                }
            if(Neutral_Flag == SET)
                {
                GPIO_ResetBits(GPIOG, GPIO_Pin_2);
                GPIO_ResetBits(GPIOG, GPIO_Pin_3);
                M1_Neutral_Counter++;
                if(M1_Neutral_Counter >= flash_param[7])
                    {
                    M1_Neutral_Counter = 0;
                    Neutral_Flag       = RESET;
                    if(Back_or_ForwardNext == FORWARD)
                       M1_Forward_onFlag   = SET;
                    if(Back_or_ForwardNext == BACKWARD)
                       M1_Backward_onFlag  = SET;
                    }
                 }
            if(M1_Backward_onFlag == SET)
                {
                 M1_Backward_Counter++;
                 GPIO_SetBits(GPIOG, GPIO_Pin_3);
                 if(M1_Backward_Counter >= flash_param[3])
                   {
                    M1_Forward_Counter  = 0;
                    M1_Forward_onFlag   = RESET;
                    Neutral_Flag        = SET;
                    Back_or_ForwardNext = FORWARD;
                   }
                } 
        return;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Looks like some good old copy/paste action
    Code:
            if(M1_Backward_onFlag == SET)
                {
                 M1_Backward_Counter++;
                 GPIO_SetBits(GPIOG, GPIO_Pin_3);
                 if(M1_Backward_Counter >= flash_param[3])
                   {
                    M1_Forward_Counter  = 0;
                    M1_Forward_onFlag   = RESET;
                    Neutral_Flag        = SET;
                    Back_or_ForwardNext = FORWARD;
                   }
                }
    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.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    15
    Damn it. What a stupid error. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding max
    By gfotedar in forum C Programming
    Replies: 2
    Last Post: 02-10-2012, 08:23 AM
  2. Finding the max in row
    By qweqwe in forum C Programming
    Replies: 5
    Last Post: 12-04-2011, 10:46 PM
  3. Finding the mean
    By openwindow in forum C Programming
    Replies: 2
    Last Post: 11-20-2011, 07:34 AM
  4. GPA and finding a job
    By camel-man in forum General Discussions
    Replies: 3
    Last Post: 10-17-2011, 06:18 AM
  5. Finding Max and Min Value
    By will15 in forum C++ Programming
    Replies: 3
    Last Post: 11-28-2006, 11:27 AM