Example #1
0
int main() {
    
    initLEDs();
    initSW1();
    
    enableInterrupts();
    
    while(1){
        switch(state){
            case waitPush:
                break;
            case waitRelease:
                break;
        }
    }
    
    return 0;
}
Example #2
0
int main(void){
    //SYSTEMConfigPerformance(10000000);
    int i = 0;
    int j = 0;
    int onLine = 0;
    float Volts = 0.0;
    init_timer_1();
    enableInterrupts();
    init_lcd();
    clear_lcd();
    initPWM();
    char voltage[8];
    initSW1();
    initADC();
    initMotor();
    move_cursor_lcd(0,1);
    print_string_lcd("lLLLLL");
    PrintValue();  
    lineL = valL + 50;//780;
    lineR = valR + 50;//805;
    lineM = valM - 60;//840;
    while(1){
        switch(state) {

            case Forward:
                OC1RS = 0;
                OC2RS = 0;
                OC3RS = 200;
                OC4RS = 200;
                onLine = 0;
                PrintValue();
                if(valR > lineR && valL > lineL && valM > lineM) { 
                    state = Double;
                }
                else if (valL > lineL) {
                    onLine = 0;
                    state = TurnLeft;
                    if (valM > lineM + 50/*&& counter == 0*/) state = Forward;
                }

                else if (valR > lineR) {
                    onLine = 0;
                    state = TurnRight;
                    if (valM > lineM /*&& counter == 0*/) state = Forward;
                    //else state = TurnRight;
                } 
                
                else onLine = 0;
                sprintf(stateDisp, "For");
                break;
            //All sensors are over a line    
            case Double:
                OC1RS = 0;
                OC2RS = 0;
                OC3RS = 200;
                OC4RS = 200;
                if (onLine == 0) {
                        counter += 1;
                        onLine = 1;
                }
                sprintf(stateDisp,"Doub");
                PrintValue();
               // delay_ms(100);
                //if(!(valR > (lineR + Thresh) && valL > (lineL + Thresh) && valM > (lineM + Thresh))) state = Forward;
                if (counter >= 1) state = TurnAround;
                //if (counter > 5) state = TurnLeft;
                if (counter == 12) state = Idle;
                break;
            case TurnAround: 
                sprintf(stateDisp,"Tur");
                OC1RS = 100;
                OC2RS = 0;
                OC3RS = 100;
                OC4RS = 0;   
                onLine = 0;
                PrintValue();   
                stateNext = TurnAround; 
                delay_ms(1);
                if (valR > lineR /*&& valR < lineR*/ && i == 1) {
                    j = 1;                    
                }   
                if(valR < lineR) i = 1;
                if (j == 1 && valM > lineM + Thresh) {
                stateNext = Forward;
                    i = 0;
                    j = 0;
                }
                //else if(valR < lineR) state = Forward;
                state = stateNext;
                break;
            case TurnLeft:
                OC1RS = 0;
                OC2RS = 0;
                OC3RS = 0;
                OC4RS = 300;
                onLine = 0;
                sprintf(stateDisp, "Lef");
                PrintValue();
                if(valL < lineL) state = Forward;
                //else if (valR > lineR && valM > lineM) state = Double;
                break;
            case TurnRight:
                OC1RS = 0;
                OC2RS = 0;
                OC3RS = 300;
                OC4RS = 0;
                onLine = 0;
                sprintf(stateDisp, "Rig");
                PrintValue();
                if(valR < lineR) state = Forward;
                //if(valL > lineL) state = TurnLeft
                if(valL > lineL) state = TurnLeft;
                if (valR > (lineR + Thresh) && valL > (lineL + Thresh) && valM > (lineM + Thresh)) state = Double;
                    //else if(counter < 1) state = Forward;
                    //else state = TurnRight;
 
                break;

            case Idle:
                idle1 = 1;
                OC1RS = 0;
                OC2RS = 0;
                OC3RS = 0;
                OC4RS = 0;
                sprintf(stateDisp, "Idl");
                PrintValue();
                break;
                
        }
    }

    return 0;
}
Example #3
0
int main(void){
    SYSTEMConfigPerformance(10000000);
    enableInterrupts();
    initTimer1();
    initTimer2();
    initTimer45();
    initSW1();
    initLCD();
    initPWM();
    initADC();
    
    setMotorDirection(M1, 1); 
    setMotorDirection(M2, 1);
    while(1){      //Lab3 Part1
       switch(state){
            case forward:
                prevstate = forward; 
                ADCbuffer = getADCbuffer();
                if((dispVolt < ADCbuffer) && ((dispVolt + 1) <= ADCbuffer)){//to reduce excessive LCD updates
                    printVoltage(ADCbuffer);
                    dispVolt = ADCbuffer;
                }
                else if((dispVolt > ADCbuffer) && ((dispVolt - 1) >= ADCbuffer)){
                    printVoltage(ADCbuffer);
                    dispVolt = ADCbuffer;
                }
                moveCursorLCD(1,0);
                printStringLCD("forward ");
                if(remap == 1){
                    setMotorDirection(M1,1);
                    setMotorDirection(M2,1);
                    delayUs(1000);
                    remap = 0;
                }
                setMotorSpeed(ADCbuffer, direction);
                break;

            case backward:
                prevstate = backward;
                ADCbuffer = getADCbuffer();
                if((dispVolt < ADCbuffer) && ((dispVolt + 1) <= ADCbuffer)){//to reduce excessive LCD updates
                    printVoltage(ADCbuffer);
                    dispVolt = ADCbuffer;
                }
                else if((dispVolt > ADCbuffer) && ((dispVolt - 1) >= ADCbuffer)){
                    printVoltage(ADCbuffer);
                    dispVolt = ADCbuffer;
                }
                moveCursorLCD(1,0);
                printStringLCD("backward");
                if(remap == 1){
                    setMotorDirection(M1,0);
                    setMotorDirection(M2,0);
                    delayUs(1000);
                    remap = 0;
                }
                setMotorSpeed(ADCbuffer, direction);
                break;           

            case idle:
                unmapPins();
                ADCbuffer = getADCbuffer();
                if((dispVolt < ADCbuffer) && ((dispVolt + 1) <= ADCbuffer)){//to reduce excessive LCD updates
                    printVoltage(ADCbuffer);
                    dispVolt = ADCbuffer;
                }
                else if((dispVolt > ADCbuffer) && ((dispVolt - 1) >= ADCbuffer)){
                    printVoltage(ADCbuffer);
                    dispVolt = ADCbuffer;
                }
                moveCursorLCD(1,0);
                printStringLCD("Idle    ");
                delayUs(1000);
                break;
        }     
    }  
    return 0;
}
Example #4
0
File: main.c Project: Zuois/lab0
int main() {
    SYSTEMConfigPerformance(10000000); //Configures low-level system parameters for 10 MHz clock
    enableInterrupts(); //This function is necessary to use interrupts.

    //TODO: Write each initialization function
    initLEDs();
    initTimer1();
    initTimer2();
    initSW1();

    while (1) {

        //TODO: Implement a state machine to create the desired functionality
        switch (state) {

            case(stateLED1):
                LED1 = LATON;
                LED2 = LATOFF;
                LED3 = LATOFF;
                break;
            case(statePressPauseLED1):
                if (T2CONbits.ON == 0) {
                    TMR2 = 0;
                    T2CONbits.ON = TIMERON;
                }
                break;
            case(statePressTimerLED1):
                if (T1CONbits.ON == 0) {
                    TMR1 = 0;
                    T1CONbits.ON = TIMERON;
                }
                break;
            case(statePressedLED1):
                if (BUTTON == 1) {
                    state = stateUnPressPauseSecLED1;
                }
                break;
            case(stateUnPressPauseLED1):
                T1CONbits.ON = TIMEROFF;
                if (T2CONbits.ON == 0) {
                    TMR2 = 0;
                    T2CONbits.ON = TIMERON;
                }
                break;
            case(stateUnPressPauseSecLED1):
                if (T2CONbits.ON == 0) {
                    TMR2 = 0;
                    T2CONbits.ON = TIMERON;
                }
                break;
            case(stateLED2):
                LED1 = LATOFF;
                LED2 = LATON;
                LED3 = LATOFF;
                break;
            case(statePressPauseLED2):
                if (T2CONbits.ON == 0) {
                    TMR2 = 0;
                    T2CONbits.ON = TIMERON;
                }
                break;
            case(statePressTimerLED2):
                if (T1CONbits.ON == 0) {
                    TMR1 = 0;
                    T1CONbits.ON = TIMERON;
                }
                break;
            case(statePressedLED2):
                if (BUTTON == 1) {
                    state = stateUnPressPauseSecLED2;
                }
                break;
            case(stateUnPressPauseLED2):
                T1CONbits.ON = TIMEROFF;
                if (T2CONbits.ON == 0) {
                    TMR2 = 0;
                    T2CONbits.ON = TIMERON;
                }
                break;
            case(stateUnPressPauseSecLED2):
                if (T2CONbits.ON == 0) {
                    TMR2 = 0;
                    T2CONbits.ON = TIMERON;
                }
                break;
            case(stateLED3):
                LED1 = LATOFF;
                LED2 = LATOFF;
                LED3 = LATON;
                break;
            case(statePressPauseLED3):
                if (T2CONbits.ON == 0) {
                    TMR2 = 0;
                    T2CONbits.ON = TIMERON;
                }
                break;
            case(statePressTimerLED3):
                if (T1CONbits.ON == 0) {
                    TMR1 = 0;
                    T1CONbits.ON = TIMERON;
                }
                break;
            case(statePressedLED3):
                if (BUTTON == 1) {
                    state = stateUnPressPauseSecLED3;
                }
                break;
            case(stateUnPressPauseLED3):
                T1CONbits.ON = TIMEROFF;
                if (T2CONbits.ON == 0) {
                    TMR2 = 0;
                    T2CONbits.ON = TIMERON;
                }
                break;
            case(stateUnPressPauseSecLED3):
                if (T2CONbits.ON == 0) {
                    TMR2 = 0;
                    T2CONbits.ON = TIMERON;
                }
                break;
        }


    }

    return 0;
}