hi and good morning,
i'm a newbie in embedded programing and arduino, and now, i'm using arduino uno for my study, the problem is, i dont know how/where to begin for i2c,
i already look for tutorial at arduino tutorial/forum and
http://www.gammon.com.au/forum/?id=10896, but i don't understand it.
i want to connect arduino 1 and arduino 2, where's arduino 1 have a switch, when the swicth is press it can give a signal to arduino 2 to on the led, when the switch is release the led at arduino 2 will off.
below is my program( i refer from arduino tutorial) :
Master:-
#include <Wire.h>
int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
// initialize the LED pin as an output:
pinMode(13, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(2, INPUT);
Wire.begin(); // join i2c bus (address optional for master)
}
void loop()
{
buttonState = digitalRead(2);
if (buttonState == HIGH){
// turn LED on:
digitalWrite(13,HIGH);
Wire.beginTransmission(4); // transmit to device #4
Wire.send(buttonState);
Wire.endTransmission();
}
}
Slave:-
#include <Wire.h>
const int ledPin = 12; // the number of the LED pin
void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
pinMode(ledPin, OUTPUT);
}
void loop()
{
delay(500); //Wait 2 Second before re-excute
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
int x = Wire.receive(); // receive byte come un integer
if (x == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
Serial.println(x);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
Serial.println(x);
}
}
i really appreciate your help.. thank you :p