Advertisement
mik32

Robot Light Follower

Feb 6th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.19 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. Servo LeftServo;
  4. Servo RightServo;
  5.  
  6. #define ServoLeftArgumentZero  88       //stop cond 88 is zero state !!!!!!!!!!!
  7. #define ServoRightArgumentZero  94     //stop condition 94 is zero state !!!!!!
  8.  
  9. #define Button1  2                      //2 pin przyciski na robocie
  10. #define Button2  4                     //4 pin przyciski na robocie
  11.  
  12. int ldrRight, ldrMidle, ldrLeft;       //global
  13.  
  14. //////////////////////////////////////////////////////////////////////////////////
  15. //Servo Right Arg if > 94 then backward
  16. // if Servo Left Arg < 88 then backward
  17. //*************************************
  18. ///Servo Right Arg if < 94 then forward
  19. // if Servo Left Arg > 88 then forward
  20. //////////////////////////////////////////////////////////////////////////////////backward
  21. void backward(int Velocity) {  //velocity is an argument
  22.  
  23.  RightServo.write(ServoRightArgumentZero + Velocity );
  24.  LeftServo.write(ServoLeftArgumentZero - Velocity );
  25. }
  26. //////////////////////////////////////////////////////////////////////////////////forward
  27. void forward(int Velocity) {   //velocity is an argument
  28.  
  29.   RightServo.write(ServoRightArgumentZero - Velocity );
  30.   LeftServo.write(ServoLeftArgumentZero + Velocity );
  31. }
  32. /////////////////////////////////////////////////////////////////////////////////turnLeft
  33. void turnLeft(int Velocity) {
  34.  
  35.   RightServo.write(ServoRightArgumentZero - Velocity );
  36.   LeftServo.write(ServoLeftArgumentZero - Velocity );
  37. }
  38. /////////////////////////////////////////////////////////////////////////////////turnRight
  39. void turnRight(int Velocity) {
  40.  
  41.   RightServo.write(ServoRightArgumentZero + Velocity );
  42.   LeftServo.write(ServoLeftArgumentZero + Velocity );
  43. }
  44. //////////////////////////////////////////////////////////////////////////////////Stop
  45. void Stop() {
  46.   RightServo.write(ServoRightArgumentZero);      // from 20 to 150 !!!!RIGHT 94 STOP
  47.   LeftServo.write(ServoLeftArgumentZero);        //!!!!!!!!!!88 LEFT STOP !!!!!!!!!!
  48. }
  49. ///////////////////////////////////////////////////////////////////////////////Navigation
  50. void Navig(int ldrLeft, int ldrMidle, int ldrRight, int velocity, int timeMiliSec){    //przekazuje argument timeMiliSec ale nic z nim specjalnego nie robi
  51.    
  52.     if(ldrLeft > ldrRight && ldrLeft > ldrMidle){
  53.       digitalWrite(7,HIGH);  
  54.       digitalWrite(12,LOW);                                
  55.       digitalWrite(13,LOW);
  56.       turnLeft(velocity);
  57.     }
  58.    
  59.     else if(ldrRight > ldrLeft && ldrRight > ldrMidle){    //0,9 jest poto zeby sztucznie wyeleminować mozliwą roznice w czulosciach tych detektorow
  60.       digitalWrite(7,LOW);  
  61.       digitalWrite(12,HIGH);                                
  62.       digitalWrite(13,LOW);
  63.       turnRight(velocity);
  64.     }
  65.    
  66.     else if( ldrMidle > ldrRight || ldrMidle > ldrLeft ){
  67.       forward(velocity);
  68.       digitalWrite(7,LOW);
  69.       digitalWrite(12,LOW);                                
  70.       digitalWrite(13,HIGH);
  71.     }
  72.    
  73. }///////////////////////////////////////////////////////////////////////////////////////////////////////////End Navig
  74.  
  75. void setup() {
  76.   Serial.begin(9600);
  77.  
  78.   RightServo.attach(5);
  79.   LeftServo.attach(6);
  80.  
  81.   RightServo.write(ServoRightArgumentZero);      // set velocity = 0 each servo
  82.   LeftServo.write(ServoLeftArgumentZero);        // set velocity = 0 each servo
  83.  
  84.   pinMode(Button1, INPUT);
  85.   pinMode(Button2, INPUT);
  86.  
  87.   pinMode(A0, INPUT);
  88.   pinMode(A1, INPUT);
  89.   pinMode(A2, INPUT);
  90.  
  91.    pinMode(12,OUTPUT);                            //dioda Orange
  92.    pinMode(13,OUTPUT);                            //dioda Red
  93.    pinMode(7,OUTPUT);
  94.  
  95.         //Calibration();                          //call calibration function it's using a global variables
  96. }       //end setup
  97.  
  98. void loop() {
  99.      
  100.     ldrRight = analogRead(A0);                   //right
  101.     ldrMidle = analogRead(A1);                   //midlle
  102.     ldrLeft = analogRead(A2);                    //left
  103.    
  104.     int velocity = 30;                          //argument of forward, turnL, etc. functions
  105.     int timeMiliSec = 333;                      // it is parameter of the function Navig()
  106.    
  107.     Navig(ldrLeft, ldrMidle, ldrRight, velocity, timeMiliSec);
  108.                
  109. }//end loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement