created a roomba behavior
This commit is contained in:
parent
89f52a9c2d
commit
072de87350
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue