I'm new at using Arduino. And i got a task from my lecture to build this things
// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include "TextCommand.h"
#include <Wire.h>
TextCommand cmd(&Serial, 100);
TextCommand i2c(&Wire, 100);
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void SerCommand() {
Wire.beginTransmission(2);
int n = cmd.available();
if (n > 0) {
if (cmd.isWrite() == 0) {
if (cmd.ucAddress() == 0)
switch (cmd.ioAddress()) {
case 0:
{
Serial.println("cek koneksi ");
Wire.println("!R0200");
Wire.endTransmission();
Wire.requestFrom(2, 8); // request 6 bytes from slave device #8
Wire.available(); { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.println("!R0000"); // print the character
}
break;
}
}}}}
void loop() {
SerCommand();
}
and below is the slave program
#include <Wire.h>
#include <TextCommand.h>
TextCommand i2c(&Wire, 100);
TextCommand cmd(&Serial, 100);
void setup() {
Wire.begin(2); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void loop() {
i2cCommand();
}
void i2cCommand()
{
int n = i2c.available();
if ( n > 0)
{
if (i2c.isWrite() == 0) {
if (i2c.ucAddress() == 2) {
switch (i2c.ioAddress()) {
case 0:
Serial.println("!R0000");
Wire.write ("c");
Serial.println ("!R0200");
}
}
}
}
}
void receiveEvent(int howMany)
{
i2cCommand();
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
i2cCommand(); // respond with message of 6 bytes
// as expected by master
}
I need to sent back the data from the slave. but i dont know how to do it. is there anyone can help me with the case?