Advertisement
odilonafonso

TesteMillisLabGar

Jul 12th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #define INTERVALO 2000
  2. #define LED1 12
  3. #define LED2 13
  4. #define LED3 14
  5. #define LED4 15
  6.  
  7. String comando = "x";
  8. long inicio, now;
  9. bool led1_aceso = false;
  10. bool led2_aceso = false;
  11. bool led3_aceso = false;
  12. bool led4_aceso = false;
  13. bool startLeds = true;
  14. bool pegarComando = true;
  15.  
  16. void setup(){
  17.     Serial.begin(9600);
  18.     Serial.println("Start...");
  19.     pinMode(LED1, OUTPUT);
  20.     pinMode(LED2, OUTPUT);
  21.     pinMode(LED3, OUTPUT);
  22.     pinMode(LED4, OUTPUT);
  23.     digitalWrite(LED1,LOW);
  24.     digitalWrite(LED2,LOW);
  25.     digitalWrite(LED3,LOW);
  26.     digitalWrite(LED4,LOW);
  27.     delay(500);
  28. }
  29.  
  30. #define TIMEOUT_SERIAL 3000
  31. String readSerial(String msg){
  32.    String resp = "";
  33.    Serial.setTimeout(TIMEOUT_SERIAL);
  34.    if (msg != "")
  35.       Serial.print(msg);
  36.    resp = Serial.readStringUntil(10);
  37.    if (resp != ""){
  38.       pegarComando = false;
  39.       Serial.print("\n{");
  40.       Serial.print(resp);
  41.       Serial.println ("}");
  42.    }
  43.    else {
  44.       resp = comando; //mantem valor anterior
  45.       Serial.println();
  46.    }
  47.    return resp;
  48. }
  49.  
  50. void loop() {
  51.    if ( pegarComando ) {
  52.       comando = readSerial("comando:");
  53.    }
  54.    if ( comando.indexOf("0") >= 0 ) {
  55.       if (startLeds == true) {
  56.          startLeds = false;
  57.          inicio = millis();
  58.       }
  59.       now = millis();
  60.       if ( (now - inicio) > INTERVALO )
  61.          if (led1_aceso == false) {
  62.             digitalWrite(LED1,HIGH);
  63.             led1_aceso = true;
  64.             Serial.println("acendeu led1");
  65.          }
  66.       if ( (now - inicio) > INTERVALO*2 )
  67.          if (led2_aceso == false) {
  68.             digitalWrite(LED2,HIGH);
  69.             led2_aceso = true;
  70.             Serial.println("acendeu led2");
  71.          }
  72.       if ( (now - inicio) > INTERVALO*3 )
  73.          if (led3_aceso == false) {
  74.             digitalWrite(LED3,HIGH);
  75.             led3_aceso = true;
  76.             Serial.println("acendeu led3");
  77.          }
  78.       if ( (now - inicio) > INTERVALO*4 )
  79.          if (led4_aceso == false) {
  80.             digitalWrite(LED4,HIGH);
  81.             led4_aceso = true;
  82.             Serial.println("acendeu led4");
  83.          }
  84.       if ( (now - inicio) > INTERVALO*5 ) {
  85.          startLeds = true;
  86.          pegarComando = true;
  87.          comando = "x";
  88.          led1_aceso = led2_aceso = led3_aceso = led4_aceso = false;
  89.          digitalWrite(LED1,LOW);
  90.          digitalWrite(LED2,LOW);
  91.          digitalWrite(LED3,LOW);
  92.          digitalWrite(LED4,LOW);
  93.          Serial.println("apagou tudo");
  94.       }
  95.    }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement