0% found this document useful (0 votes)
3 views

arduino code

The document contains an Arduino code for controlling two motors and reading inputs from an mz80 sensor and two IR sensors. It sets up the necessary pin configurations and implements a loop to control motor directions and speeds based on sensor readings. The motors are activated or reversed depending on the detected sensor values, with specific speed settings applied.

Uploaded by

1aslyli3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

arduino code

The document contains an Arduino code for controlling two motors and reading inputs from an mz80 sensor and two IR sensors. It sets up the necessary pin configurations and implements a loop to control motor directions and speeds based on sensor readings. The motors are activated or reversed depending on the detected sensor values, with specific speed settings applied.

Uploaded by

1aslyli3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// connect motor controller pins to Arduino digital pins

// motor one
int enA = 5;
int in1 = 6;
int in2 = 7;
// motor two
int enB = 10;
int in3 = 8;
int in4 = 9;
// Connect mz80 pins to arduino
int mz80 = 2;
//Connect IR sensor to arduino
int ir1 = 11;
int ir2 = 12;

void setup() {
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(mz80, INPUT);
pinMode(ir1, INPUT);
pinMode(ir2, INPUT);

delay(5000);
}

void loop() {
// put your main code here, to run repeatedly:
int sensorValuemz = digitalRead(mz80); // Read the sensor value (HIGH or LOW)
int sensorValueir1 = digitalRead(ir1);
int sensorValueir2 = digitalRead(ir2);
if (sensorValueir1 == HIGH || sensorValueir2 == HIGH) {
// Assuming sensor gives LOW when white is detected and HIGH when black
detected
if (sensorValuemz == HIGH) {
// turn on motor A
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// set speed to 200 out of possible range 0~255
analogWrite(enA, 120);
// turn on motor B
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// set speed to 200 out of possible range 0~255
analogWrite(enB, 120);
} else {
// turn back motor A
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// set speed to 200 out of possible range 0~255
analogWrite(enA, 120);
// turn forward motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enB, 120);
}
} else {
// turn on motor A
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enA, 120);
// turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enB, 120);
} delay(300);

You might also like