Hey guys I am very new to C programming but i find it super interesting,
I am currently working on a robot to improve my C skills, I am programming it so that it does not crash(with the help of its IR sensors)
I want to be ableto make the C code i have written cleaner and more proffessional lets say, so far i have packed everything in the main(), all my functions/methods etc.
I want to make header files for each switch case and/or function I have used in the main(). I have watched a few youtube tutorials and some google sites but i do not know how to implement it for my code I have written.
IN ORDER FOR ME TO SHOW YOU WHAT I MEAN I HAVE TO POST THE WHOLE CODE BUT PLEASE HAVE SOME PATIENCE AND READ THROUGH IF YOU GUYS CAN. I know Stackoverflow does not reccommend posting the whole code.
Anyways could you guys help me out? If you guys can show me how i can do that with a few examples I would very much appreciate it.
And if you guys can give me a few pointers on how exactly to comment code that would be nice too. Yes pointers was a pun.
The main.h has all the libraries neccassary for the robot.
#include "main.h"
/**
 * The Main
 */
int main() {
    sei();
    bot_init();
    display_init(DISPLAY_TYPE_TXT);
    gfx_init();
    floor_init();
    leds_init();
    spi_init();
    i2c_init();
    /**
     * 2 Different states for of the distance sensor
     */
    enum new_State
    {
        OBJECT_DETECTED, NO_OBJECT_DETECTED
    };
    /**
     * There are 5 distance sensors and each of them need a state variable
     */
    typedef enum new_State state_t;
    state_t state0;
    state_t state1;
    state_t state2;
    state_t state3;
    state_t state4;
    /**
     * The s3 button needs two states
     * TRUE = ON
     * FALSE = OFF
     */
    enum s3_State
    {
        S3_TRUE, S3_FALSE
    };
    typedef enum s3_State s3_State;
    s3_State s3; /// state of the s3 button
    /*
     * The following is a distance measuring Function
     * The coprozessor needs to be initialized for this because
     * the distance measurement is done with the help of the IR sensors
     * and the coprozessor handles it
     * Function is derived from the HelloDistance example
     */
    copro_ir_startMeasure();
    /// Text output
    ///char output[20]= "";
    /// Vairbale for storing/ buffering sensor values
    int current_distance = 0;
    /// Sign for different movement switch cases
    int sign = 0;
    while (1 == 1)
    {
        /// Default state of the s3 button
        s3 = S3_FALSE;
        if (s3_was_pressed())
        {
            if (s3 == S3_FALSE)
            {
                s3 = S3_TRUE; /// Switches s3 button ON/TRUE
            }
            else
            {
                s3 = S3_FALSE; /// Switches s3 button OFF/FALSE
            }
    }
        while (s3 == S3_TRUE)
        {
            /**
             * When/If the s3 Button is pressed all LEDs MUST
             * turn off
             */
        if (s3_was_pressed()) {
            copro_stop(); /// Stops Coprozessor
                leds_set_status(LEDS_OFF, 0);
                leds_set_status(LEDS_OFF, 1);
                leds_set_status(LEDS_OFF, 2);
                leds_set_status(LEDS_OFF, 3);
                leds_set_status(LEDS_OFF, 4);
                leds_set_status(LEDS_OFF, 5);
                leds_set_status(LEDS_OFF, 6);
                break;
            }
        /// initiating the distance sensors
        /// output displays the distance from each sensor
            char output[20] = "";
            sprintf(output, "%3i", current_distance);
            gfx_move(25, 25);
            gfx_print_text(output);
            for(int i = 1; i < 5; i++){
                current_distance = copro_distance[i]/256;
            ///uint8_t distance = nds3_get_dist();
            uint8_t distance = copro_ir_startMeasure();
            char text[20] = "";
            sprintf(text, "Distance: %3i", (int) distance);
            gfx_move(0, 0);
            gfx_print_text(text);
            copro_update();
            ///current_distance = copro_distance[0] / 256;
            }
            /**
             * A Switch case function for measuring distance
             * from each sensor. If distance < 200 then there is no object
             * LEDs will stay green if no objects are detected (200 <)
             * If the distance between object and sensor > 200 then there is an object near the sensor
             * LED will turn RED indicating Object detection
             */
            for (int i = 0; i < 5; i++) {
                current_distance = copro_distance[i] / 256;
                switch (i)
                {
                case 0:
                    if (current_distance < 120)
                    {
                        state0 = NO_OBJECT_DETECTED;
                        leds_set_status(LEDS_GREEN, 7);
                    } else
                    {
                        state0 = OBJECT_DETECTED;
                        leds_set_status(LEDS_RED, 7);
                    }
                    break;
                case 1:
                    if (current_distance < 120)
                    {
                        state1 = NO_OBJECT_DETECTED;
                        leds_set_status(LEDS_GREEN, 6);
                    } else
                    {
                        state1 = OBJECT_DETECTED;
                        leds_set_status(LEDS_RED, 6);
                    }
                    break;
                case 2:
                    /*
                     * Sensors in front need more space
                     * hence a it needs more distance to react to objects
                     * Turn left or right
                     */
                    if (current_distance < 100)
                    {
                        state2 = NO_OBJECT_DETECTED;
                        leds_set_status(LEDS_GREEN, 4);
                        leds_set_status(LEDS_GREEN, 5);
                    } else
                    {
                        state2 = OBJECT_DETECTED;
                        leds_set_status(LEDS_RED, 4);
                        leds_set_status(LEDS_RED, 5);
                    }
                    break;
                case 3:
                    if (current_distance < 100)
                    {
                        state3 = NO_OBJECT_DETECTED;
                        leds_set_status(LEDS_GREEN, 3);
                    } else
                    {
                        state3 = OBJECT_DETECTED;
                        leds_set_status(LEDS_RED, 3);
                    }
                    break;
                case 4:
                    if (current_distance < 100)
                    {
                        state4 = NO_OBJECT_DETECTED;
                        leds_set_status(LEDS_GREEN, 2);
                    } else
                    {
                        state4 = OBJECT_DETECTED;
                        leds_set_status(LEDS_RED, 2);
                    }
                    break;
                }
                /*
                 * If the front sensors do not detect any objects
                 */
                if (state1 == NO_OBJECT_DETECTED && state2 == NO_OBJECT_DETECTED
                        && state3 == NO_OBJECT_DETECTED)
                {
                    /*
                     * If the path is clear with no objects
                     * This would indicate sign 0
                     * The robo will keep going straight
                     */
                    if (sign == 0)
                    {
                        copro_setSpeed(14, 14); /// Controlling the speed on both motors L & R
                    }
                    /*
                     * If there are Objects in front and on R or L < 200
                     * robo will drive backwards
                     */
                    else if (sign == 1)
                    {
                        copro_setSpeed(-14, -14);
                        if (state0 == NO_OBJECT_DETECTED || state4 == NO_OBJECT_DETECTED)
                        {
                            sign = 2;
                        }
                    }
                    /*
                     * sign 2 indicates that the robo is out of the "Deadlock"
                     * Should avoid "Deadlock"
                     * robo will find open path
                     */
                    else if (sign == 2)
                    {
                        copro_setTargetRel(-200, -200, 20);
                        delay(1000);
                        if(state0 == NO_OBJECT_DETECTED)
                        {
                            copro_setTargetRel(200, -200, 28);
                        }
                        else
                        {
                            copro_setTargetRel(-200,200, 28);
                        }
                        delay(1000);
                        sign = 0;
                    }
                }
                /*
                 * OBJECT_DETECTED on R at distance < 200
                 * robo turns L
                 */
                else if (state1 == OBJECT_DETECTED && state2 == NO_OBJECT_DETECTED
                        && state3 == NO_OBJECT_DETECTED)
                {
                    //copro_stop();
                    copro_setSpeed(0, 14);
                }
                /*
                 * OBJECT_DETECTED on L at distance < 200
                 * robo turns R
                 */
                else if (state1 == NO_OBJECT_DETECTED && state2 == NO_OBJECT_DETECTED
                        && state3 == OBJECT_DETECTED)
                {
                    //copro_stop();
                    copro_setSpeed(14, 0);
                }
                /*
                 * robo is stuck in a deadlock and can reverse backwards
                 * signg1 means deadlock
                 */
                else if (state0 == OBJECT_DETECTED && state2 == OBJECT_DETECTED
                        && state4 == OBJECT_DETECTED)
                {
                    sign = 1;
                    ///copro_stop();
                    copro_setSpeed(-28, -28);
                }
                /*
                 * Object In front
                 * Object on R > 200 robo turns R
                 * Object on L > 200 robo turns L
                 * If both are clear? then what ?
                 */
                else if (state2 == OBJECT_DETECTED)
                {
                    //copro_stop();
                    if (copro_distance[1] / 256 > 100 || copro_distance[0] / 256 > 100)
                    {
                        copro_setSpeed(0, 14);
                    }
                    else if(copro_distance[3] / 256 > 100 || copro_distance[4] / 256 > 100)
                    {
                        copro_setSpeed(14, 0);
                    }
                    else
                    {
                        //copro_stop();
                        copro_setSpeed(0,14);
                    }
                }
            }
        }
    }
}
I would like to once again exaggerate on the fact that I posted all the code because there are several functions withing the function so i need to know how i can make a header file for it.
 
    