私は障害物コース(モデルサイズ)を作ろうとしている別の学校プロジェクトに取り組んでいます。このプロジェクトでは、2つの異なるボタンで制御したい2つのサーボモータを使用しています。 1つのサーボモータに1つのボタンが接続され、もう1つのサーボに1つのボタンが接続されています。私は実際に両方のボタンをサーボ・モーターで動作させるのに苦労しています。2つのボタンを備えた2つのサーボモータを制御Arduino
私は1つのボタンと1つのサーボモータを取り付けると、すべて私が欲しいのと同じように動作します。ボタンを押すとサーボモータが90度回転し、5秒後に戻ります。
コード:
#include <Servo.h>
Servo myservo;
const int servoPin = D8; // Servo pin
const int buttonPin = D7; // Pushbutton pin
void setup() {
myservo.attach(servoPin);
pinMode(buttonPin, INPUT);
}//setup
void loop() {
if (digitalRead(buttonPin) == HIGH) {
myservo.write(180);
delay(50); // waits 50ms to reach the position
delay(15000);//15 seconden wachten
myservo.write(0);
delay(50); // waits 50ms to reach the position
}
}//loop
は、しかし、私はあなたがもっとして1つのサーボモータを使用する場合、あなたは異なったコードを記述する必要があることフォーラムに読みます。
#include <Servo.h>
Servo myservoa, myservob;
コードを変更すると、すべてが機能しなくなり、ここで間違っていることが分かりません。私はサーボモータが2つの異なるボタンで同じ時間に動作するようにしたい。
新しいコード:
#include <Servo.h>
Servo myservoa, myservob;
const int servoPin1 = D8; // Servo pin
const int servoPin2 = D6; // Servo pin
const int buttonPin1 = D7; // Pushbutton pin
const int buttonPin2 = D5; // Pushbutton pin
void setup() {
myservoa.attach(servoPin1);
myservob.attach(servoPin2);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}//setup
void loop() {
if (digitalRead(buttonPin1) == HIGH) {
myservoa.write(90);
delay(50); // waits 50ms to reach the position
delay(5000);// 5 seconden wachten
myservoa.write(0);
delay(50); // waits 50ms to reach the position
}
if (digitalRead(buttonPin2) == HIGH) {
myservob.write(90);
delay(50); // waits 50ms to reach the position
delay(5000);// 5 seconden wachten
myservob.write(0);
delay(50); // waits 50ms to reach the position
}
}//loop
私は誰かが私を助けることを願って!
EDIT:
だから私は2つのサーボモータは、実際に私のNodeMCUのための多くにあったことが分かりました。コメントのコードはうまく働いた!今私は小型モーターとサーボモーターを組み合わせようとしています。 2つのセンサーはうまく機能しますが、振動モーターを正しく作動させることができません。
ボタンを押した後、振動モーターを5秒間振動させたいです。 5秒後、自動的に停止する必要があります。コードでは、ボタンを押すと振動モーターのみが振動します。ボタンを押さないと、振動モーターは直接停止します。
コード:
#include <Servo.h>
Servo myservo;
const int servoPin = D8; // Servo pin
const int vibratiePin = D3; // Servo pin
const int buttonPin1 = D6; // Pushbutton pin
const int buttonPin2 = D5; // Pushbutton Pin
unsigned long stopA = 0;
unsigned long stopB = 0;
bool controlA = false;
bool controlB = false;
void setup() {
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
myservo.attach(servoPin);
pinMode(vibratiePin, OUTPUT);
}//setup
void loop() {
unsigned long now = millis();
if(controlA && stopA < now) {
myservo.write(0);
controlA = false;
} else if (!controlA && digitalRead(buttonPin1) == HIGH) {
controlA = true;
myservo.write(90);
stopA = millis() + 5000;
}
if(controlB && stopB < now) {
digitalWrite(vibratiePin, LOW);
controlB = false;
stopB = millis() + 5000;
} else if (!controlB && digitalRead(buttonPin2) == HIGH) {
controlB = true;
digitalWrite(vibratiePin, HIGH);
}
stopB = now;
}
私は私が間違ってやっているのか理解していないので、誰かがここで問題を見ることができると思います。
ありがとうございます!今試してみるよ! –
コードは動作しますが、私は今すぐ別の問題に走っています。私はあなたが私を助けることができるかもしれない私の質問を編集! –
その場合、別の問題がある場合は別の質問で質問する必要があります – Doleron