Advertisement
Guest User

Untitled

a guest
Apr 1st, 2012
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. //STEERING POSITIONS:
  2. //Neutral:90
  3. //Left: 55
  4. //Right: 125
  5.  
  6. //THROTTLE POSITIONS
  7. //Full Reverse: 0
  8. //Full Ahead: 180
  9. //Normal Reverse: 75
  10. //Normal Ahead: 105
  11. //Neutral: 90
  12.  
  13. #include <Servo.h>
  14. #include <avr/interrupt.h>
  15.  
  16. Servo steering;
  17. Servo throttle;
  18. int FrontSensorPin = 2;
  19. int LeftSensorPin = 3;
  20. int RightSensorPin = 4;
  21. int LEDPin = 13;
  22. int AccelerationSpeed = 103;
  23. int ReverseSpeed = 77;
  24. int Neutral = 90;
  25. int LeftTurn = 55;
  26. int RightTurn = 125;
  27. int leftsensorState = 0;
  28. int rightsensorState = 0;
  29. volatile int frontsensorstate;
  30.  
  31. void setup() {
  32.   steering.attach(5);
  33.   throttle.attach(6);
  34.   pinMode(FrontSensorPin, INPUT);
  35.   pinMode(LeftSensorPin, INPUT);
  36.   pinMode(RightSensorPin, INPUT);
  37.   pinMode(LEDPin, OUTPUT);
  38.   digitalWrite(FrontSensorPin, HIGH);
  39.   attachInterrupt(0, frontsensorISR, FALLING);
  40.   interrupts();
  41.   frontsensorstate = 0;
  42.   MoveForward();
  43. }
  44.  
  45. void frontsensorISR(){
  46.     FrontBrakes();
  47.     frontsensorstate = 1;
  48.     digitalWrite(LEDPin, LOW);
  49. }
  50.  
  51. void FrontBrakes() {
  52.       throttle.write(ReverseSpeed);
  53. }
  54.  
  55. void Stop() {
  56.   throttle.write(Neutral);
  57. }
  58.  
  59. void MoveForward(){
  60.   throttle.write(AccelerationSpeed);
  61.   digitalWrite(LEDPin, HIGH);
  62. }
  63.  
  64. void TurnLeft(){
  65.   while(digitalRead(RightSensorPin) < 1){
  66.     steering.write(LeftTurn);
  67.   }
  68.   delay(100);
  69.   steering.write(Neutral);
  70. }
  71.  
  72. void TurnRight(){
  73.   while(digitalRead(LeftSensorPin) < 1){
  74.     steering.write(RightTurn);
  75.   }
  76.   delay(100);
  77.   steering.write(Neutral);
  78. }
  79.  
  80. void BackUp() {
  81.     throttle.write(ReverseSpeed);
  82.     delay(2500);
  83.     Stop();
  84.     delay(500);
  85. }
  86.  
  87. void ReverseAway(){
  88.     throttle.write(ReverseSpeed);
  89.     delay(1350);
  90.     Stop();
  91.     delay(500);
  92.     steering.write(Neutral);
  93.     delay(500);
  94.     MoveForward();
  95. }
  96.  
  97. void loop() {
  98.   steering.write(Neutral); //set steering to neutral - can't do this in setup
  99.   if (frontsensorstate > 0){  //sensor state value - this is set high in interrup routine
  100.     frontsensorstate = 0; //reset sensor state
  101.     delay(200); //need to wait before brakes are released
  102.     Stop(); //release brakes, set throttle to neutral
  103.     delay(200); //need to wait before re-engaging throttle
  104.     if (digitalRead(LeftSensorPin) < 1) { //check if obstacles present to left
  105.       steering.write(RightTurn);
  106.       ReverseAway();
  107.     }
  108.     if (digitalRead(RightSensorPin) < 1) { //check if obstacles present to right
  109.       steering.write(LeftTurn);
  110.       ReverseAway();
  111.     }
  112.     if (digitalRead(RightSensorPin) > 0 && digitalRead(LeftSensorPin) > 0) { //check if no obstacles present either side
  113.       steering.write(LeftTurn);
  114.       ReverseAway();
  115.     }
  116.     if (digitalRead(RightSensorPin) < 1 && digitalRead(LeftSensorPin) < 1) { //check if obstacles present either side
  117.       BackUp();
  118.     }
  119.   }
  120.  
  121.  if (digitalRead(LeftSensorPin) < 1) { //check if obstacles present to left
  122.    TurnRight();
  123.  }
  124.  
  125.   if (digitalRead(RightSensorPin) < 1) { //check if obstacles present to right
  126.    TurnLeft();
  127.  }
  128.  
  129.   if (digitalRead(LeftSensorPin) < 1 && digitalRead(LeftSensorPin) < 1) { //check if obstacles present either side
  130.    throttle.write(Neutral);
  131.   }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement