Skip to content

Instantly share code, notes, and snippets.

@eduardodx
Created January 26, 2012 23:19
Show Gist options
  • Save eduardodx/1685774 to your computer and use it in GitHub Desktop.
Save eduardodx/1685774 to your computer and use it in GitHub Desktop.
Arduino - I2C Slave
#include <LiquidCrystal.h>
#include <Wire.h>
int x, y, resposta = 0;
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
void setup() {
lcd.begin(16, 2);
lcd.home();
Wire.begin(2);// Inicia a comunicação I2c, e este arduino possui endereço 2
Wire.onRequest(requestEvent);//Quando o master requisita algo, será executado a função requestEvent
Wire.onReceive(receiveEvent);//Quando for recebida alguma informação
}
// receiveEvent, pelo que eu entendi, howMany recebe quantos bytes estão disponiveis para a leitura
void receiveEvent(int howMany){
resposta = Wire.receive();
}
//requestEvent
void requestEvent(){
randomSeed (x);
y = random (256);
Wire.send (y);
}
//loop, escreve as informações no lcd (y é o que é enviado, e reposta é o que é recebido)
void loop(){
x++;
if (x >1000) x=0;
lcd.clear();
lcd.home();
lcd.print (y);
lcd.setCursor (0, 1);
lcd.print (resposta);
delay (100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment