segunda-feira, 3 de dezembro de 2012

codigo 16ch usando o switch case


 void setup()
     {
       Serial.begin(9600);
      
       pinMode(2, OUTPUT);
       pinMode(3, OUTPUT);
       pinMode(4, OUTPUT);
       pinMode(5, OUTPUT);
       pinMode(6, OUTPUT);
       pinMode(7, OUTPUT);
       pinMode(8, OUTPUT);
       pinMode(9, OUTPUT);
       pinMode(10,OUTPUT);
       pinMode(11,OUTPUT);
       pinMode(12,OUTPUT);
       pinMode(22,OUTPUT);
       pinMode(23,OUTPUT);
       pinMode(24,OUTPUT);
       pinMode(25,OUTPUT);
       pinMode(26,OUTPUT);
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, LOW);
       digitalWrite(6, LOW);
       digitalWrite(7, LOW);
       digitalWrite(8, LOW);
       digitalWrite(9, LOW);
       digitalWrite(10,LOW);
       digitalWrite(11,LOW);
       digitalWrite(12,LOW);
       digitalWrite(22,LOW);
       digitalWrite(23,LOW);
       digitalWrite(24,LOW);
       digitalWrite(25,LOW);
       digitalWrite(26,LOW);
     }

     void loop(){
    
        if (Serial.available()){
      
          char c = Serial.read();
    // apenas uma leitura por cada iteração do loop
    // em seguida faz os testes para ver onde o dado recebido da serial se encaixa

          
          switch(c){   
         
             case 'w':
                digitalWrite(2, HIGH);
                digitalWrite(4, HIGH);
                Serial.println("Frente");
                delay(100);          
                digitalWrite(2, LOW);
                digitalWrite(4, LOW);
                break;

             case 's':
                digitalWrite(3, HIGH);
                digitalWrite(5, HIGH);
                Serial.println("Trás");
                delay(100);          
                digitalWrite(3, LOW);
                digitalWrite(5, LOW);
                break;

             case 'a':
                digitalWrite(2, HIGH);
                digitalWrite(5, HIGH);
                Serial.println("Direita");
                delay(100);          
                digitalWrite(2, LOW);
                digitalWrite(5, LOW);
                break;

             case 'd':
                digitalWrite(3, HIGH);
                digitalWrite(4, HIGH);
                Serial.println("Esquerda");
                delay(100);          
                digitalWrite(3, LOW);
                digitalWrite(4, LOW);
                break;

             case 'q':
                digitalWrite(6, HIGH);
                Serial.println("gir.e");
                delay(100);          
                digitalWrite(6, LOW);
                break;

             case 'e':
                digitalWrite(7, HIGH);
                Serial.println("gir.d");
                delay(100);          
                digitalWrite(7, LOW);
                break;

             case 'r':
                digitalWrite(8,HIGH);
                Serial.println("bras.s");
                delay(100);          
                digitalWrite(8,LOW);
                break;

             case 'f':
                digitalWrite(9,HIGH);
                Serial.println("bras.d");
                delay(100);          
                digitalWrite(9,LOW);
                break;           

             case 't':
                digitalWrite(10,HIGH);
                Serial.println("a.bras.s");
                delay(100);          
                digitalWrite(10,LOW);
                break;

             case 'g':
                digitalWrite(11,HIGH);
                Serial.println("a.bras.d");
                delay(100);          
                digitalWrite(11,LOW);
                break;              

             case 'y':
                digitalWrite(12,HIGH);
                Serial.println("pul.s");
                delay(100);          
                digitalWrite(12,LOW);
                break;

             case 'h':
                digitalWrite(22,HIGH);
                Serial.println("pul.d");
                delay(100);          
                digitalWrite(22,LOW);
                break;

             case 'i':
                digitalWrite(23,HIGH);
                Serial.println("gpul.h");
                delay(100);
                digitalWrite(23,LOW);
                break;

             case 'u':
                digitalWrite(24,HIGH);
                Serial.println("gpul.a");
                delay(100);
                digitalWrite(24,LOW);
                break;

             case 'j':
                digitalWrite(25,HIGH);
                Serial.println("pin.a");
                delay(100);
                digitalWrite(25,LOW);
                break;

             case 'k':
                digitalWrite(26,HIGH);
                Serial.println("pin.f");
                delay(100);
                digitalWrite(26,LOW);
                break;
             }

          }
    }

Um comentário: