attention polarity Anode ,Cathode swap may not work
Led ต้องพิจารณาขั้ว แอโนด แคโทดด้วยนะครับ สลับจะไม่ติด
L298 motor control board
Left motor B Middle external power supply range for motor specification Right Motor A
ซ้าย คือ มอเตอร์ B ตรงคือ จ่ายไฟจากภายนอกให้ มอเตอร์ ขวาคือมอเตอร์ A
HC06 Bluetooth module
Wiring
Bluetooth to Arduino wiring
SoftwareSerial mySerial(10,11); // TX, RX
L298 motor control board connect to Arduino
ENA=>2;
IN1=>3;
IN2=>4;
IN3=>5;
IN4=>6;
ENB=>7;
#include<SoftwareSerial.h>
char receivedChar;
int ENA=2;
int IN1=3;
int IN2=4;
int IN3=5;
int IN4=6;
int ENB=7;
int timer = 50; // The higher the number, the slower the timing.
int ledPins[] = {
IN1,IN2,IN3,IN4
}; // an array of pin numbers to which LEDs are attached
int pinCount = 4; // the number of pins (i.e. the length of the array)
SoftwareSerial mySerial(10,11); // TX, RX
//RX on Bluetooth to TCX on Arduino through a voltage divider 50ohm from arduino and 100ohm to ground. This is so we can drop the the voltage down to 3.3v (roughly)
// Use blueterm on android to get messages
void setup()
{
pinMode(ENA,OUTPUT);//output
pinMode(ENB,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
mySerial.begin(9600);
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() // run over and over
{
while (!mySerial.available()); // stay here so long as COM port is empty
receivedChar = mySerial.read();
switch(receivedChar)
{
case 'A':
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);// High is start driving car
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(50);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(50);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(20);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
step1:
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(100);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(10);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(10);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(200);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(200);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
goto step1;
}
break;
default:
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);// High is start driving car
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น