mag niet van hendrik D:
Dependencies: mbed MatrixMath QEI HIDScope Matrix biquadFilter MODSERIAL FastPWM
main.cpp
- Committer:
- Hendrikvg
- Date:
- 2019-09-23
- Revision:
- 16:40183eeadb6d
- Parent:
- 15:80b3ac2b8448
- Child:
- 17:cacf9e75eda7
File content as of revision 16:40183eeadb6d:
#include "mbed.h" #include "FastPWM.h" #include "MODSERIAL.h" MODSERIAL pc(USBTX, USBRX); DigitalOut ledr(LED_RED); DigitalOut ledg(LED_GREEN); DigitalOut ledb(LED_BLUE); Ticker ReduceEmission; Timer R; Timer G; Timer B; InterruptIn BUT1(D1); InterruptIn BUT2(D0); FastPWM lichtje(D3); AnalogIn ain(A0); DigitalOut direction(D6); DigitalOut speed(D7); // variables volatile char command = 'r'; enum states {red, green, blue}; states CurrentState = red; bool StateChanged = true; volatile int on_time_ms; // The time the LED should be on, in microseconds volatile int off_time_ms; int n=5; // functions void plus() { n++; // n=n+1 if (n>10) { n=10; } } void min() { n--; if (n<0) { n=0; } } void TurnLedRed() { ledr = 0; ledg = 1; ledb = 1; } void TurnLedGreen() { ledr = 1; ledg = 0; ledb = 1; } void TurnLedBlue() { ledr = 1; ledg = 1; ledb = 0; } void PulseWidthModulation() // Bepaalt de duty cycle gebaseerd op de potentiometer en zet de motor dan op die snelheid mbh PWM { on_time_ms = (int) (ain.read()*(1.0/50)*1.0e3); off_time_ms = (int) ((1-ain.read())*(1.0/50)*1.0e3); direction = 1; wait_ms(on_time_ms); direction = 0; wait_ms(off_time_ms); } void EnergySaving() // Functie voor de "ReduceEmissions" ticker, zet de motor uit en LED op rood { command = 'r'; } void CheckCommandFromTerminal(void) // Functie voor de in de main loop { command = pc.getcNb(); } void WhileRed() { if (command == 'g') { R.stop(); pc.printf("The LED has been red for %f seconds!\n\r", R.read()); CurrentState= green; StateChanged= true; } if (command == 'b') { R.stop(); pc.printf("The LED has been red for %f seconds!\n\r", R.read()); CurrentState= blue; StateChanged= true; } } void WhileGreen() { PulseWidthModulation(); if (command == 'r') { G.stop(); pc.printf("The LED has been green for %f seconds!\n\r", G.read()); CurrentState= red; StateChanged= true; } if (command == 'b') { G.stop(); pc.printf("The LED has been green for %f seconds!\n\r", G.read()); CurrentState= blue; StateChanged= true; } } void WhileBlue() { PulseWidthModulation(); if (command == 'r') { B.stop(); pc.printf("The LED has been blue for %f seconds!\n\r", B.read()); CurrentState= red; StateChanged= true; } if (command == 'g') { B.stop(); pc.printf("The LED has been blue for %f seconds!\n\r", B.read()); CurrentState= green; StateChanged= true; } } void StateMachine(void) { switch(CurrentState) { case red: if (StateChanged) { pc.printf("Initiating turning LED red\n\r"); StateChanged= false; TurnLedRed(); R.start(); direction = 0; pc.printf("LED is now red!\n\r"); } WhileRed(); break; case green: if (StateChanged) { pc.printf("Initiating turning LED green\n\r"); StateChanged= false; TurnLedGreen(); G.start(); speed = 0; pc.printf("LED is now green!\n\r"); } WhileGreen(); break; case blue: if (StateChanged) { pc.printf("Initiating turning LED blue\n\r"); StateChanged= false; TurnLedBlue(); B.start(); speed = 255; pc.printf("LED is now blue!\n\r"); } WhileBlue(); break; default: pc.printf("Unknown or unimplemented state reached!\n\r"); } } // main int main() { pc.baud(115200); ReduceEmission.attach(EnergySaving,20); BUT1.fall(plus); BUT2.fall(min); while(true) { CheckCommandFromTerminal(); StateMachine(); lichtje.write(ain.read()); // duty cycle } }