私の入門記録であって、入門者向け解説サイトではありません。

サーボモーター

  • 投稿日:
  • Category:

サーボモーターはラジコン等やる方にはお馴染みのパーツだろうが、私は経験が無いので今回が初めての購入となった。回路は簡単で電源と信号線だけ、スケッチも標準サンプルに入っていると教科書には書いてあったが、探すのに少し手間取った(結局、メニューをスクロールして行った一番下の方で発見)

[ スケッチ:Sweep]

#include <Servo.h>
Servo myservo;  // create servo object
// twelve servo objects can be created on most boards
int pos = 0;    // variable to store the servo position
void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position 'pos'
    delay(15);                       // waits 15ms for the servo to reach pos
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to 'pos'
    delay(15);                       // waits 15ms for the servo to reach pos
  }
}

関数化

  • 投稿日:
  • Category:

こうなると、スケッチの信号点灯処理の繰り返し部分が気になって来る。ソフト経験のある方なら関数化したくなるのが人情と言うものだろう。
というわけで、signalSwitch() という関数を新たに設け、各信号の値をそこへ渡す形に改造したのが以下である。

[ スケッチ:IfStatementConditional31]

// These constants won't change:
const int analogPin = A0;    // pin that the sensor is attached to
const int ledPin = 13;       // pin that the LED is attached to
const int led2Pin = 12;      // pin that the LED is attached to
const int led3Pin = 11;      // pin that the LED is attached to
const int threshold = 150;   // an arbitrary threshold level that's in the range of the analog input
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}
void loop() {
  // read the value of the potentiometer:
  int analogValue = analogRead(analogPin);
  // if the analog value is high enough, turn on the LED:
  if (analogValue < threshold) {
    signalSwitch(1, 0, 0);  // RED
    delay(6000);
    signalSwitch(0, 0, 1);  // YEL
    delay(6000);
  } else {
    signalSwitch(0, 1, 0);  // GRN
  }
  // print the analog value:
  Serial.println(analogValue);
  delay(1);        // delay in between reads for stability
}
void signalSwitch(int x, int y, int z) {
  digitalWrite(ledPin,  x);
  digitalWrite(led2Pin, y);
  digitalWrite(led3Pin, z);
  return;
}

鉄道信号

  • 投稿日:
  • Category:

次の課題は鉄道信号だ。前項で光センサーは日没と共に照明が点灯するという想定で使用したが、今回は車両がセンサーを覆う事をトリガーに赤信号を点灯させる。なので、前項のLEDはそのまま赤信号と思えば良いわけだ。これに黄と緑の信号を加え、赤点灯で6秒後に黄に切り替え、さらに6秒後に緑に戻すというロジックで実験を行なった(教科書76ページ)。まずは結果の動画をご覧あれ。左下がセンサーだ。

自動点灯

  • 投稿日:
  • Category:
光センサーを発展させ、LEDの自動点灯を試みる(教科書63ページ)。回路は入力側を前回そのままに、出力側にLEDを繋ぐだけだ。 Img_6643.jpg

光センサー

  • 投稿日:
  • Category:

次は光センサー。具体的に言うと、CdSセルの実験だ(教科書56ページ)。これも実験基板上に実装されているのでそれを使う。…つもりだったのだが、結局ウンともスンとも言わなかった。念の為、基板から外して直結してもみたのだが反応がなく、どうもセル自体が不良品のようだ。こうなるともう、実験基板の信頼性は当てにならないな。

という事で、急遽ブレッドボードとCdsセルを追加購入。手元にあった抵抗と組み合わせれば、実験回路の完成だ。結線は教科書通りだが、今回は入力を判定する機能なので、写真でArduino左側に並ぶピンが入力サイドという事になるのか?

Img_6641.jpg