Hi!
I would like to establish a two-way communication between Arduino 1 and Arduino 2 using I2C.
Arduino 1 has a weight-sensor with an HX711 connected to it & a ZS040 bluetooth module.
Arduino 2 has a button and a LED connected to it.
If the weight sensor exceeds a certain weight-limit, the LED at Arduino 2 should light up.
If the LED is lit AND the button connected to Arduino 2 is pressed, Arduino 1 should send a bluetooth-signal to the computer to initiate Mouse.press.
At the moment Arduino 2 is the Master with this code:
#include <Wire.h>
int LED = 13;
int button = 2;
int mouse_send = 0;
void setup(){
Wire.begin();
Serial.begin(9600);
pinMode(LED, OUTPUT);
pinMode(button, INPUT);
}
void loop(){
buttonState = digitalRead(button);
Wire.requestFrom(8,2);
while (Wire.available()) {
int weight_received = Wire.read();
Serial.println(weight_received);
if (weight_received == 2){
digitalWrite(LED, HIGH);
}
if (weight_received == 3){
digitalWrite(LED, LOW);
}
if (weight_received == 2 && button == HIGH)){
mouse_send = 4;
Wire.beginTransmission(8);
Wire.write(mouse);
Wire.endTransmission();
}
if (button == LOW)){
mouse_send = 5;
Wire.beginTransmission(8);
Wire.write(mouse);
Wire.endTransmission();
}
}
}
And Arduino 1 is the Slave with this code:
#include "HX711.h"
#include <Wire.h>
#include <Mouse.h>
#define DOUT 5
#define CLK 4
HX711 scale(DOUT, CLK);
float calibration_factor = -7050;
int weight = 0;
void setup(){
Wire.begin(8);
Serial.begin(9600);
Wire.onReceive(receiveEvent);
scale.tare();
long zero_factor = scale.read_average();
Serial.print("Zero factor: ");
}
void receiveEvent(int bytes){
int mouse_received = Wire.read();
if (mouse_received == 4){
Mouse.press();
}
if (mouse_received == 5){
Mouse.release();
}
}
void loop(){
scale.set_scale(calibration_factor);
delay(200);
Serial.println(scale.get_units());
if (scale.get_units() < -40.00) {
int weight = 2;
Wire.write(weight);
}
if (scale.get_units() > -40.00) {
int weight = 3;
Wire.write(weight);
}
}
The bluetooth is not connected because it didn't work like this connected to the laptop yet...
I think the problem is probably that I didn't understand the I2C communication completely.
Hope you can help and thanks in advance!