Skip to content

Instantly share code, notes, and snippets.

@sonnens
Created October 14, 2014 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonnens/cbcd491229df5ffa199a to your computer and use it in GitHub Desktop.
Save sonnens/cbcd491229df5ffa199a to your computer and use it in GitHub Desktop.
#include <Wire.h>
#define SLAVE_ADDRESS 0x04
int number = 0;
int state = 0;
int moist;
void setup() {
pinMode(13, OUTPUT);
// initialize i2c as slave
Wire.begin(SLAVE_ADDRESS);
// define callbacks for i2c communication
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
}
void loop() {
delay(100);
moist = GetMoist();
}
// callback for received data
void receiveData(int byteCount){
while(Wire.available()) {
number = Wire.read();
if (number == 1){
if (state == 0){
digitalWrite(13, HIGH); // set the LED on
state = 1;
} else{
digitalWrite(13, LOW); // set the LED off
state = 0;
}
}
if(number==2) {
number = (int)moist;
}
}
}
// callback for sending data
void sendData(){
Wire.write(number);
}
// Get the internal temperature of the arduino
int GetMoist(void)
{
return(analogRead(A2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment