TI-Nspire & Arduino、サーボを使う 3、Keyboard ライブラリーを使って TI-Nspire で角度をモニタリングする

f:id:ti-nspire:20160806070632p:plain:w475
Leonardo sends angle data to TI-Nspire Student Software through a USB cable by using the Keyboard library. TI-Nspire takes these data into its Lists & Spreadsheet application, and shows them graphically.
 
.ino

#include <Servo.h>
#include <Keyboard.h>
#define sigPin 9
#define swPin 12

Servo myservo;
int pos = 0;

void setup() {
  pinMode(swPin, INPUT_PULLUP);
  myservo.attach(sigPin);
  Keyboard.begin();
}

void loop() {
  for (pos = 0; pos < 180; pos += 1) {
    if (digitalRead(swPin) == LOW) {
      myservo.write(pos);
      Keyboard.print(pos);
      Keyboard.press(KEY_RETURN);
      Keyboard.releaseAll();
      delay(15);
    }
  }
  for (pos = 180; pos > 0; pos -= 1) {
    if (digitalRead(swPin) == LOW) {
      myservo.write(pos);
      Keyboard.print(pos);
      Keyboard.press(KEY_RETURN);
      Keyboard.releaseAll();
      delay(15);
    }
  }
}


.tns
sweep.tns - Google ドライブ