Frewacom
5/22/2018 - 11:12 AM

MPT - Prov.c

// Fredrik Engstrand TE16
// Prov i Mikrodatortillämpningar
// Installera EZSTK för att använda: https://github.com/Frewacom/EZSTK

#define NUMERIC_PORT PORTA
#define MOTOR_PORT PORTC

#include <EZSTK/boilerplate.h>

#include <EZSTK/numeric.h>
#include <EZSTK/motor.h>

int lights[8] = {LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8};
int t11Speeds[5] = {10, 15, 20, 25, 30};
int t6Status = 0;
int t8CurrentSum = 0;
int t9ClickedButton = 0;
int t9Number = 0;
int t8Pins[6] = {1, 2, 8, 64, 16, 32};
int t11Speed = 0;
int t11Button = 0;
int t11Direction = ENGINE_FORWARD;

void Blink(int blinkLight, int exitSwitch, int onInterval, int offInterval, int startVal) {
    while (1) {
        if (EZSTK::SwitchDown(exitSwitch)) {
            return;
        } else {
            if (startVal == 0) {
                LED_PORT = 255;
                EZSTK::Delay(offInterval);
                EZSTK::LightOn(blinkLight);
                EZSTK::Delay(onInterval);
            } else {
                EZSTK::LightOn(blinkLight);
                EZSTK::Delay(onInterval);
                LED_PORT = 255;
                EZSTK::Delay(offInterval);
            }
        }
    }
}

void Task1() {
    EZSTK::LightOn(SW1 | SW2 | SW3);
}

void Task2() {
    if (EZSTK::SwitchDown(SW1)) {
        LED_PORT = 0;
    }
}

void Task3() {
    if (EZSTK::SwitchDown(SW2)) {
        Blink(LED2, SW2, 500, 500, 1);   
    } 
}

void Task4() {
    if (EZSTK::SwitchDown(SW3)) {
        Blink(LED3, SW3, 2000, 3000, 0);
    }
}

void Task5() {
    if (EZSTK::SwitchDown(SW4 | SW5)) {
        EZSTK::LightOn(LED1);
    }
}

void Task6() {
    if (EZSTK::SwitchDown(SW6)) {
        EZSTK::LightOn(LED1);
        t6Status = 1;
    } else {
        if (t6Status) {
            _delay_ms(2000);
            LED_PORT = 255;
            t6Status = 0;
        }
    }
}

void Task7() {
    EZSTK::Numeric::Number(3);
}

void Task8() {
    for (int i = 0; i < 16; i++) {
        if (i < 8) {
            t8CurrentSum += lights[i];
        } else {
            t8CurrentSum -= lights[i - 8];
        }

        EZSTK::LightOn(t8CurrentSum);
        _delay_ms(200);
    }
}

void Task8_2() {
    for (int i = 0; i < 6; i++) {
        NUMERIC_PORT = 255 - t8Pins[i];
        _delay_ms(75);
    }
}

void Task9() {
    if (t9ClickedButton) {
        if (EZSTK::SwitchDown(SW8)) {   
            if (t9Number != 0) {
                EZSTK::Numeric::Number(t9Number);
            }
        }
    } else {
        while (t9Number == 0) {
            t9Number = EZSTK::GetSwitch();
        }

        t9ClickedButton = 1;
        EZSTK::LightOn(lights[t9Number - 1]);
    }
}

void Task10() {
    if (EZSTK::SwitchDown(SW1)) {
        for (int i = 0; i < ENGINE_ONE_SPIN * 3; i++) {
            EZSTK::Motor::Step(25, 1);
        }

        _delay_ms(3000);

        for (int i = 0; i < ENGINE_ONE_SPIN * 3; i++) {
            EZSTK::Motor::Step(10, 0);
        }
    }
}

void Task11() {
    t11Button = EZSTK::GetSwitch();

    if (t11Button != 0 && t11Button < 6) {
        t11Speed = t11Speeds[t11Button - 1];
        EZSTK::Numeric::Number(t11Button);
    }

    EZSTK::Motor::Step(t11Speed, t11Direction);
}

int main(void)
{
    EZSTK::Initialize();
    EZSTK::Numeric::Initialize();
    EZSTK::Motor::Initialize();

    while (1)
    {
		// För att köra de olika uppgifterna så byter du ut denna funktion till en annan,
		// ex. Task1(); eller Task5();
        Task11();
    }

    return 1;}