Advertisement
Guest User

147852369

a guest
Nov 26th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #define NUMLED 4
  2. #define NUMBOT 4
  3. #define HOR 0
  4. #define ANTI 1
  5. #define MAIS 2
  6. #define MENOS 3
  7. #define LIGADO LOW
  8. #define DESLIGADO HIGH
  9. #define APERTADO LOW
  10. #define NAPERTADO HIGH
  11. #define TEMPOMAX 2000
  12.  
  13. long int time;
  14. int tecla = 0;
  15. int vetLed[NUMLED] = {6, 7, 8, 9};
  16. int vetBot[NUMBOT] = {2, 3, 4, 5};
  17. int curLed = 0;
  18. int tempo = 500;
  19. int sentido;
  20. int passo = 50;
  21.  
  22.  
  23. void setup() {
  24. Serial.begin(19200);
  25. for(int i = 0; i < NUMLED; i++){
  26. pinMode(vetLed[i], OUTPUT);
  27. pinMode(vetBot[i], INPUT);
  28. }
  29. }
  30.  
  31. void loop() {
  32. ligaLed();
  33. //for(int i = 0; i < tempo; i++){
  34. while((millis() - time) < tempo){
  35. checaTec();
  36. checaBot();
  37. //delay(1);
  38. }
  39. desligaLed();
  40. giraLed();
  41. }
  42.  
  43.  
  44. void ligaLed (){
  45. digitalWrite(vetLed[curLed], LIGADO);
  46. time = millis();
  47. }
  48.  
  49. void desligaLed (){
  50. digitalWrite(vetLed[curLed], DESLIGADO);
  51. }
  52.  
  53. void giraLed (){
  54. if(sentido == ANTI){
  55. curLed++;
  56. if(curLed >= NUMLED)
  57. curLed = 0;
  58.  
  59. }else{
  60. if(sentido == HOR){
  61. curLed--;
  62. if(curLed < 0)
  63. curLed = NUMLED - 1;
  64. }
  65. }
  66. }
  67.  
  68. void checaTec(){
  69. if(!Serial.available())
  70. return;
  71.  
  72. tecla = Serial.read();
  73.  
  74. switch(tecla){
  75. case 'a':
  76. case 'A':
  77. sentido = HOR;
  78. if(tecla == 'h') break;
  79. break;
  80.  
  81. case 'h':
  82. case 'H':
  83. sentido = ANTI;
  84. if(tecla == 'a') break;
  85. break;
  86.  
  87. case '+':
  88. case '=':
  89. tempo = tempo - passo;
  90. if(tempo <=50) tempo = passo;
  91. break;
  92.  
  93. case '-':
  94. case '_':
  95. tempo = tempo + passo;
  96. if(tempo >= 2000) tempo = 2000;
  97. break;
  98.  
  99. }
  100. }
  101.  
  102. void checaBot(){
  103. for(int i = 0; i < NUMBOT; i++){
  104. if(digitalRead(vetBot[i]) == APERTADO){
  105.  
  106. switch(i){
  107. case HOR: sentido = HOR;
  108. break;
  109.  
  110. case ANTI: sentido = ANTI;
  111. break;
  112.  
  113. case MAIS:
  114. tempo = tempo - passo;
  115. if(tempo <= passo) tempo = passo;
  116. break;
  117.  
  118. case MENOS:
  119. tempo = tempo + passo;
  120. if(tempo >= TEMPOMAX) tempo = TEMPOMAX;
  121. break;
  122.  
  123. }
  124. while(digitalRead(vetBot[i]) == APERTADO);
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement