Created
February 12, 2017 10:50
-
-
Save zinntikumugai/16ffb82b50652b1c966f2e25d8c55226 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
int cnt = 0; | |
void setup() { | |
Wire.begin(); //Masterで起動 | |
Serial.begin(9600); | |
Serial.println(""); | |
} | |
void loop() { | |
Wire.requestFrom(8, 1); //ID:8のSlaveに1Byteリクエスト | |
while (Wire.available()) { | |
char c = Wire.read(); | |
Serial.print(c); | |
if( cnt == 4 ) //4文字出力したら「 」(スペース)を入れる | |
Serial.print(' '); | |
else if( cnt == 8 ) { //8文字分出力したら改行 | |
Serial.println(""); | |
cnt = 0; | |
} | |
cnt++; | |
} | |
delay(500); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#define BTNPIN 5 //ボタンのPIN番号 | |
char list[] = {'-','o'}; //押された時、押されていない時 | |
char output; //送信用 | |
int btn = 0; //ボタンの状態保存用 | |
void setup() { | |
pinMode(BTNPIN, INPUT); | |
Wire.begin(8); //ID:8のSlaveで起動 | |
Wire.onRequest(requestEvent); | |
} | |
void loop() { | |
btn = digitalRead(5); //ボタンの状態を取得 | |
} | |
void requestEvent() { //ボタンの状態から返す値を決定 | |
if( btn == HIGH ) | |
output = list[1]; | |
else | |
output = list[0]; | |
Wire.write(output); //送信 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment