日本と海外

日本と海外の国の比較と名古屋愛知情報

arduino とサーボモーター SG90

arduinoサーボモーターを動かしてみました。
arduinoの電源だけでは電力が足りず、追加の電源がないと動きません。私は動かない為プログラムを数回手直ししました。
電源を追加したらあっさりと動きます。モーター類は追加電源が必須です。高電圧を使うと時にはリレーなどが要ります。
Arduinoに高い電圧を入れるとarduino昇天の可能性があります。
IDEの例を変数化しました。
//---------------------
間の変数は初期値を当て、const intで指定した変数はプログラムに関連するなど、初期値を変えると正常に動かなくなる可能性があります。
set up
volid loop
の部分は一切変えなくていいようにしました。
これは可変抵抗の変化をサーボモーターで再現するだけなので、これを応用したプログラムの発展を望みます。
 
/*
 Controlling a servo position using a potentiometer (variable resistor)

 modified on 8 Nov 2013
 by Scott Fitzgerald
*/
 
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
//    ----------------------------------------------------------
const int potpin = A0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
const int sPin=9;
const int hayasa=9800;
int saitei=0;
int saikou=180;
int yomu0=0;
int yomu9=1023;
int mati=5;
//  ---------------------------------------------------------------
void setup() {//   setup**********************************
  myservo.attach(sPin);  // attaches the servo on pin 9 to the servo object
  Serial.begin(hayasa);

}

void loop() {//  loop  ******************************************

  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, yomu0, yomu9,saitei, saikou);     // scale it for use with the servo (value between 0 and 180)

  Serial.println(val);

myservo.write(val);                  // sets the servo position according to the scaled value
  delay(mati*3);                           // waits for the servo to get there
}