42 | | nekoboard2に書き込まれているファームウェアのスケッチです。 |
43 | | {{{ |
44 | | // Scratch sensor board sketch for Arduino bootloader on Nekoboard2 |
45 | | // Switch-Science |
46 | | // 2016/04/25 |
47 | | |
48 | | int analogPin[4] = { A0,A1,A2,A3 }; // select the input pin for the general analog 0-4 |
49 | | int lightPin = A4; // select the input pin for the sensor |
50 | | int micPin = A5; // select the input pin for the mic |
51 | | int sliderPin = A6; // select the input pin for the slide volume |
52 | | int buttonPin = 2; // select the pin for the button |
53 | | |
54 | | int sliderValue = 0; // variable to store the value coming from the slide volume |
55 | | int lightValue = 0; // variable to store the value coming from the light sensor |
56 | | int micValue = 0; // variable to store the value coming from the mic |
57 | | int analogValue[4] = { 0,0,0,0}; // variable to store the value coming from the analog 0-4 |
58 | | const int firmware = 0x04; // Firmware virsion |
59 | | |
60 | | int senddata[18]={ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; |
61 | | |
62 | | void setsenddata(int value,int channel) { |
63 | | int pos; |
64 | | byte highdata = 0; |
65 | | byte lowdata = 0; |
66 | | if (channel==15) { |
67 | | pos = 0; |
68 | | } else { |
69 | | pos = channel*2+2; |
70 | | } |
71 | | highdata = 0x80 | (channel<<3) | lowByte(value >>7); |
72 | | lowdata = lowByte(0x7f & value); |
73 | | senddata[pos] = highdata; |
74 | | senddata[pos+1] = lowdata; |
75 | | } |
76 | | |
77 | | void setup() { |
78 | | // declare the ledPin as an OUTPUT: |
79 | | pinMode(buttonPin,INPUT); |
80 | | digitalWrite(buttonPin,HIGH); // プルアップのため |
81 | | Serial.begin(38400); |
82 | | } |
83 | | |
84 | | void loop() { |
85 | | int i; |
86 | | // if(1){ |
87 | | if (Serial.available() > 0) { // 受信したデータが存在したら |
88 | | int inByte = Serial.read(); // 受信データを読み込む |
89 | | if (inByte == 0x0001) { // 送られてきたデータが0x01だったら |
90 | | for (int i=0; i<18; i++) { |
91 | | Serial.write(senddata[i]); // 用意されているセンサーデータを送信する |
92 | | } |
93 | | } |
94 | | |
95 | | // read the value from the sensor: |
96 | | sliderValue = 1023 - analogRead(sliderPin); |
97 | | lightValue = analogRead(lightPin) - 15; |
98 | | if(lightValue < 0){ |
99 | | lightValue = 0; |
100 | | } |
101 | | micValue = analogRead(micPin); |
102 | | |
103 | | for (i=0; i<4; i++) { |
104 | | analogValue[i] = analogRead(analogPin[i]); |
105 | | } |
106 | | |
107 | | // Make sending data |
108 | | for (i=0; i<16; i++) { |
109 | | switch(i) { |
110 | | case 0: |
111 | | // Ch0 Registance-D |
112 | | setsenddata(analogValue[3],0); |
113 | | break; |
114 | | case 1: |
115 | | // Ch1 Registance-C |
116 | | setsenddata(analogValue[2],1); |
117 | | break; |
118 | | case 2: |
119 | | // Ch2 Registance-B |
120 | | setsenddata(analogValue[1],2); |
121 | | break; |
122 | | case 3: |
123 | | // Ch3 Button |
124 | | if (digitalRead(buttonPin) == 1) { |
125 | | setsenddata(1023,3); |
126 | | } |
127 | | else { |
128 | | setsenddata(0,3); |
129 | | } |
130 | | break; |
131 | | case 4: |
132 | | // Ch4 Registance-A |
133 | | setsenddata(analogValue[0],4); |
134 | | break; |
135 | | case 5: |
136 | | // Ch5 Light |
137 | | setsenddata(lightValue,5); |
138 | | break; |
139 | | case 6: |
140 | | // Ch6 Sound |
141 | | setsenddata(micValue,6); |
142 | | break; |
143 | | case 7: |
144 | | // Ch7 Slider |
145 | | setsenddata(sliderValue,7); |
146 | | break; |
147 | | case 15: |
148 | | // Ch15 Firmware ID |
149 | | setsenddata(firmware,15); |
150 | | break; |
151 | | default: |
152 | | // Ch8-14 No operation |
153 | | break; |
154 | | } |
155 | | } |
156 | | } |
157 | | } |
158 | | |
159 | | }}} |
160 | | |
161 | | |
162 | | |
163 | | |
164 | | |
165 | | |
166 | | |
| 42 | nekoboard2に書き込まれている[https://github.com/SWITCHSCIENCE/samplecodes/tree/master/nekoboard2/Arduino/nekoboard2 ファームウェアのスケッチ]です。 |