0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GR-Citrusで近接センサーとServoをつなげたよ

Posted at

GR-Citrusシリーズですが合成してみましょう

近接センサーで近づいてきたらサーボモーターが動くようにしましょう。

GR-Citrus-WIre-HC-SR-servo.jpg

/* GR-CITRUS Sketch Template V2.20 */
# include <Arduino.h>
# include <Servo.h> //Servoモーターを動かすよ
# define trigPin 8
# define echoPin 9
# define INTERVAL 500
unsigned char g_pos = 0;
Servo servo0;
Servo servo1;


void setup(){
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(PIN_LED0, OUTPUT);
 servo0.attach(7);
 servo1.attach(6);
}

void loop(){
 int duration, distance;
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(1000);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 if (distance <= 50 || distance == 0){  //ここで距離を設定できるよ
   Serial.println("Near");
  servo0.write(g_pos);
  servo1.write(g_pos);
    g_pos+=10;}
    if(g_pos > 180){
            g_pos = 0;
    }
 if (distance >= 200 || distance <= 0){
   Serial.println("Out of range");
 }
 else {
   Serial.print(distance);
   Serial.println(" cm");
 }
 delay(500);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?