Arduino + サーボモータ。ボタンを押すと左右に回転!
ボタンを押すとサーボモーターがある方向へ回転、ある程度動くと反対側に動く。
というものを作ってみました。
サーボモータの動かし方を覚えたくて。
なんかすごく楽に行けちゃいましたね。
#include <Servo.h> #define BUTTON 7 Servo myservo; // create servo object to control a servo int val = 0; int direct = 1; int angle = 90; void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object pinMode(BUTTON, INPUT); } // scale it to use it with the servo (value between 0 and 180) void loop() { val = digitalRead(BUTTON); if (angle <= 0) { direct = 1; } else if (angle >= 180) { direct = -1; } if (val == HIGH) { angle = angle + direct; } myservo.write(angle); delay(10); }