This commit is contained in:
Cagdas Aras CIBLAK 2023-04-09 23:47:40 +02:00
commit 4640a8b9f3
6 changed files with 220 additions and 0 deletions

33
Linefollower.ino Normal file
View File

@ -0,0 +1,33 @@
#include "MeMCore.h"
MeLineFollower lineFollower(PORT_1);
MeDCMotor motor1(9);
MeDCMotor motor2(10);
const uint8_t motorSpeed = 100;
void setup() {
Serial.begin(9600);
}
void loop() {
uint8_t sensorValue = lineFollower.readSensors();
Serial.println(sensorValue);
//S1_IN_S2_IN
if (sensorValue == 0) { // Both sensors are on the black line
// Move forward
motor1.run(-motorSpeed);
motor2.run(motorSpeed);
} else if (sensorValue == 2) {
// Turn slightly right
motor1.run(-motorSpeed);
motor2.run(motorSpeed / 200);
} else if (sensorValue == 1) {
// Turn slightly left
motor1.run(-motorSpeed / 200);
motor2.run(motorSpeed);
} else { // Both sensors are on the white surface (lost the line)
motor1.stop();
motor2.stop();
}
}

46
Rumba.ino Normal file
View File

@ -0,0 +1,46 @@
#include "MeMCore.h"
MeUltrasonicSensor ultrasensor(PORT_3);
MeDCMotor motor1(9);
MeDCMotor motor2(10);
uint8_t motorSpeed = 100;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
randomSeed(analogRead(A0));
}
void loop() {
// put your main code here, to run repeatedly:
if (ultrasensor.distanceCm() > 10) {
motor1.run(-motorSpeed);
motor2.run(motorSpeed);
} else {
motor1.stop();
motor2.stop();
delay(500);
// Random rotation duration between 500 ms and 2000 ms
unsigned long rotationDuration = random(500, 2001);
// Random rotation angle between 45 and 315 degrees
int rotationAngle = random(60, 316);
unsigned long rotationTime = map(rotationAngle, 0, 360, 0, rotationDuration);
// Rotate in a random direction
if (random(0, 2) == 0) { // 50% chance of being CW or CCW
motor1.run(-motorSpeed);
motor2.run(-motorSpeed);
} else {
motor1.run(motorSpeed);
motor2.run(motorSpeed);
}
delay(rotationTime);
motor1.stop();
motor2.stop();
delay(500);
}
}

67
control.ino Normal file
View File

@ -0,0 +1,67 @@
#include "MeMCore.h"
#include <Wire.h>
MeDCMotor motor1(9);
MeDCMotor motor2(10);
MeGyro gyro(PORT_4);
const uint8_t motorSpeed = 100;
const unsigned long forwardDuration = 2000;
const float Kp = 5.0;
void setup() {
Serial.begin(9600);
gyro.begin();
}
void turnRight90Degrees() {
float targetAngle = ((int)gyro.getAngleZ() - 90)%360;
float error;
do {
gyro.update();
error = targetAngle - gyro.getAngleZ();
int speedAdjustment = Kp * error;
motor1.run(constrain(-motorSpeed + speedAdjustment, -motorSpeed, motorSpeed));
motor2.run(constrain(motorSpeed - speedAdjustment, motorSpeed, -motorSpeed));
Serial.print("gyro.getAngleZ()= ");
Serial.print(gyro.getAngleZ());
Serial.print("error= ");
Serial.print(error);
Serial.print("speedAdjustment= ");
Serial.println(speedAdjustment);
delay(50);
} while (abs(error) > 2); // Exit the loop when the error is less than 2 degrees
motor1.stop();
motor2.stop();
}
void loop() {
for (int i = 0; i < 4; i++) {
// Move forward
motor1.run(-motorSpeed);
motor2.run(motorSpeed);
delay(forwardDuration);
// Stop the motors
motor1.stop();
motor2.stop();
delay(1000);
// Turn right 90 degrees using the gyro sensor
turnRight90Degrees();
// Stop the motors
motor1.stop();
motor2.stop();
delay(1000);
}
//Stop the robot after drawing the square
while (true) {
motor1.stop();
motor2.stop();
}
}

30
gyro.ino.ino Normal file
View File

@ -0,0 +1,30 @@
#include "MeMCore.h"
#include <Wire.h>
MeGyro gyro(PORT_4);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
gyro.begin();
}
void loop() {
// put your main code here, to run repeatedly:
gyro.update();
Serial.read();
Serial.print("X: ");
Serial.print(gyro.getAngleX() );
Serial.print("Y: ");
Serial.print(gyro.getAngleY() );
Serial.print("Z: ");
Serial.println(gyro.getAngleZ() );
Serial.print(gyro.getAngleX());
Serial.print(",");
Serial.print(gyro.getAngleY());
Serial.print(",");
Serial.println(gyro.getAngleZ());
delay(5);
}

29
motion.ino Normal file
View File

@ -0,0 +1,29 @@
#include "MeMCore.h"
MeDCMotor motor1(9);
MeDCMotor motor2(10);
uint8_t motorSpeed = 100;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
motor1.run(-motorSpeed);
motor2.run(motorSpeed);
delay (1000);
motor1.stop();
motor2.stop();
delay (1000);
motor1.run(-motorSpeed);
motor2.run(-motorSpeed);
delay(1000);
motor1.stop();
motor2.stop();
delay (1000);
}

15
ultrasonic.ino.ino Normal file
View File

@ -0,0 +1,15 @@
#include "MeMCore.h"
MeUltrasonicSensor ultrasensor(PORT_3);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("Distance: ");
Serial.print (ultrasensor.distanceCm());
Serial.println (" cm");
delay(500);
}