domenica 14 dicembre 2014

TRASMISSIONE I2C - 3a parte


ARDUINO MASTER SLAVE
    
    //MASTER
    #include <Wire.h>
   
    #define ADDRESS1 2
   
    #define LED1  8
    #define LED2  9
    #define LED3 10
    #define LED4 11  
    
    int NumByte = 1;
  
    void setup()
       
        { Wire.begin();        
          pinMode (LED1,OUTPUT);
          pinMode (LED2,OUTPUT);
          pinMode (LED3,OUTPUT);
          pinMode (LED4,OUTPUT); }
  
    void loop()  {
    

      Wire.requestFrom(ADDRESS1,NumByte); 
      char c = Wire.read(); 
      
      //Se la stringa in rx è a accendo LED1
      String a = "";
      a = a + c;
      delay (100);
      if ( a == "a")
         { digitalWrite(LED1, HIGH); }
      else
         { digitalWrite(LED1, LOW); }
      delay(10);
   
      //Se la stringa in rx è b accendo LED2
     String b = "";
     b = b + c;
      delay (100);
      if ( b == "b")
      { digitalWrite(LED2, HIGH); }
      else
         { digitalWrite(LED2, LOW); }
      delay(10);
   
     //Se la stringa in rx è g accendo LED3
      String g = "";
      g = g + c;
      if ( g == "c")
         { digitalWrite(LED3, HIGH); }
      else
         { digitalWrite(LED3, LOW); }
      delay(10);
   
    //Se la stringa in rx è da accendo LED4
    String d = "";
     d = d + c;
      delay (100);
      if ( d == "d")
         { digitalWrite(LED4, HIGH); }
      else
         { digitalWrite(LED4, LOW); }
      delay(10);
 
  }




ARDUINO SLAVE CODE

    //  SLAVE VERONA
    #include <Wire.h>
    #define ADDRESS 2
    
    #define SWITCH1 2
    #define SWITCH2 3
    #define SWITCH3 4
    #define SWITCH4 5
    
    void setup()
   
        { pinMode(SWITCH1, INPUT);
          pinMode(SWITCH2, INPUT);
          pinMode(SWITCH3, INPUT);
          pinMode(SWITCH4, INPUT);
          Wire.begin(ADDRESS);     
          Wire.onRequest(requestEvent);  }
   
    void loop(){
         delay(100);
         }
   
    void requestEvent() {
      
      if ( digitalRead(SWITCH1) == HIGH)
         { Wire.write("a");     }
   
      if ( digitalRead(SWITCH2) == HIGH)
         { Wire.write("b");     }
  
      if ( digitalRead(SWITCH3) == HIGH)
         { Wire.write("c");     }
   
      if ( digitalRead(SWITCH4) == HIGH)
         { Wire.write("d");     }

}


Nessun commento:

Posta un commento