From 4640a8b9f3a17e153c262c9d3de1e8b2b8025303 Mon Sep 17 00:00:00 2001 From: cagdas aras Date: Sun, 9 Apr 2023 23:47:40 +0200 Subject: [PATCH] asd --- Linefollower.ino | 33 +++++++++++++++++++++++ Rumba.ino | 46 +++++++++++++++++++++++++++++++ control.ino | 67 ++++++++++++++++++++++++++++++++++++++++++++++ gyro.ino.ino | 30 +++++++++++++++++++++ motion.ino | 29 ++++++++++++++++++++ ultrasonic.ino.ino | 15 +++++++++++ 6 files changed, 220 insertions(+) create mode 100644 Linefollower.ino create mode 100644 Rumba.ino create mode 100644 control.ino create mode 100644 gyro.ino.ino create mode 100644 motion.ino create mode 100644 ultrasonic.ino.ino diff --git a/Linefollower.ino b/Linefollower.ino new file mode 100644 index 0000000..059dd42 --- /dev/null +++ b/Linefollower.ino @@ -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(); + } +} diff --git a/Rumba.ino b/Rumba.ino new file mode 100644 index 0000000..54898ef --- /dev/null +++ b/Rumba.ino @@ -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); + } +} diff --git a/control.ino b/control.ino new file mode 100644 index 0000000..d44de85 --- /dev/null +++ b/control.ino @@ -0,0 +1,67 @@ +#include "MeMCore.h" +#include + +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(); + } +} diff --git a/gyro.ino.ino b/gyro.ino.ino new file mode 100644 index 0000000..a92310b --- /dev/null +++ b/gyro.ino.ino @@ -0,0 +1,30 @@ +#include "MeMCore.h" +#include + +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); +} diff --git a/motion.ino b/motion.ino new file mode 100644 index 0000000..6ad4be6 --- /dev/null +++ b/motion.ino @@ -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); +} diff --git a/ultrasonic.ino.ino b/ultrasonic.ino.ino new file mode 100644 index 0000000..9710a8f --- /dev/null +++ b/ultrasonic.ino.ino @@ -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); +}