IntroRoboticsLab2/roomba/roomba.ino

47 lines
776 B
C++

#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);
}
}