Untitled

 avatar
unknown
plain_text
a year ago
3.7 kB
4
Indexable
#include <Arduino.h>

#define m1 28 // Right Motor MA1
#define m2 30 // Right Motor MA2
#define m3 32 // Left Motor MB1
#define m4 34 // Left Motor MB2
#define e1 2  // Right Motor Enable Pin EA
#define e2 3  // Left Motor Enable Pin EB

//**********5 Channel IR Sensor Connection**********//
#define ir1 A0
#define ir2 A1
#define ir3 A2
#define ir4 A3
#define ir5 A4
//*************************************************//
char c;
int tocdo = 255;
void setup()
{
    Serial.begin(9600);
    pinMode(m1, OUTPUT);
    pinMode(m2, OUTPUT);
    pinMode(m3, OUTPUT);
    pinMode(m4, OUTPUT);
    pinMode(e1, OUTPUT);
    pinMode(e2, OUTPUT);
    pinMode(ir1, INPUT);
    pinMode(ir2, INPUT);
    pinMode(ir3, INPUT);
    pinMode(ir4, INPUT);
    pinMode(ir5, INPUT);
}

void loop()
{
    int IR1 = digitalRead(ir1); // Left Most Sensor
    int IR2 = digitalRead(ir2); // Left Sensor
    int IR3 = digitalRead(ir3); // Middle Sensor
    int IR4 = digitalRead(ir4); // Right Sensor
    int IR5 = digitalRead(ir5); // Right Most Sensor
    if (IR1 == 1 && IR2 == 1 && IR3 == 0 && IR4 == 1 && IR5 == 1)
        thang();
    if (IR1 == 1 && IR2 == 0 && IR3 == 0 && IR4 == 0 && IR5 == 1)
        thang();
    if (IR1 == 1 && IR2 == 0 && IR3 == 0 && IR4 == 1 && IR5 == 1)
        thang();
    if (IR1 == 1 && IR2 == 1 && IR3 == 0 && IR4 == 0 && IR5 == 1)
        thang();

    if (IR1 == 1 && IR2 == 1 && IR3 == 1 && IR4 == 1 && IR5 == 0)
        phai();
    if (IR1 == 1 && IR2 == 1 && IR3 == 1 && IR4 == 0 && IR5 == 0)
        phai();
    if (IR1 == 1 && IR2 == 1 && IR3 == 0 && IR4 == 0 && IR5 == 0)
        phai();
    if (IR1 == 1 && IR2 == 0 && IR3 == 0 && IR4 == 0 && IR5 == 0)
        phai();

    if (IR1 == 0 && IR2 == 1 && IR3 == 1 && IR4 == 1 && IR5 == 1)
        trai();
    if (IR1 == 0 && IR2 == 0 && IR3 == 1 && IR4 == 1 && IR5 == 1)
        trai();
    if (IR1 == 0 && IR2 == 0 && IR3 == 0 && IR4 == 1 && IR5 == 1)
        trai();
    if (IR1 == 0 && IR2 == 0 && IR3 == 0 && IR4 == 0 && IR5 == 1)
        trai();

    if (IR1 == 1 && IR2 == 1 && IR3 == 1 && IR4 == 1 && IR5 == 1)
        dung();
    //-------------------------------------------
}

void thang()
{
    Serial.println("Xe chay thang ");
    analogWrite(e1, tocdo); // you can adjust the speed of the motors from 0-255
    analogWrite(e2, tocdo); // you can adjust the speed of the motors from 0-255
    digitalWrite(m1, HIGH);
    digitalWrite(m2, LOW);
    digitalWrite(m3, HIGH);
    digitalWrite(m4, LOW);
    delay(300);
    dung();
}
void lui()
{
    Serial.println("Xe chay lui ");
    analogWrite(e1, tocdo); // you can adjust the speed of the motors from 0-255
    analogWrite(e2, tocdo);
    digitalWrite(m1, LOW);
    digitalWrite(m2, HIGH);
    digitalWrite(m3, LOW);
    digitalWrite(m4, HIGH);
    delay(300);
    dung();
}
void phai()
{
    Serial.println("Xe re phai ");
    analogWrite(e1, tocdo); // you can adjust the speed of the motors from 0-255
    analogWrite(e2, tocdo);
    digitalWrite(m1, LOW);
    digitalWrite(m2, HIGH);
    digitalWrite(m3, HIGH);
    digitalWrite(m4, LOW);
    delay(300);
    dung();
}
void trai()
{
    Serial.println("Xe re trai ");
    analogWrite(e1, tocdo); // you can adjust the speed of the motors from 0-255
    analogWrite(e2, tocdo);
    digitalWrite(m1, HIGH);
    digitalWrite(m2, LOW);
    digitalWrite(m3, LOW);
    digitalWrite(m4, HIGH);
    delay(300);
    dung();
}
void dung()
{
    Serial.println("Xe dung");
    analogWrite(e1, 0); // you can adjust the speed of the motors from 0-255
    analogWrite(e2, 0); // you can adjust the speed of the motors from 0-255
}
Editor is loading...
Leave a Comment