coldzeroextreme

Project_SENILA firmware

Aug 28th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.51 KB | None | 0 0
  1. /* ========================
  2.   TriniX Development
  3.   Project: Mr Senila
  4.   Author: coldzero
  5. =========================== */
  6.  
  7. #include <SoftwareSerial.h>
  8. #include <Servo.h>
  9.  
  10. #define btRxPin 10 //recv pin
  11. #define btTxPin 11 //transmit pin
  12.  
  13. #define MOTOR1_PIN1 3
  14. #define MOTOR1_PIN2 5
  15. #define MOTOR2_PIN1 6
  16. #define MOTOR2_PIN2 9
  17.  
  18. #define SERVO_PIN 7
  19.  
  20. #define TRIGER_PIN 12
  21. #define ECHO_PIN 13
  22.  
  23. //RX pin = receive pin, TX pin = transmit pin
  24. SoftwareSerial BlueTooth(btRxPin, btTxPin);
  25.  
  26. //Sensor servo stuff
  27. Servo SensorServo;
  28. int SensorServoPos = 0;
  29. int iDirection = 1; //1 - Right, 2 - Left
  30. int MaxAngle = 150;
  31. int MinAngle = 0;
  32. int ServoStep = 10;
  33. int iCurAngle = 0;
  34.  
  35. //Sensor stuff
  36. int iTime = 0;
  37. int iDistance = 0;
  38. int iDistanceMap[3];
  39. int iCriticalDistance = 20;
  40.  
  41. boolean gotMission = false;
  42.  
  43. void setup()
  44. {
  45.   Serial.begin(9600);
  46.  
  47.   BlueTooth.begin(9600);
  48.  
  49.   pinMode(MOTOR1_PIN1, OUTPUT);
  50.   pinMode(MOTOR1_PIN2, OUTPUT);
  51.   pinMode(MOTOR2_PIN1, OUTPUT);
  52.   pinMode(MOTOR2_PIN2, OUTPUT);
  53.  
  54.   SensorServo.attach(SERVO_PIN);
  55.   SensorServo.write(80);
  56.  
  57.   pinMode(TRIGER_PIN, OUTPUT);
  58.   pinMode(ECHO_PIN, INPUT);
  59.  
  60.   gotMission = true;
  61. }
  62.  
  63. void loop()
  64. {
  65.   if (gotMission)
  66.   {
  67.     /*iCurAngle = 0;
  68.     for (int i=0; i<3; i++)
  69.     {
  70.       SensorServo.write(iCurAngle);
  71.       if (iCurAngle == 0) //wait for the servo to turn
  72.         delay(1200);
  73.       else
  74.         delay(800);
  75.       //Sensor stuff first!!!
  76.       digitalWrite(TRIGER_PIN, LOW); //to ensure a clean TRIGER
  77.       delayMicroseconds(2);
  78.       digitalWrite(TRIGER_PIN, HIGH);
  79.       delayMicroseconds(5);
  80.       digitalWrite(TRIGER_PIN, LOW);
  81.  
  82.       iTime = pulseIn(ECHO_PIN, HIGH);
  83.       iDistance = (iTime/2) / 29.1;
  84.       iDistanceMap[i] = iDistance;
  85.  
  86.       //For DEBUGING
  87.       Serial.print(iDistance);
  88.       Serial.println(" cm");
  89.       iCurAngle += 90;
  90.     }*/
  91.    
  92.     iDistanceMap[1] = GetDistanceFrom(80);
  93.    
  94.     if (iDistanceMap[1] <= iCriticalDistance && iDistanceMap[1] >= 0) //critical distance
  95.     {
  96.       StopTank(); //STOP IT OR YOU WILL DIE!
  97.       iDistanceMap[0] = GetDistanceFrom(0);
  98.       if (iDistanceMap[0] < 0)
  99.         iDistanceMap[0] = 200;
  100.       iDistanceMap[2] = GetDistanceFrom(180);
  101.       if (iDistanceMap[2] < 0)
  102.         iDistanceMap[2] = 200;
  103.      
  104.       if (iDistanceMap[0] > iDistanceMap[2])
  105.       {
  106.         TurnForLeft(80);
  107.         delay(2000);
  108.         StopTank();
  109.       }
  110.       else
  111.       {
  112.         TurnForRight(80);
  113.         delay(2000);
  114.         StopTank();
  115.       }
  116.     }
  117.     else
  118.     {
  119.       MoveForward(80);
  120.     }
  121.  
  122.     //MoveForward(100);
  123.  
  124.     //BlueTooth.listen();
  125.     if (BlueTooth.available() > 0)
  126.     {
  127.       char inBtByte = BlueTooth.read();
  128.       if (inBtByte == 1) //UP
  129.       {
  130.         BlueTooth.write("UP");
  131.         Move(255, 255);
  132.       }
  133.       else if (inBtByte == 2) //DOWN
  134.       {
  135.         BlueTooth.write("DOWN");
  136.         Move(-255, -255);
  137.       }
  138.       else if (inBtByte == 3) //LEFT
  139.       {
  140.         BlueTooth.write("LEFT");
  141.         Move(-255,255);
  142.       }
  143.       else if (inBtByte == 4) //RIGHT
  144.       {
  145.         BlueTooth.write("RIGHT");
  146.         Move(255,-255);
  147.       }
  148.       else if (inBtByte == 5)
  149.       {
  150.         BlueTooth.write("STOP");
  151.         Move(0,0);
  152.       }
  153.     }
  154.     delay(100);
  155.   }
  156. }
  157.  
  158. int GetDistanceFrom(int iAngle)
  159. {
  160.   int itempDistance, itempTime;
  161.  
  162.   SensorServo.write(iAngle);
  163.   delay(800);
  164.   //Sensor stuff first!!!
  165.   digitalWrite(TRIGER_PIN, LOW); //to ensure a clean TRIGER
  166.   delayMicroseconds(2);
  167.   digitalWrite(TRIGER_PIN, HIGH);
  168.   delayMicroseconds(5);
  169.   digitalWrite(TRIGER_PIN, LOW);
  170.  
  171.   itempTime = pulseIn(ECHO_PIN, HIGH);
  172.   itempDistance = (itempTime/2) / 29.1;
  173.  
  174.   //For DEBUGING
  175.   Serial.print(itempDistance);
  176.   Serial.println(" cm");
  177.  
  178.   return itempDistance;
  179. }
  180.  
  181. void StopTank()
  182. {
  183.   analogWrite(MOTOR1_PIN1, 0);
  184.   analogWrite(MOTOR1_PIN2, 0);
  185.  
  186.   analogWrite(MOTOR2_PIN1, 0);
  187.   analogWrite(MOTOR2_PIN2, 0);
  188. }
  189.  
  190. void MoveForward(int MotorSpeed)
  191. {
  192.   analogWrite(MOTOR1_PIN1, MotorSpeed);
  193.   analogWrite(MOTOR1_PIN2, 0);
  194.  
  195.   analogWrite(MOTOR2_PIN1, MotorSpeed);
  196.   analogWrite(MOTOR2_PIN2, 0);
  197. }
  198.  
  199. void MoveBackward(int MotorSpeed)
  200. {
  201.   analogWrite(MOTOR1_PIN1, 0);
  202.   analogWrite(MOTOR1_PIN2, -MotorSpeed);
  203.  
  204.   analogWrite(MOTOR2_PIN1, 0);
  205.   analogWrite(MOTOR2_PIN2, -MotorSpeed);
  206. }
  207.  
  208. void TurnForLeft(int MotorSpeed)
  209. {
  210.   analogWrite(MOTOR1_PIN1, MotorSpeed);
  211.   analogWrite(MOTOR1_PIN2, 0);
  212.  
  213.   analogWrite(MOTOR2_PIN1, 0);
  214.   analogWrite(MOTOR2_PIN2, 0);
  215. }
  216.  
  217. void TurnForRight(int MotorSpeed)
  218. {
  219.   analogWrite(MOTOR1_PIN1, 0);
  220.   analogWrite(MOTOR1_PIN2, 0);
  221.  
  222.   analogWrite(MOTOR2_PIN1, MotorSpeed);
  223.   analogWrite(MOTOR2_PIN2, 0);
  224. }
  225.  
  226. void TurnBackLeft(int MotorSpeed)
  227. {
  228.   analogWrite(MOTOR1_PIN1, 0);
  229.   analogWrite(MOTOR1_PIN2, -MotorSpeed);
  230.  
  231.   analogWrite(MOTOR2_PIN1, 0);
  232.   analogWrite(MOTOR2_PIN2, 0);
  233. }
  234.  
  235. void TurnBackRight(int MotorSpeed)
  236. {
  237.   analogWrite(MOTOR1_PIN1, 0);
  238.   analogWrite(MOTOR1_PIN2, 0);
  239.  
  240.   analogWrite(MOTOR2_PIN1, 0);
  241.   analogWrite(MOTOR2_PIN2, -MotorSpeed);
  242. }
  243.  
  244. void Move(int speedLeft, int speedRight)
  245. {
  246.   if (speedLeft >= 0)
  247.   {
  248.     analogWrite(MOTOR1_PIN1, speedLeft);
  249.     analogWrite(MOTOR1_PIN2, 0);
  250.   }
  251.   else
  252.   {
  253.     analogWrite(MOTOR1_PIN1, 0);
  254.     analogWrite(MOTOR1_PIN2, -speedLeft);
  255.   }
  256.  
  257.   if (speedRight >= 0)
  258.   {
  259.     analogWrite(MOTOR2_PIN1, speedRight);
  260.     analogWrite(MOTOR2_PIN2, 0);
  261.   }
  262.   else
  263.   {
  264.     analogWrite(MOTOR2_PIN1, 0);
  265.     analogWrite(MOTOR2_PIN2, -speedRight);
  266.   }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment