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);
 }
}
the program at aduino 1 is master, and arduino 2 is slave, when i push the pushbutton at master(arduino1), nothing happen at slave(i mean no led on at arduino 2,) the led at arduino 1 to indicate if the pushbutton is push at master, and the arduino 2 appear the result with led on or off
yes, i connected with analog input 4 and 5 master to slave.
The master code sends a HIGH but never sends a LOW.
It also sends lots and lots of HIGHs for as long as the button returns HIGH.
How are the buttons wired? It is best to wire then between input and ground and enable the internal pull up resistors.
Also do something at the master end so you can see it is sending something, like toggle the LED.
Did you mean ground at master and slave must be connected together?
Yes, and put a 4K7 resistor from A4 to +5V and another one from A5 to +5V
did you mean I2C always send high only?
Yes, the code still does, you need to send LOW when the switch CHANGES to a low state.
/ i dont understand in this part, it is boolean or math expression?
Boolean IS maths.
//what != it use be?
It means " not equal to "
if (digitalRead(BUTTON) != last_state){
Means if what you read is not equalt to what it was last time. Now that code is wrong, you need to set last_state to the current state at the end of the loop. That loop function should look like:-
boolean last_state = HIGH;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
void loop() {Â
int button_state = digitalRead(BUTTON)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
 if (button_state != last_state){         Â
  last_state = button_state;
  Serial.println("Start");
  Wire.beginTransmission(ADDRESS);
  Serial.println("Beginning transmission");
  Wire.send(button_state);
  Serial.println("Sent Data");
  Wire.endTransmission();
  Serial.println("Ended transmission");
 }
}
Look you are asking for advice and I am giving it.
Yes that photo is insufficient. Why do you think I told you about pull up resistors.
If you don't want to believe me then don't bother asking.
Read the details here if you can understand it.
for this recievEvent function it is compulsary to add void receiveEvent(int howMany)
Yes it is known as a call back function, it is triggered when the I2C circuits detect something arriving.
and to write the while (Wire.available() > 0)
The while is a command that causes a loop to happen, it is used here to ensure that you gather all the bytes that are currently waiting in the I2C buffer. So it is not compulsory but you need to replace it with something that will do the same job.
boolean b = Wire.receive();Â Â // this also
This creates a variable of type boolean to put the received byte from the I2C interface into. It needs to go somewhere and in this example you only need a logic variable (boolean) that is high or low.