domenica 14 dicembre 2014

TRASMISSIONE I2C - 2a parte


La pressione del tasto su Arduino Slave determina l'accensione del led su Arduino Master

ARDUINO MASTER CODE

    //MASTER
    #include <Wire.h>
   
    #define ADDRESS1 2
   
    #define LED1 8
   
    int NumByte = 1;
  
    void setup()
       
        { Wire.begin();        
          pinMode (LED1,OUTPUT); }
  
    void loop()  {
    

      Wire.requestFrom(ADDRESS1,NumByte); 
      String b = "";
      char c = Wire.read(); 
      b = b + c;
      delay (100);
      if ( b == "a")
         { digitalWrite(LED1, HIGH); }
      else
         { digitalWrite(LED1, LOW); }
      delay(10);
    }




ARDUINO SLAVE CODE
    //  SLAVE VERONA
    #include <Wire.h>
    #define ADDRESS 2
    # define SWITCH 2
   
    void setup()
   
        { pinMode(SWITCH, INPUT);
          Wire.begin(ADDRESS);     
          Wire.onRequest(requestEvent);  }
   
    void loop(){
         delay(100);
         }
   
    void requestEvent() {
      
      if ( digitalRead(SWITCH) == HIGH)
         { Wire.write("a");     }
      else
         { Wire.write("s"); }
   
  }


Nessun commento:

Posta un commento