bingxuan9112

Untitled

Feb 21st, 2021
377
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // generated by mBlock5 for mBot
  2. // codes make you happy
  3.  
  4. #include <MeMCore.h>
  5. #include <Arduino.h>
  6. #include <Wire.h>
  7. #include <SoftwareSerial.h>
  8.  
  9. double currentTime = 0;
  10. double lastTime = 0;
  11. MeUltrasonicSensor ultrasonic_3(3);
  12. MeDCMotor motor_9(9);
  13. MeDCMotor motor_10(10);
  14.  
  15. float distance = 0;
  16.  
  17. double getLastTime(){
  18.   return currentTime = millis() / 1000.0 - lastTime;
  19. }
  20. void move(int direction, int speed) {
  21.   int leftSpeed = 0;
  22.   int rightSpeed = 0;
  23.   if(direction == 1) {
  24.     leftSpeed = speed;
  25.     rightSpeed = speed;
  26.   } else if(direction == 2) {
  27.     leftSpeed = -speed;
  28.     rightSpeed = -speed;
  29.   } else if(direction == 3) {
  30.     leftSpeed = -speed;
  31.     rightSpeed = speed;
  32.   } else if(direction == 4) {
  33.     leftSpeed = speed;
  34.     rightSpeed = -speed;
  35.   }
  36.   motor_9.run((9) == M1 ? -(leftSpeed) : (leftSpeed));
  37.   motor_10.run((10) == M1 ? -(rightSpeed) : (rightSpeed));
  38. }
  39.  
  40. void _delay(float seconds) {
  41.   long endTime = millis() + seconds * 1000;
  42.   while(millis() < endTime) _loop();
  43. }
  44.  
  45. void setup() {
  46.   lastTime = millis() / 1000.0;
  47.   while(!(getLastTime() > 4))
  48.   {
  49.     _loop();
  50.     distance = ultrasonic_3.distanceCm();
  51.     if(distance > 50){
  52.  
  53.         move(1, 50 / 100.0 * 255);
  54.  
  55.     }
  56.  
  57.   }
  58.  
  59. }
  60.  
  61. void _loop() {
  62. }
  63.  
  64. void loop() {
  65.   _loop();
  66. }
  67.  
  68.  
RAW Paste Data