-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy patharduino-ibmcloud-part2.ino
More file actions
85 lines (77 loc) · 2.24 KB
/
arduino-ibmcloud-part2.ino
File metadata and controls
85 lines (77 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// ヘッダファイル指定 Including header files
#include <SakuraIO.h>
// LEDの定義 Definition of LED
#define LED_1 7
#define LED_2 6
#define LED_3 5
// 変数の定義 Definition of variables
SakuraIO_I2C sakuraio;
uint32_t cnt = 0;
// 起動時に1回だけ実行 Run once at startup
void setup() {
Serial.begin(9600);
Serial.print("Waiting to come online");
for (;;) {
if ( (sakuraio.getConnectionStatus() & 0x80) == 0x80 ) break;
Serial.print(".");
delay(1000);
}
Serial.println("");
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
}
// 以下ループ実行 Loop execution
void loop() {
// cnt値のカウントアップ Count up cnt value
cnt++;
Serial.println(cnt);
// データの即時送信 Immediate transmission of data
if (sakuraio.sendImmediately(0, cnt) != CMD_ERROR_NONE) {
Serial.println("[ERR] enqueue error");
}
digitalWrite(LED_3, HIGH);
delay(500);
digitalWrite(LED_3, LOW);
delay(500);
// 利用可能な領域(Available)とデータが格納された領域(Queued)の取得 Get Available and Queued
uint8_t available;
uint8_t queued;
if (sakuraio.getTxQueueLength(&available, &queued) != CMD_ERROR_NONE) {
Serial.println("[ERR] get tx queue length error");
}
Serial.print("Available :");
Serial.print(available);
Serial.print(" Queued :");
Serial.println(queued);
// sakura.ioからの受信データに応じたLED点灯(Digital7,6) Lights the LED according to received data
available = 0;
if (sakuraio.getRxQueueLength(&available, &queued) != CMD_ERROR_NONE) {
Serial.println("[ERR] get rx queue length error");
}
if (available > 0)
{
for (int i = 0; i < available; i++)
{
uint8_t ch, type, value[8];
int64_t offset;
sakuraio.dequeueRx(&ch, &type, value, &offset);
if (ch == 0) {
if (value[0] == 1) {
digitalWrite(LED_1, HIGH);
} else {
digitalWrite(LED_1, LOW);
}
} else if (ch == 1) {
if (value[0] == 1) {
digitalWrite(LED_2, HIGH);
} else {
digitalWrite(LED_2, LOW);
}
}
}
sakuraio.clearRx();
}
// 待機時間の調整 Adjust wait time
delay(5000);
}