サーボコントローラーを作ってみた。
適当設計なのですが、毎回サーボテストするのにブレッドボードでなんだかんだするのが面倒になってきたのでサーボテスト用の基板を1ボードに纏めてみました。ArduinoにスイッチとLED載せただけなんですが、なにげに結構便利です。ESCのテストもこれと電源で出来ちゃいます。

■回路図

■スケッチ
#include <stdio.h>
#include <Servo.h>
//Key message
char msgs[5][16] = {"SW No.1 : 1000 ",
"SW No.2 : 1250 ",
"SW No.3 : 1500 ",
"SW No.4 : 1750 ",
"SW No.5 : 2000 " };
int adc_key_val[5] ={30, 150, 360, 535, 750 };
int led_pin_val[5] ={4, 5, 6, 7, 13 };
static uint8_t NUM_KEYS = 5;
int adc_key_in;
int key=-1;
static uint8_t oldkey=-1;
Servo myservo; // create servo object to control a servo
void setup() {
// Setup Serial
Serial.begin(19200);
for (int i=0;i<5;i++){
pinMode(led_pin_val[i], OUTPUT);
digitalWrite(led_pin_val[i], HIGH);
}
Serial.println("KEYPAD testing... pressing");
myservo.attach(8) ; // attaches the servo on pin 9 to the servo object
}
void loop() {
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) // if keypress is detected
{
delay(50); // wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey)
{
oldkey = key;
if (key >=0)
{
Serial.println(msgs[key]);
myservo.write(key*250+1000);
for (int i=0;i<5;i++){
digitalWrite(led_pin_val[i], HIGH);
}
digitalWrite(led_pin_val[key], LOW);
}
}
}
delay(200);
}
// Convert ADC value to key number
int get_key(unsigned int input){
int k;
for (k = 0; k < NUM_KEYS; k++){
if (input < adc_key_val[k]){
return k;
}
}
if (k >= NUM_KEYS) k = -1; // No valid key pressed
return k;
}
コメントはまだありません。