created a roomba behavior

This commit is contained in:
Gabri6 2023-04-24 12:07:05 +02:00
parent 89f52a9c2d
commit 072de87350
1 changed files with 47 additions and 0 deletions

47
roomba/roomba.ino Normal file
View File

@ -0,0 +1,47 @@
#include <MeMCore.h>
MeUltrasonicSensor sensor(3);
MeDCMotor leftRotor(9);
MeDCMotor rightRotor(10);
float distance;
float lSpeed;
float rSpeed;
float duration;
int Side;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
leftRotor.run(0);
rightRotor.run(0);
lSpeed = -200.0;
rSpeed = 200.0;
}
void loop() {
// put your main code here, to run repeatedly:
distance = sensor.distanceCm();
duration = random(1000);
if (distance>15.0){
leftRotor.run(lSpeed);
rightRotor.run(rSpeed);
}
else{
Side = random(2);
if (Side == 0){
leftRotor.run(lSpeed);
rightRotor.run(-rSpeed);
}
if (Side == 1){
leftRotor.run(-lSpeed);
rightRotor.run(rSpeed);
}
delay(duration);
}
}