Proyecto Parcial
main.cpp@0:b79d9ad8d1c3, 2013-10-31 (annotated)
- Committer:
- DnPlas
- Date:
- Thu Oct 31 00:31:28 2013 +0000
- Revision:
- 0:b79d9ad8d1c3
Proyecto Parcial;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DnPlas | 0:b79d9ad8d1c3 | 1 | //This program will execute a little game of intermitent leds |
DnPlas | 0:b79d9ad8d1c3 | 2 | #include "mbed.h" |
DnPlas | 0:b79d9ad8d1c3 | 3 | |
DnPlas | 0:b79d9ad8d1c3 | 4 | BusOut leds(D13, D10, D9, D8); |
DnPlas | 0:b79d9ad8d1c3 | 5 | AnalogIn pot(A2); |
DnPlas | 0:b79d9ad8d1c3 | 6 | DigitalIn bttn1(D11); |
DnPlas | 0:b79d9ad8d1c3 | 7 | DigitalIn bttn2(D12); |
DnPlas | 0:b79d9ad8d1c3 | 8 | Serial pc(USBTX, USBRX); |
DnPlas | 0:b79d9ad8d1c3 | 9 | |
DnPlas | 0:b79d9ad8d1c3 | 10 | float pot_val; //The value of the potenciometer will be a float since it is get from 0 to 1 |
DnPlas | 0:b79d9ad8d1c3 | 11 | int on = 1, counter, reps = 100; //Declaration of variables |
DnPlas | 0:b79d9ad8d1c3 | 12 | |
DnPlas | 0:b79d9ad8d1c3 | 13 | void chk_bttn() //Prototype Function 1. It checks the button while pressed and assigns a certain action |
DnPlas | 0:b79d9ad8d1c3 | 14 | { |
DnPlas | 0:b79d9ad8d1c3 | 15 | if (on == 8 && bttn1 ==0) |
DnPlas | 0:b79d9ad8d1c3 | 16 | { |
DnPlas | 0:b79d9ad8d1c3 | 17 | pc.printf("Winner!\nScore: %i\n_________\n", counter); |
DnPlas | 0:b79d9ad8d1c3 | 18 | wait (1); |
DnPlas | 0:b79d9ad8d1c3 | 19 | } |
DnPlas | 0:b79d9ad8d1c3 | 20 | else |
DnPlas | 0:b79d9ad8d1c3 | 21 | if(on !=8 && bttn1 ==0) |
DnPlas | 0:b79d9ad8d1c3 | 22 | { |
DnPlas | 0:b79d9ad8d1c3 | 23 | pc.printf("Loser :(\nScore: %i\n_________\n", counter); |
DnPlas | 0:b79d9ad8d1c3 | 24 | wait(1); |
DnPlas | 0:b79d9ad8d1c3 | 25 | } |
DnPlas | 0:b79d9ad8d1c3 | 26 | } |
DnPlas | 0:b79d9ad8d1c3 | 27 | |
DnPlas | 0:b79d9ad8d1c3 | 28 | void rst_game() //Prototype Function 2. This function reset the game |
DnPlas | 0:b79d9ad8d1c3 | 29 | { |
DnPlas | 0:b79d9ad8d1c3 | 30 | if (bttn2 ==0) |
DnPlas | 0:b79d9ad8d1c3 | 31 | { |
DnPlas | 0:b79d9ad8d1c3 | 32 | counter =100; |
DnPlas | 0:b79d9ad8d1c3 | 33 | pc.printf("Welcome back to the game!\nYour initial score is %i\n", counter); |
DnPlas | 0:b79d9ad8d1c3 | 34 | wait (1); |
DnPlas | 0:b79d9ad8d1c3 | 35 | } |
DnPlas | 0:b79d9ad8d1c3 | 36 | } |
DnPlas | 0:b79d9ad8d1c3 | 37 | |
DnPlas | 0:b79d9ad8d1c3 | 38 | |
DnPlas | 0:b79d9ad8d1c3 | 39 | int main () |
DnPlas | 0:b79d9ad8d1c3 | 40 | { |
DnPlas | 0:b79d9ad8d1c3 | 41 | while (1) |
DnPlas | 0:b79d9ad8d1c3 | 42 | { |
DnPlas | 0:b79d9ad8d1c3 | 43 | pot_val=pot; //Gives to the variable pot_val the value of the potenciometer |
DnPlas | 0:b79d9ad8d1c3 | 44 | chk_bttn(); //Checks if button 1 is pressed |
DnPlas | 0:b79d9ad8d1c3 | 45 | rst_game(); //Resets the game |
DnPlas | 0:b79d9ad8d1c3 | 46 | leds = on; //Turns on the leds |
DnPlas | 0:b79d9ad8d1c3 | 47 | wait ((pot_val*200+150)/1000); //Gives pauses to each led with the same value as the potenciometer |
DnPlas | 0:b79d9ad8d1c3 | 48 | on = on <<1; |
DnPlas | 0:b79d9ad8d1c3 | 49 | counter = 0; |
DnPlas | 0:b79d9ad8d1c3 | 50 | if (on==16) |
DnPlas | 0:b79d9ad8d1c3 | 51 | { |
DnPlas | 0:b79d9ad8d1c3 | 52 | on = 1; |
DnPlas | 0:b79d9ad8d1c3 | 53 | } |
DnPlas | 0:b79d9ad8d1c3 | 54 | } |
DnPlas | 0:b79d9ad8d1c3 | 55 | } |