| 
      1
     | 
    
      #include <Servo.h>
 
     | 
  
  
    | 
      2
     | 
    
      
 
     | 
  
  
    | 
      3
     | 
    
      
 
     | 
  
  
    | 
      4
     | 
    
      Servo ServoMot_1;
 
     | 
  
  
    | 
      5
     | 
    
      Servo ServoMot_2;
 
     | 
  
  
    | 
      6
     | 
    
      //int left=90;
 
     | 
  
  
    | 
      7
     | 
    
      //int right=90;
 
     | 
  
  
    | 
      8
     | 
    
      int posLR=90;
 
     | 
  
  
    | 
      9
     | 
    
      int posUD=110;
 
     | 
  
  
    | 
      10
     | 
    
      //int up=110;
 
     | 
  
  
    | 
      11
     | 
    
      //int down=110;
 
     | 
  
  
    | 
      12
     | 
    
      
 
     | 
  
  
    | 
      13
     | 
    
      void setup()
 
     | 
  
  
    | 
      14
     | 
    
      {
     | 
  
  
    | 
      15
     | 
    
      
 
     | 
  
  
    | 
      16
     | 
    
        
 
     | 
  
  
    | 
      17
     | 
    
      ServoMot_1.attach(5);
 
     | 
  
  
    | 
      18
     | 
    
      ServoMot_2.attach(6);
 
     | 
  
  
    | 
      19
     | 
    
      ServoMot_1.write(90);
 
     | 
  
  
    | 
      20
     | 
    
      ServoMot_2.write(110);
 
     | 
  
  
    | 
      21
     | 
    
      
 
     | 
  
  
    | 
      22
     | 
    
      //Setup Canale  A
 
     | 
  
  
    | 
      23
     | 
    
      pinMode(12, OUTPUT); //Inizializza  Canale A pin
 
     | 
  
  
    | 
      24
     | 
    
      pinMode(9, OUTPUT); //Inizializza Fermo Canale A pin
 
     | 
  
  
    | 
      25
     | 
    
      pinMode(13, OUTPUT); //Inizializza  Canale B pin
 
     | 
  
  
    | 
      26
     | 
    
      pinMode(8, OUTPUT); //Inizializza Fermo Canale B pin
 
     | 
  
  
    | 
      27
     | 
    
      
 
     | 
  
  
    | 
      28
     | 
    
      Serial.begin(9600);
 
     | 
  
  
    | 
      29
     | 
    
      
 
     | 
  
  
    | 
      30
     | 
    
      
 
     | 
  
  
    | 
      31
     | 
    
      
 
     | 
  
  
    | 
      32
     | 
    
      }
 
     | 
  
  
    | 
      33
     | 
    
      
 
     | 
  
  
    | 
      34
     | 
    
      
 
     | 
  
  
    | 
      35
     | 
    
      void loop()
 
     | 
  
  
    | 
      36
     | 
    
      {
     | 
  
  
    | 
      37
     | 
    
        
 
     | 
  
  
    | 
      38
     | 
    
      
 
     | 
  
  
    | 
      39
     | 
    
       if (Serial.available()) {
     | 
  
  
    | 
      40
     | 
    
          int inByte = Serial.read();
 
     | 
  
  
    | 
      41
     | 
    
       
 
     | 
  
  
    | 
      42
     | 
    
           // UP
 
     | 
  
  
    | 
      43
     | 
    
          if (inByte==117 && posUD <= 179) 
 
     | 
  
  
    | 
      44
     | 
    
          {         
     | 
  
  
    | 
      45
     | 
    
            posUD = posUD + 1 ;
 
     | 
  
  
    | 
      46
     | 
    
            ServoMot_2.write(posUD);  
 
     | 
  
  
    | 
      47
     | 
    
          }
 
     | 
  
  
    | 
      48
     | 
    
          // DOWN
 
     | 
  
  
    | 
      49
     | 
    
          if (inByte==100 && posUD >= 80) 
 
     | 
  
  
    | 
      50
     | 
    
          {     
     | 
  
  
    | 
      51
     | 
    
            posUD = posUD - 1 ;
 
     | 
  
  
    | 
      52
     | 
    
            ServoMot_2.write(posUD);
 
     | 
  
  
    | 
      53
     | 
    
          } 
 
     | 
  
  
    | 
      54
     | 
    
           //Left
 
     | 
  
  
    | 
      55
     | 
    
          if (inByte==108 && posLR >= 1) {
     | 
  
  
    | 
      56
     | 
    
            posLR = posLR - 1 ;
 
     | 
  
  
    | 
      57
     | 
    
            ServoMot_1.write(posLR);
 
     | 
  
  
    | 
      58
     | 
    
          
 
     | 
  
  
    | 
      59
     | 
    
          }
 
     | 
  
  
    | 
      60
     | 
    
          //Right
 
     | 
  
  
    | 
      61
     | 
    
          if (inByte==114 && posLR <= 179) 
 
     | 
  
  
    | 
      62
     | 
    
          {
     | 
  
  
    | 
      63
     | 
    
            posLR = posLR + 1 ;
 
     | 
  
  
    | 
      64
     | 
    
            ServoMot_1.write(posLR);
 
     | 
  
  
    | 
      65
     | 
    
          
 
     | 
  
  
    | 
      66
     | 
    
          }
 
     | 
  
  
    | 
      67
     | 
    
          
 
     | 
  
  
    | 
      68
     | 
    
       
 
     | 
  
  
    | 
      69
     | 
    
      
 
     | 
  
  
    | 
      70
     | 
    
       // Controllo Motori
 
     | 
  
  
    | 
      71
     | 
    
      
 
     | 
  
  
    | 
      72
     | 
    
       
 
     | 
  
  
    | 
      73
     | 
    
           // AVANTI
 
     | 
  
  
    | 
      74
     | 
    
          if (inByte==97) 
 
     | 
  
  
    | 
      75
     | 
    
          {         
     | 
  
  
    | 
      76
     | 
    
            digitalWrite(12, LOW);  // AVANTI Motore A
 
     | 
  
  
    | 
      77
     | 
    
            digitalWrite(13, HIGH); // AVANTI Motore B
 
     | 
  
  
    | 
      78
     | 
    
            digitalWrite(9, LOW);   //Sblocco  A
 
     | 
  
  
    | 
      79
     | 
    
            digitalWrite(8, LOW);   //Sblocco B
 
     | 
  
  
    | 
      80
     | 
    
            analogWrite(3, 255);   // Velocità Motore A
 
     | 
  
  
    | 
      81
     | 
    
            analogWrite(11, 255);   // Velocità Motore B
 
     | 
  
  
    | 
      82
     | 
    
            delay(10);
 
     | 
  
  
    | 
      83
     | 
    
            digitalWrite(9, HIGH); //Blocco Motore A
 
     | 
  
  
    | 
      84
     | 
    
            digitalWrite(8, HIGH); //Blocco Motore B
 
     | 
  
  
    | 
      85
     | 
    
          }
 
     | 
  
  
    | 
      86
     | 
    
          // INDIETRO
 
     | 
  
  
    | 
      87
     | 
    
          if (inByte==105 ) 
 
     | 
  
  
    | 
      88
     | 
    
          {     
     | 
  
  
    | 
      89
     | 
    
            digitalWrite(12, HIGH);  // INDIETRO Motore A
 
     | 
  
  
    | 
      90
     | 
    
            digitalWrite(13, LOW); // INDIETRO Motore B
 
     | 
  
  
    | 
      91
     | 
    
            digitalWrite(9, LOW);   //Sblocco  A
 
     | 
  
  
    | 
      92
     | 
    
            digitalWrite(8, LOW);   //Sblocco B
 
     | 
  
  
    | 
      93
     | 
    
            analogWrite(3, 255);   // Velocità Motore A
 
     | 
  
  
    | 
      94
     | 
    
            analogWrite(11, 255);   // Velocità Motore B
 
     | 
  
  
    | 
      95
     | 
    
            delay(10);
 
     | 
  
  
    | 
      96
     | 
    
            digitalWrite(9, HIGH); //Blocco Motore A
 
     | 
  
  
    | 
      97
     | 
    
            digitalWrite(8, HIGH); //Blocco Motore B
 
     | 
  
  
    | 
      98
     | 
    
          } 
 
     | 
  
  
    | 
      99
     | 
    
           //Left 's' = 115 (ascii)
 
     | 
  
  
    | 
      100
     | 
    
          if (inByte==115) {
     | 
  
  
    | 
      101
     | 
    
            digitalWrite(12, LOW);  // INDIETRO Motore A
 
     | 
  
  
    | 
      102
     | 
    
            digitalWrite(13, LOW); // INDIETRO Motore B
 
     | 
  
  
    | 
      103
     | 
    
            digitalWrite(9, LOW);   //Sblocco  A
 
     | 
  
  
    | 
      104
     | 
    
            digitalWrite(8, LOW);   //Sblocco B
 
     | 
  
  
    | 
      105
     | 
    
            analogWrite(3, 255);   // Velocità Motore A
 
     | 
  
  
    | 
      106
     | 
    
            analogWrite(11, 255);   // Velocità Motore B
 
     | 
  
  
    | 
      107
     | 
    
            delay(10);
 
     | 
  
  
    | 
      108
     | 
    
            digitalWrite(9, HIGH); //Blocco Motore A
 
     | 
  
  
    | 
      109
     | 
    
            digitalWrite(8, HIGH); //Blocco Motore B    
 
     | 
  
  
    | 
      110
     | 
    
      
 
     | 
  
  
    | 
      111
     | 
    
          
 
     | 
  
  
    | 
      112
     | 
    
          }
 
     | 
  
  
    | 
      113
     | 
    
          //Right 'e' = 101 (ascii)
 
     | 
  
  
    | 
      114
     | 
    
          if (inByte==101) 
 
     | 
  
  
    | 
      115
     | 
    
          {
     | 
  
  
    | 
      116
     | 
    
            digitalWrite(12, HIGH);  // INDIETRO Motore A
 
     | 
  
  
    | 
      117
     | 
    
            digitalWrite(13, HIGH); // INDIETRO Motore B
 
     | 
  
  
    | 
      118
     | 
    
            digitalWrite(9, LOW);   //Sblocco  A
 
     | 
  
  
    | 
      119
     | 
    
            digitalWrite(8, LOW);   //Sblocco B
 
     | 
  
  
    | 
      120
     | 
    
            analogWrite(3, 255);   // Velocità Motore A
 
     | 
  
  
    | 
      121
     | 
    
            analogWrite(11, 255);   // Velocità Motore B
 
     | 
  
  
    | 
      122
     | 
    
            delay(10);
 
     | 
  
  
    | 
      123
     | 
    
            digitalWrite(9, HIGH); //Blocco Motore A
 
     | 
  
  
    | 
      124
     | 
    
            digitalWrite(8, HIGH); //Blocco Motore B    
 
     | 
  
  
    | 
      125
     | 
    
          
 
     | 
  
  
    | 
      126
     | 
    
          }
 
     | 
  
  
    | 
      127
     | 
    
          
 
     | 
  
  
    | 
      128
     | 
    
        }
 
     | 
  
  
    | 
      129
     | 
    
       
 
     | 
  
  
    | 
      130
     | 
    
       
 
     | 
  
  
    | 
      131
     | 
    
      }
 
     |