夕べは忘年会でした。今日は、けだるさが残ったまま休日出勤です、引っ越した職場は誰も居ません。仕事をさくっと仕上げて早めに帰宅。
今日は、テストベンチ用のモーターコントローラーのプログラムを完成させます。サーボを1Chコントロールできるようにスケッチを追加します。サンプルプログラムをそのまま利用し、オシロで信号が出ていることを確認して今日は終了。 一応スケッチを載せておきます。
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potPin0 = 0; // analog pin used to connect the potentiometer
int val = 0 ; // variable to read the value from the analog pin
int sxPin1 = 9;
int mcPin1 = 5; // MC connected to digital pin 5
int mcPin2 = 6; // MC connected to digital pin 6
int potPin1 = 1; // VR1 pin1
int potPin2 = 2; // VR2 pin2
int val1 = 0 ;
int val2 = 0 ;
int sw1 = 0 ;
int sw2 = 0 ;
void setup() {
// nothing happens in setup
myservo.attach(sxPin1); // attaches the servo on pin 9 to the servo object
analogWrite(mcPin1, 0);
analogWrite(mcPin2, 0);
delay(9600);
}
void loop() {
val1 = analogRead(potPin1) / 4;
val2 = analogRead(potPin2) / 4;
if ( val1 < 10 ) sw1 = 1 ;
if ( sw1 == 1 ) analogWrite(mcPin1, val1);
if ( val2 < 10 ) sw2 = 1 ;
if ( sw2 == 1 ) analogWrite(mcPin2, val2);
val = analogRead(potPin0); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15);
}
0 件のコメント:
コメントを投稿