「Arduino Mega 2560 LESSON:29 直流モータ」の版間の差分
編集の要約なし |
編集の要約なし |
||
| 165行目: | 165行目: | ||
} | } | ||
</code></pre></div><script></freescript> | </code></pre></div><script></freescript> | ||
<freescript></script><div class="hljs-wrap"><pre data-label="Arduino Code" class="pre-wrap"> | |||
<code class="cpp">//www.elegoo.com | |||
//2017.12.12 | |||
/************************ | |||
Exercise the motor using | |||
the L293D chip | |||
************************/ | |||
#define ENABLE 5 | |||
#define DIRA 3 | |||
#define DIRB 4 | |||
int i; | |||
void setup() { | |||
//---set pin direction | |||
pinMode(ENABLE, OUTPUT); | |||
pinMode(DIRA, OUTPUT); | |||
pinMode(DIRB, OUTPUT); | |||
Serial.begin(9600); | |||
} | |||
void loop() { | |||
//---back and forth example | |||
Serial.println("One way, then reverse"); | |||
digitalWrite(ENABLE, HIGH); // enable on | |||
for (i = 0; i < 5; i++) { | |||
digitalWrite(DIRA, HIGH); //one way | |||
digitalWrite(DIRB, LOW); | |||
delay(500); | |||
digitalWrite(DIRA, LOW); //reverse | |||
digitalWrite(DIRB, HIGH); | |||
delay(500); | |||
} | |||
digitalWrite(ENABLE, LOW); // disable | |||
delay(2000); | |||
Serial.println("fast Slow example"); | |||
//---fast/slow stop example | |||
digitalWrite(ENABLE, HIGH); //enable on | |||
digitalWrite(DIRA, HIGH); //one way | |||
digitalWrite(DIRB, LOW); | |||
delay(3000); | |||
digitalWrite(ENABLE, LOW); //slow stop | |||
delay(1000); | |||
digitalWrite(ENABLE, HIGH); | |||
digitalWrite(DIRA, LOW); | |||
digitalWrite(DIRB, HIGH); | |||
delay(3000); | |||
// 電気ブレーキ | |||
digitalWrite(DIRB, LOW); | |||
delay(2000); | |||
Serial.println("PWM full then slow"); | |||
//---PWM example, full speed then slow | |||
analogWrite(ENABLE, 255); //enable on | |||
digitalWrite(DIRA, HIGH); //one way | |||
digitalWrite(DIRB, LOW); | |||
delay(2000); | |||
analogWrite(ENABLE, 180); //half speed | |||
delay(2000); | |||
analogWrite(ENABLE, 128); //half speed | |||
delay(2000); | |||
analogWrite(ENABLE, 50); //half speed | |||
delay(2000); | |||
analogWrite(ENABLE, 128); //half speed | |||
delay(2000); | |||
analogWrite(ENABLE, 180); //half speed | |||
delay(2000); | |||
analogWrite(ENABLE, 255); //half speed | |||
delay(2000); | |||
digitalWrite(ENABLE, LOW); //all done | |||
delay(1000); | |||
} | |||
</code></pre></div><script></freescript> | |||
[[Arduino Mega 2560]]に戻る。 | [[Arduino Mega 2560]]に戻る。 | ||
2026年11月7日 (土) 23:12時点における最新版
概要
電源供給モジュールとモーターとL293Dを接続するレッスンです。
完成図は以下のとおりです。
[[|380px||none|完成図]]
PCB図は以下のようなものです。
[[|380px||none|PCB図]]
3Dプレビュー図は以下のようなものです。
[[|380px||none|3Dプレビュー図]]
コードは以下のようなものです。
//www.elegoo.com
//2017.12.12
/************************
Exercise the motor using
the L293D chip
************************/
#define ENABLE 5
#define DIRA 3
#define DIRB 4
int i;
void setup() {
//---set pin direction
pinMode(ENABLE,OUTPUT);
pinMode(DIRA,OUTPUT);
pinMode(DIRB,OUTPUT);
Serial.begin(9600);
}
void loop() {
//---back and forth example
Serial.println("One way, then reverse");
digitalWrite(ENABLE,HIGH); // enable on
for (i=0;i<5;i++) {
digitalWrite(DIRA,HIGH); //one way
digitalWrite(DIRB,LOW);
delay(500);
digitalWrite(DIRA,LOW); //reverse
digitalWrite(DIRB,HIGH);
delay(500);
}
digitalWrite(ENABLE,LOW); // disable
delay(2000);
Serial.println("fast Slow example");
//---fast/slow stop example
digitalWrite(ENABLE,HIGH); //enable on
digitalWrite(DIRA,HIGH); //one way
digitalWrite(DIRB,LOW);
delay(3000);
digitalWrite(ENABLE,LOW); //slow stop
delay(1000);
digitalWrite(ENABLE,HIGH); //enable on
digitalWrite(DIRA,LOW); //one way
digitalWrite(DIRB,HIGH);
delay(3000);
digitalWrite(DIRA,LOW); //fast stop
delay(2000);
Serial.println("PWM full then slow");
//---PWM example, full speed then slow
analogWrite(ENABLE,255); //enable on
digitalWrite(DIRA,HIGH); //one way
digitalWrite(DIRB,LOW);
delay(2000);
analogWrite(ENABLE,180); //half speed
delay(2000);
analogWrite(ENABLE,128); //half speed
delay(2000);
analogWrite(ENABLE,50); //half speed
delay(2000);
analogWrite(ENABLE,128); //half speed
delay(2000);
analogWrite(ENABLE,180); //half speed
delay(2000);
analogWrite(ENABLE,255); //half speed
delay(2000);
digitalWrite(ENABLE,LOW); //all done
delay(10000);
}
//www.elegoo.com
//2017.12.12
/************************
Exercise the motor using
the L293D chip
************************/
#define ENABLE 5
#define DIRA 3
#define DIRB 4
int i;
void setup() {
//---set pin direction
pinMode(ENABLE, OUTPUT);
pinMode(DIRA, OUTPUT);
pinMode(DIRB, OUTPUT);
Serial.begin(9600);
}
void loop() {
//---back and forth example
Serial.println("One way, then reverse");
digitalWrite(ENABLE, HIGH); // enable on
for (i = 0; i < 5; i++) {
digitalWrite(DIRA, HIGH); //one way
digitalWrite(DIRB, LOW);
delay(500);
digitalWrite(DIRA, LOW); //reverse
digitalWrite(DIRB, HIGH);
delay(500);
}
digitalWrite(ENABLE, LOW); // disable
delay(2000);
Serial.println("fast Slow example");
//---fast/slow stop example
digitalWrite(ENABLE, HIGH); //enable on
digitalWrite(DIRA, HIGH); //one way
digitalWrite(DIRB, LOW);
delay(3000);
digitalWrite(ENABLE, LOW); //slow stop
delay(1000);
digitalWrite(ENABLE, HIGH);
digitalWrite(DIRA, LOW);
digitalWrite(DIRB, HIGH);
delay(3000);
// 電気ブレーキ
digitalWrite(DIRB, LOW);
delay(2000);
Serial.println("PWM full then slow");
//---PWM example, full speed then slow
analogWrite(ENABLE, 255); //enable on
digitalWrite(DIRA, HIGH); //one way
digitalWrite(DIRB, LOW);
delay(2000);
analogWrite(ENABLE, 180); //half speed
delay(2000);
analogWrite(ENABLE, 128); //half speed
delay(2000);
analogWrite(ENABLE, 50); //half speed
delay(2000);
analogWrite(ENABLE, 128); //half speed
delay(2000);
analogWrite(ENABLE, 180); //half speed
delay(2000);
analogWrite(ENABLE, 255); //half speed
delay(2000);
digitalWrite(ENABLE, LOW); //all done
delay(1000);
}