// MOTOR & STEPPER MOTOR Series :: http://fitrox.lnwshop.com
// EP.7 A4988 Motor Driver w/o Library Example
int dirPin = 2; // pin 2 กำหนดเป็น Direction pin
int stepPin = 3; // pin 3 กำหนดเป็น Step pin
int stepsPerRev = 500; // กำหนด step/รอบ เป็น 200 (Full Step Mode)
int dirPin2 = 4; // pin 2 กำหนดเป็น Direction pin
int stepPin2 = 5;
int reset_1 = 6; // pin 2 กำหนดเป็น Direction pin
int reset_2 = 7; // pin 3 กำหนดเป็น Step pin
int stepsPerRev2 = 200;
int switch_1 = 8; //สวิตช์เดินหน้า
int switch_2 = 9; //สวิตช์เลี้ยวซ้าย/่ถอยหลังยาว
int sw_1;
int sw_2;
//int lastState = HIGH; // the previous state from the input pin
//int sw_1State; // the current reading from the input pin // กำหนด step/รอบ เป็น 200 (Full Step Mode
void setup() {
// StepPin และ dirPin เป็น OUTPUT
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(switch_1, INPUT_PULLUP);
pinMode(switch_2, INPUT_PULLUP);
digitalWrite(reset_1,OUTPUT);
digitalWrite(reset_2,OUTPUT);
}
void loop() {
// ให้มอเตอร์หมุนทิศตามเข็มนาฬิกาโดยให้ Direction เป็น HIGH
//digitalWrite(dirPin, HIGH);
// สร้าง Pulse ให้มอเตอร์ ซึ่ง 1 ลูกมอเตอร์จะหมุน 1 Step ต้องการ 1 รอบ ต้องสร้าง Pulse 200 ลูก
sw_1=digitalRead(switch_1);//สวิตช์กดเดินหน้า
if(sw_1==0){
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
}
}
// Pulse 1 ลูก กว้าง 4000 us
/*delay(2000); // หน่วงเวลา 2 วินาที
// ให้มอเตอร์หมุนอีกทิศ คือทวนนาฬิกาโดยให้ Direction เป็น LOW
digitalWrite(dirPin, LOW);
// สร้าง Pulse เหมือนเดิม แต่จะให้มอเตอร์หมุนเร็วขึ้นด้วยการให้ Pulse 1 ลูกมีความกว้างน้อยลงกว่าเดิม
for(int i = 0; i < stepsPerRev; i++){
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
// Pulse 1 ลูก กว้าง 1000 us
}
//จบตัวที่ 1
delay(2000); // หน่วงเวลา 2 วินาที
digitalWrite(dirPin2, HIGH);
// สร้าง Pulse ให้มอเตอร์ ซึ่ง 1 ลูกมอเตอร์จะหมุน 1 Step ต้องการ 1 รอบ ต้องสร้าง Pulse 200 ลูก
for(int i = 0; i < stepsPerRev; i++){
digitalWrite(stepPin2, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin2, LOW);
delayMicroseconds(2000);
// Pulse 1 ลูก กว้าง 4000 us
}
delay(2000); // หน่วงเวลา 2 วินาที
// ให้มอเตอร์หมุนอีกทิศ คือทวนนาฬิกาโดยให้ Direction เป็น LOW
digitalWrite(dirPin2, LOW);
// สร้าง Pulse เหมือนเดิม แต่จะให้มอเตอร์หมุนเร็วขึ้นด้วยการให้ Pulse 1 ลูกมีความกว้างน้อยลงกว่าเดิม
for(int i = 0; i < stepsPerRev; i++){
digitalWrite(stepPin2, LOW);
delayMicroseconds(1000);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(1000);
// Pulse 1 ลูก กว้าง 1000 us
}
delay(2000); // หน่วงเวลา 2 */