47 lines
1.1 KiB
C++
47 lines
1.1 KiB
C++
#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);
|
|
}
|
|
}
|