Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include <SimplyFuzzy.h>
  2. #include <AFMotor.h>
  3. #include <Math.h>
  4.  
  5. #define TRIGGER_R  52
  6. #define ECHO_R     53
  7.  
  8. #define TRIGGER_M  38
  9. #define ECHO_M     39
  10.  
  11. #define TRIGGER_L  23
  12. #define ECHO_L     22
  13.  
  14. AF_DCMotor leftWheel(1);
  15. AF_DCMotor rightWheel(2);
  16. SimplyFuzzy sf;
  17. int left, mid, right;
  18. float outLeft, outRight;
  19.  
  20. void setup(){
  21.   Serial.begin(9600);
  22.   pinMode(TRIGGER_R,OUTPUT);
  23.   pinMode(ECHO_R,INPUT);
  24.   pinMode(TRIGGER_M,OUTPUT);
  25.   pinMode(ECHO_M,INPUT);
  26.   pinMode(TRIGGER_L,OUTPUT);
  27.   pinMode(ECHO_L,INPUT);
  28.   sf.init();
  29. }
  30.  
  31. int distanceL(){
  32.  
  33.  digitalWrite(TRIGGER_L, LOW);
  34.  delayMicroseconds(2);
  35.  
  36.  digitalWrite(TRIGGER_L, HIGH);
  37.  delayMicroseconds(10);
  38.  
  39.  digitalWrite(TRIGGER_L, LOW);
  40.  long duration = pulseIn(ECHO_L, HIGH);
  41.  int distance = duration/58.2;
  42.  if(distance > 200) {
  43.   return 200;
  44.  }
  45.  return distance;
  46. }
  47.  
  48. int distanceP(){
  49.  
  50.  digitalWrite(TRIGGER_R, LOW);
  51.  delayMicroseconds(2);
  52.  
  53.  digitalWrite(TRIGGER_R, HIGH);
  54.  delayMicroseconds(10);
  55.  
  56.  digitalWrite(TRIGGER_R, LOW);
  57.  long duration = pulseIn(ECHO_R, HIGH);
  58.  int distance = duration/58.2;
  59.  if(distance > 200) {
  60.   return 200;
  61.  }
  62.  return distance;
  63.  
  64. }
  65.  
  66. int distanceM(){
  67.  
  68.  digitalWrite(TRIGGER_M, LOW);
  69.  delayMicroseconds(2);
  70.  
  71.  digitalWrite(TRIGGER_M, HIGH);
  72.  delayMicroseconds(10);
  73.  
  74.  digitalWrite(TRIGGER_M, LOW);
  75.  long duration = pulseIn(ECHO_M, HIGH);
  76.  int distance = duration/58.2;
  77.  if(distance > 200) {
  78.   return 200;
  79.  }
  80.  return distance;
  81. }
  82.  
  83. bool reverseCheck(float oldSpeed) {
  84.   if(oldSpeed<0) return true;
  85.   else return false;
  86. }
  87.  
  88. float speedCheck(float oldSpeed) {
  89.  
  90. }
  91.  
  92. void loop(){
  93.   left = distanceL();
  94.   Serial.println("Inputs: ");
  95.   Serial.print("Left : ");
  96.   Serial.println(left);
  97.  
  98.   mid = distanceM();
  99.   Serial.print("Mid  : ");
  100.   Serial.println(mid);
  101.  
  102.   right = distanceP();
  103.   Serial.print("Right: ");
  104.   Serial.println(right);
  105.  
  106.   Serial.println("\n");
  107.  
  108.   sf.setInputs(left, mid, right);
  109.   outLeft = sf.getOutput(true);
  110.  
  111.   Serial.print("Left Wheel : ");
  112.   Serial.print(outLeft);
  113.  
  114.   Serial.println("\n");
  115.   outRight = sf.getOutput(false);
  116.  
  117.   Serial.print("Right Wheel: ");
  118.   Serial.print(outRight);
  119.   Serial.println("\n");
  120.  
  121.   if(reverseCheck(outLeft)){
  122.    
  123.     leftWheel.run(RELEASE);
  124.     leftWheel.run(BACKWARD);
  125.     outLeft=abs(outLeft);
  126.   } else {
  127.     leftWheel.run(FORWARD);
  128.   }
  129.  
  130.   if(reverseCheck(outRight)){
  131.     rightWheel.run(RELEASE);
  132.     rightWheel.run(BACKWARD);
  133.     outRight=abs(outRight);
  134.   } else {
  135.     rightWheel.run(FORWARD);
  136.   }
  137.  
  138.   leftWheel.setSpeed(outLeft);
  139.   rightWheel.setSpeed(outRight);
  140.  
  141.   delay(100);
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement