In I2C, the combination of Master and Slave is a major principle.
Master always takes the initiative in communication, and Slave responds to requests from Master.
Therefore, it is not possible to communicate between Slave or send a request from Slave to Master.
#include<Wire.h>intx=0;voidsetup(){Wire.begin(8);//Secondary address is 8pinMode(13,OUTPUT);//Built in LEDWire.onReceive(receiveEvent);}voidreceiveEvent(intbytes){//INTERRUPTION 割り込みx=Wire.read();//read 1 from Master}voidloop(){if(x=='1'){digitalWrite(13,HIGH);//LED}if(x=='0'){digitalWrite(13,LOW);//LED}}
#include<Wire.h>intint_y=0;// charvoidsetup(){Wire.begin(8);//Secondary address is 8pinMode(5,INPUT);Wire.onRequest(requestEvent);}voidrequestEvent(){//INTERRUPTION 割り込みint_y=digitalRead(5);Wire.write(int_y);//write to Master}voidloop(){}
Master(write and request), Secondary(read and write)¶
Master
Secondary
Write “1” to Secondary …(1)
ISR onReceive run receiveEvent …(2)
input “1” from Master into x …(3)
“1” run ISR within if(x == `1`){ } …(4)
request 1 byte to Secondary …(5)
loof forever and wait from Secandary …(6)
ISR onRequest run requestEvent …(7)
read value from sensor and send to Master …(8)
Read value from secondary …(9)
Exit from forever loop by the value from Secondary …(10)
#include<Wire.h>intx=0;intint_y=0;voidsetup(){Wire.begin(8);//Secondary address is 8pinMode(5,INPUT);//Sensor pinpinMode(13,OUTPUT);//Built in LEDWire.onReceive(receiveEvent);// ...(2)Wire.onRequest(requestEvent);// ...(7)}voidreceiveEvent(intbytes){//Interrupt Service Routine 割り込みx=Wire.read();//read 1 from Master ...(3)}voidrequestEvent(){//ISR 割り込みint_y=digitalRead(5);// ...(8)Wire.write(int_y);//write to Master}voidloop(){if(x=='1'){// ...(4)//digitalWrite(13, HIGH);//LEDwhile(int_y==0){int_y=digitalRead(5);digitalWrite(13,HIGH);}//restdigitalWrite(13,LOW);//LEDx='0';}}
#include<Servo.h>//#include <Servo_megaTinyCore.h>#include<Wire.h>intweight1=0;intmasu=85;//weight of masuintsake=30;//weight of sakeintpour=0;//int x = 1;intledPin=12;// LED connected to digital pin 12//int switchPin = 7; // Switch to behave scale weightintswitchPin=A3;// Switch to behave scale weight(pro micro)Servomyservo1;// create servo1 object to control a servoServomyservo2;// create servo2 object to control a servointpos=0;// change the servo degreesintpos_now=1;intLiquid_level=0;// received liquid_level from Secondary//int int_x = 0;voidsetup(){Serial.begin(9600);myservo1.attach(6);// attaches the servo1 on pin 8 (PWM) to the servo objectmyservo2.attach(9);// attaches the servo2 on pin 9 (PWM) to the servo object//Set the servos to initial positionsmyservo1.write(90);myservo2.write(135);pinMode(ledPin,OUTPUT);digitalWrite(ledPin,HIGH);//Wire.swap(1);Wire.begin();}voidloop(){Serial.println("Loop");intsensorValue=analogRead(switchPin);floatweight1=sensorValue*((masu+sake)/1023.0);Serial.println(weight1);if(weight1<75){// do nothingSerial.println("do nothing under 75g");}elseif(weight1>90){// do nothingSerial.println("do nothing over 90g");}else{// Servo1if((weight1>=75)&&(weight1<=90)){//If the weight1 is 75-90gSerial.println("masu is ON");for(pos=90;pos>=0;pos-=1){myservo1.write(pos);// tell servo1 to go to position in variable 'pos'delay(30);// waits 30ms for the servo to reach the position}//mp3Wire.beginTransmission(8);// Secondary address is 8Wire.write("2",strlen("2"));// Send 1 to secondaryWire.endTransmission();Serial.print("sent ");Serial.println("2");delay(500);//OLEDWire.beginTransmission(9);// Secondary address is 9Wire.write("3",strlen("3"));// Send 1 to secondaryWire.endTransmission();Serial.print("sent ");Serial.println("3");delay(500);// Servo2for(pos=135;pos>=55;pos-=1){// goes from 135 degrees to 55 degreesmyservo2.write(pos);sensorValue=analogRead(switchPin);weight1=sensorValue*((masu+sake)/1023.0);Serial.println(weight1);Serial.println("pouring quickly");pos_now=pos;if(weight1>=(masu+sake/3)){//85 + 30/3=95gbreak;}delay(50);}//in here, weight1 >= masu + sake / 3// Servo2for(pos=pos_now;pos>=45;pos-=1){// goes from 55 degrees to 45 degreesmyservo2.write(pos);sensorValue=analogRead(switchPin);weight1=sensorValue*((masu+sake)/1023.0);Serial.println(weight1);Serial.println("pouring slowly");pos_now=pos;if(weight1>=(masu+sake)){//85 + 30=115gbreak;}delay(300);}// in here, weight1 > masu + sake// Servo2for(pos=pos_now;pos<=135;pos+=1){// goes from 45 degrees to 135 degreesmyservo2.write(pos);delay(50);}// Servo1for(pos=0;pos<=90;pos+=1){// goes from 0 degrees to 90 degrees// in steps of 1 degreemyservo1.write(pos);delay(30);//}pour=pour+1;Serial.print("pour= ");Serial.print(pour);Serial.println(" not_empty");delay(500);if(pour>=3){Serial.println("empty now request to Refill");delay(3000);Wire.beginTransmission(8);// Secondary address is 8Wire.write("1",strlen("1"));// Send 1 to secondaryWire.endTransmission();Serial.print("sent ");Serial.println("1");delay(3000);while(1){// loop foreverWire.requestFrom(8,1);// request 1 bytes from secondary device #8Serial.println("request Liquid_level");while(Wire.available()){Liquid_level=Wire.read();// receive from secondarySerial.print("Liquid_level=");Serial.println(char(Liquid_level));// "1" is ASCII code 49}if(Liquid_level=='1'){//Liquid is fullbreak;}delay(5000);}//while(1)Serial.println("break");pour=0;}//if (pour >= 3)}//if}//elsedelay(500);}//loop
% ls /dev | grep usb
tty.usbmodem14301
tty.usbserial-D307RG9V
% screen /dev/tty.usbmodem14301 9600
// command + n // open new Terminal
% screen /dev/tty.usbserial-D307RG9V 9600
There are both 7- and 8-bit versions of I2C addresses. 7 bits identify the device, and the eighth bit determines if it’s being written to or read from. The Wire library uses 7 bit addresses throughout. If you have a datasheet or sample code that uses 8 bit address, you’ll want to drop the low bit (i.e. shift the value one bit to the right), yielding an address between 0 and 127. However the addresses from 0 to 7 are not used because are reserved so the first address that can be used is 8. Please note that a pull-up resistor is needed when connecting SDA/SCL pins. Please refer to the examples for more informations. MEGA 2560 board has pull-up resistors on pins 20 - 21 onboard.