私はArduinoとプログラミングにもかなり新しいです。
私はディップスイッチを使ってサーボをスイープしたい。現在のところ、4ポジションディップスイッチを使用しています。各スイッチには、サーボを実行する回数を設定します。例えば、DIPスイッチのPos1には、5回スイープが表示されます。ポジ2は言う、10回掃引など
私は次のコードを書いた:
#include
Servo myservo;
int pos = 0;
int count = 0; //might need later
int runXTimes = 0;
#define S1 2
#define S2 3
#define S3 4
#define S4 5
int s1state = HIGH;
int s2state = HIGH;
int s3state = HIGH;
int s4state = HIGH;
void setup() {
myservo.attach(9);
pinMode(S1, INPUT_PULLUP);
pinMode(S2, INPUT_PULLUP);
pinMode(S3, INPUT_PULLUP);
pinMode(S4, INPUT_PULLUP);
pinMode(9, OUTPUT);
}
void loop() {
s1state = digitalRead(S1);
s2state = digitalRead(S2);
s3state = digitalRead(S3);
s4state = digitalRead(S4);
//Servo control through Switch 1
if (s1state == LOW) { //read the input pin
for (int runXTimes = 0; runXTimes < 5; runXTimes++) { //Servo to sweep for 5 times
for (pos = 0; pos <= 80; pos += 1) //goes from 0 degrees to 80 degrees in steps of degree
{
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(5);
}
for(pos = 80; pos>=0; pos-=1) //goes from 80 degrees to 0 degrees
{
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(5);
}
}
}
//Servo control through Switch 2. Used servo detach here
else if (s2state == LOW) { //read the input pin
for (int runXTimes = 0; runXTimes < 10; runXTimes++) { //Servo to sweep for 10 times
for (pos = 0; pos <= 80; pos += 1) //goes from 0 degrees to 80 degrees in steps of degree
{
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(5);
}
for(pos = 80; pos>=0; pos-=1) //goes from 80 degrees to 0 degrees
{
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(5);
}
}
myservo.detach();
}
//Servo control through Switch 3
else if (s3state == LOW) { //read the input pin
for (pos = 0; pos <= 80; pos += 1) //goes from 0 degrees to 80 degrees in steps of degree
{ //
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(5);
}
for(pos = 80; pos>=0; pos-=1) //goes from 80 degrees to 0 degrees
{
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(5);
}
}
//Servo control through Switch 4
else if (s4state == LOW) { //read the input pin
for (pos = 0; pos <= 80; pos += 1) //goes from 0 degrees to 80 degrees in steps of degree
{
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(5);
}
for(pos = 80; pos>=0; pos-=1) //goes from 80 degrees to 0 degrees
{
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(5);
}
}
}
上記のコードで発生するアクションは次のとおりです。
1)スイッチ1によるサーボ制御 - 直ちにON(0)、OFF(1)を行うと、サーボは5回掃引します。ボタンスイッチのように機能します。私がもう一度オンとオフを切り替えると、私は5回掃引することができます。しかし、スイッチを1のままにしておくと、5回後にスイープが停止することはありません。
2)スイッチ2を介したサーボ制御:スイッチをONにすると、サーボは10回掃引し、私の興味のために停止する。しかし、スイッチを再び0と1に変更すると、シーケンスは開始しません。実際、他のスイッチは動作しません。私は再びプログラムを実行するためにArduinoの電源を切る必要があります。
3)スイッチ3(=スイッチ4)によるサーボ制御:スイッチが1の場合、サーボが掃引します。私のスイッチが0の場合、サーボは停止します。
あなたはお勧めしますか:
1)スイッチが常に1になっていても、または0と1の間を切り替えるときでも、設定した回数だけ自分のサーボが掃引するようにコードを書き込む方法はありますか?
2)私はシリアル通信(通常はPC)を使いたくない。 7セグメントまたはLCDをインターフェースして、完了した、またはまだ残っているスイープの数を表示できますか?
前もって感謝します。