เรียนรู้การสร้างหุ่นยนต์อัตโนมัติ
ดาวน์โหลด Arduino IDE
ดาวน์โหลด Driver Pot Com
โค้ดโปรแกรม
1.หุ่นยนต์เดินตามเส้น
-----------------------------------------------------------------------------------
const int MotorL = 2;
const int MotorR = 3;
#define SensorL 4
#define SensorR 5
void setup() {
pinMode(SensorL, INPUT);
pinMode(SensorR, INPUT);
pinMode(MotorL, OUTPUT);
pinMode(MotorR, OUTPUT);
}
void loop() {
if(digitalRead(SensorL) == HIGH) {digitalWrite(MotorL, LOW);}
else {digitalWrite(MotorL, HIGH);}
if(digitalRead(SensorR) == HIGH) {digitalWrite(MotorR, LOW);}
else {digitalWrite(MotorR, HIGH);}
}
-----------------------------------------------------------------------------------------------------------
2.หุ่นยนต์แข่งขัน
int Button=13;
int IR1=2; //Right sensor
int IR2=3; //Left sensor
int Motor1=4; // Motor Right
int Motor2=5; // Motor Left
int lineCount = 0;
boolean lineDetected = false;
boolean started = false;
void setup()
{
Serial.begin(9600);
pinMode(Button,INPUT);
pinMode(IR1,INPUT);
pinMode(IR2,INPUT);
pinMode(Motor1,OUTPUT);
pinMode(Motor2,OUTPUT);
}
//***************************************************************************//
void loop()
{
// Serial.print("Button: ");
// Serial.println(digitalRead(Button));
if(digitalRead(Button)== HIGH )
{
delay(1000);
forword();
delay(500);
countLine(3);
followLine();
if(digitalRead(IR1)==HIGH && digitalRead(IR2)==HIGH)
{
forword();
delay(800);
stop();
}
exit(0);
}
}
//************************************************************************//
void testLineSensors() {
Serial.print("L: ");
Serial.print(IR1);
Serial.print("\tR:");
Serial.println(IR2);
delay(100);
}
//*************************************************************************//
void followLine()
{
if(digitalRead(IR1)==LOW && digitalRead(IR2)==LOW)
{
digitalWrite(Motor1,HIGH);
digitalWrite(Motor2,HIGH);
}
else if(digitalRead(IR1)==LOW && digitalRead(IR2)==HIGH)
{
digitalWrite(Motor1,HIGH);
digitalWrite(Motor2,LOW);
}
else if(digitalRead(IR1)==HIGH && digitalRead(IR2)==LOW)
{
digitalWrite(Motor1,LOW);
digitalWrite(Motor2,HIGH);
}
delay(25);
}
//****************************************************************************//
void countLine(int target) {
while (lineCount < target) {
followLine();
if (lineDetected == false) {
if (digitalRead(IR1)==HIGH && digitalRead(IR2)==HIGH) {
lineCount++;
Serial.println(lineCount);
lineDetected = true;
}
}
else if (lineDetected == true) {
if (digitalRead(IR1)==LOW && digitalRead(IR2)==LOW) {
lineDetected = false;
}
}
}
delay(250);
forword();
}
//******************************************************************//
void forword()
{
digitalWrite(Motor1,HIGH);
digitalWrite(Motor2,HIGH);
}
void turnRight()
{
digitalWrite(Motor1,HIGH);
digitalWrite(Motor2,LOW);
}
void turnLeft()
{
digitalWrite(Motor1,LOW);
digitalWrite(Motor2,HIGH);
}
void stop()
{
digitalWrite(Motor1,LOW);
digitalWrite(Motor2,LOW);
}
-----------------------------------------------------------------------------------------------------------
ไม่มีความคิดเห็น:
แสดงความคิดเห็น