งานที่ 19 หุ่นยนต์เดินตาม เปลวไฟ Fire Extinguisher
HARDWARE
SOFTWARE
// Motor A pins (enableA = enable motor, pinA1 = forward, pinA2 = backward)
int enableA = 3;
int pinA1 = 6;
int pinA2 = 7;
//Motor B pins (enabledB = enable motor, pinB2 = forward, pinB2 = backward)
int enableB = 5;
int pinB1 = 8;
int pinB2 = 9;
int sensor[5] = {0, 0, 0, 0, 0};
void setup()
{
pinMode(enableA, OUTPUT);
pinMode(pinA1, OUTPUT);
pinMode(pinA2, OUTPUT);
pinMode(enableB, OUTPUT);
pinMode(pinB1, OUTPUT);
pinMode(pinB2, OUTPUT);
enableMotors();
analogWrite(enableA, 120); // ปรับค่าความเร็วของหุ่นยนต์
analogWrite(enableB, 120); // ปรับค่าความเร็วของหุ่นยนต์
delay(2000);
}
void loop()
{
sensor[0] = analogRead(A1);
sensor[1] = analogRead(A2);
sensor[2] = analogRead(A3);
sensor[3] = analogRead(A4);
sensor[4] = analogRead(A5);
if ((sensor[0] > sensor[1]) && (sensor[0] > sensor[2]) && (sensor[0] > sensor[3]) && (sensor[0] > sensor[4]))
turnLeft(200);
else if ((sensor[1] > sensor[0]) && (sensor[1] > sensor[2]) && (sensor[1] > sensor[3]) && (sensor[1] > sensor[4]))
turnLeft(100);
else if ((sensor[2] > sensor[0]) && (sensor[2] > sensor[1]) && (sensor[2] > sensor[3]) && (sensor[2] > sensor[4]))
forward(1);
else if ((sensor[3] > sensor[0]) && (sensor[3] > sensor[1]) && (sensor[3] > sensor[2]) && (sensor[3] > sensor[4]))
turnRight(100);
else if ((sensor[4] > sensor[0]) && (sensor[4] > sensor[1]) && (sensor[4] > sensor[2]) && (sensor[4] > sensor[3]))
turnRight(200);
else
coast(100);
}
//Define high-level H-bridge commands
void enableMotors()
{
motorAOn();
motorBOn();
}
void disableMotors()
{
motorAOff();
motorBOff();
}
void forward(int time)
{
motorAForward();
motorBForward();
delay(time);
}
void backward(int time)
{
motorABackward();
motorBBackward();
delay(time);
}
void turnLeft(int time)
{
motorABackward();
motorBForward();
delay(time);
}
void turnRight(int time)
{
motorAForward();
motorBBackward();
delay(time);
}
void coast(int time)
{
motorACoast();
motorBCoast();
delay(time);
}
void brake(int time)
{
motorABrake();
motorBBrake();
delay(time);
}
//Define low-level H-bridge commands
//enable motors
void motorAOn()
{
digitalWrite(enableA, HIGH);
}
void motorBOn()
{
digitalWrite(enableB, HIGH);
}
//disable motors
void motorAOff()
{
digitalWrite(enableB, LOW);
}
void motorBOff()
{
digitalWrite(enableA, LOW);
}
//motor A controls
void motorAForward()
{
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, LOW);
}
void motorABackward()
{
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, HIGH);
}
//motor B controls
void motorBForward()
{
digitalWrite(pinB1, HIGH);
digitalWrite(pinB2, LOW);
}
void motorBBackward()
{
digitalWrite(pinB1, LOW);
digitalWrite(pinB2, HIGH);
}
//coasting and braking
void motorACoast()
{
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, LOW);
}
void motorABrake()
{
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, HIGH);
}
void motorBCoast()
{
digitalWrite(pinB1, LOW);
digitalWrite(pinB2, LOW);
}
void motorBBrake()
{
digitalWrite(pinB1, HIGH);
digitalWrite(pinB2, HIGH);
}
ความคิดเห็น
แสดงความคิดเห็น