int ir1=A0; int ir2=A1; int ir3=A2; int count=0; boolean state = true; int enA = 6; //motoriin hurd tohiruulah int in1 = 8; int in2 = 7; // Zuun motor
Thursday, October 31, 2024
3 мэдрэгчтэй шоо цуглуулах робот код
Tuesday, October 29, 2024
IR sensor test
void setup()
Saturday, October 26, 2024
Shugam dagadag robot PID
#include <QTRSensors.h>
QTRSensors qtrrc((unsigned char[]) {A0, A1, A2, A3, A4, A5, A6, A7}, // sensor inputs
8, // number of sensors
2500, // timeout
EMITTER_PIN); // emitter pin
// Motor pins
const int motor1Pin1 = 9;
const int motor1Pin2 = 10;
const int motor2Pin1 = 11;
const int motor2Pin2 = 12;
// PID constants
float Kp = 0.2;
float Ki = 0.001;
float Kd = 0.1;
float error, lastError, errorSum;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize QTR sensors
qtrrc.setTypeRC();
// Initialize motor pins as outputs
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
}
void loop() {
// Read sensor values
unsigned int sensorValues[8];
qtrrc.read(sensorValues);
// Calculate error (simplified example)
error = sensorValues[2] - (sensorValues[0] + sensorValues[4]) / 2;
// Calculate PID output
float P = Kp * error;
float I = Ki * errorSum;
float D = Kd * (error - lastError);
float output = P + I + D;
// Control motors
if (output > 0) {
// Turn right
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
} else if (output < 0) {
// Turn left
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
} else {
// Go straight
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
lastError = error;
errorSum += error;
}
Shugam tooloh
ОБЬЕКТ ТООЛОХ
int irs = 7;
int counter = 0; void setup() { Serial.begin(9600); pinMode(irs,INPUT); } void loop() { while(digitalRead(irs)==LOW) { counter++; Serial.print("Count: "); Serial.println(counter); delay(2000); } }
https://vsa.edu.in/b7-object-counter-using-serial-monitor/