Advertisement
BlanchardBison

train code

Jan 25th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>  
  2.  
  3. Servo servo0;
  4.  
  5. //don't change
  6. const int LDR0 = 0;
  7. const int LDR1 = 1;
  8. const int USS0 = 2;
  9.  
  10. //pinmodes
  11. const int greenPin = 13;
  12. const int yellowPin = 12;
  13. const int redPin = 11;
  14. const int motorPin = 10;
  15. const int trigPin = 8;
  16. const int echoPin = 7;
  17. const int buttonPin = 2;
  18.  
  19. //change
  20. int WayPoint = 0;  
  21. int hdrLoop = 0;
  22. int LDRValue0 = 0;
  23. int LDRValue1 = 0;
  24. int light_sensitivity0 = 150;
  25. int light_sensitivity1 = 200;
  26. int ultra_sensitivity = 4;
  27. int pos = 0;
  28. long duration, distance;
  29.  
  30.  
  31. void setup()
  32. {
  33.   Serial.begin(9600);
  34.   pinMode(greenPin, OUTPUT); //green
  35.   digitalWrite(greenPin, HIGH);
  36.   pinMode(yellowPin, OUTPUT); //yellow
  37.   digitalWrite(yellowPin, LOW);
  38.   pinMode(redPin, OUTPUT); //red
  39.   digitalWrite(redPin, LOW);  
  40.   pinMode(motorPin, OUTPUT); //motor
  41.   digitalWrite(motorPin, LOW);
  42.   pinMode(trigPin, OUTPUT); //trigPin
  43.   digitalWrite(trigPin, LOW);
  44.   pinMode(echoPin, INPUT); //echoPin
  45.   digitalWrite(echoPin, LOW);
  46.   pinMode(buttonPin, INPUT); //button
  47.   digitalWrite(buttonPin, LOW);
  48.  
  49.   servo0.attach(9);  
  50. }
  51.  
  52. //clockwise
  53. void servoCW(int deg)
  54. {
  55.   for(pos = 0; pos <= deg; pos += 1) // goes from 0 degrees to n degrees
  56.   {                                  // in steps of 1 degree
  57.     servo0.write(pos);              // tell servo to go to position in variable 'pos'
  58.     delay(15);                       // waits 15ms for the servo to reach the position
  59.   }
  60. }
  61.  
  62. //counter clock-wise
  63. void servoCCW(int deg)
  64. {
  65.   for(pos = 0; pos >= deg; pos -= 1) // goes from 0 degrees to n degrees
  66.   {                                  // in steps of 1 degree
  67.     servo0.write(pos);              // tell servo to go to position in variable 'pos'
  68.     delay(15);                       // waits 15ms for the servo to reach the position
  69.   }
  70. }
  71.  
  72. void loop()
  73. {  
  74.   delay(50);
  75.  
  76.   //button is pressed
  77.   if(digitalRead(buttonPin) == HIGH)
  78.   {
  79.     digitalWrite(13, HIGH);
  80.     digitalWrite(12, LOW);
  81.     digitalWrite(11, LOW);    
  82.   }
  83.  
  84.   //if nothing is on tracks
  85.   if (hdrLoop >= 20)
  86.   {
  87.    servoCW(180);
  88.   }
  89.  
  90.   //check train tracks
  91.   if (WayPoint == 3 && hdrLoop < 20)
  92.   {
  93.     digitalWrite(11, HIGH);
  94.    
  95.     //http://www.instructables.com/id/Simple-Arduino-and-HC-SR04-Example/step3/Upload-the-sketch/
  96.     digitalWrite(8, LOW);
  97.     delayMicroseconds(2);
  98.     digitalWrite(8, HIGH);
  99.     delayMicroseconds(10);
  100.     digitalWrite(8, LOW);
  101.     duration = pulseIn(7, HIGH);
  102.     distance = (duration/2) / 29.1;
  103.    
  104.     if(distance <= 8.4)
  105.     {
  106.       hdrLoop = 0;
  107.     }
  108.     hdrLoop += 1;
  109.     delay(100);
  110.   }
  111.  
  112.   //pass second sensor
  113.   if(WayPoint == 2)
  114.   {
  115.     LDRValue1 = analogRead(LDR1);
  116.     if(LDRValue1 < light_sensitivity1)
  117.     {
  118.       WayPoint += 1;
  119.       digitalWrite(13, LOW);
  120.       digitalWrite(12, LOW);
  121.       digitalWrite(11, HIGH); //red on
  122.       digitalWrite(10, HIGH); //put barriers down
  123.       delay(500);
  124.       digitalWrite(10, LOW);
  125.       servoCCW(-90);
  126.     }
  127.   }
  128.  
  129.   //pass first sensor
  130.   if(WayPoint == 1)
  131.   {
  132.     LDRValue0 = analogRead(LDR0);
  133.     if(LDRValue0 < light_sensitivity0)
  134.     {
  135.       WayPoint += 1;
  136.       digitalWrite(13, LOW);
  137.       digitalWrite(12, HIGH); //yellow on
  138.       digitalWrite(11, LOW);
  139.       servoCCW(-90);
  140.     }
  141.   }
  142.  
  143.   //start
  144.   if(WayPoint == 0)
  145.   {
  146.     servoCW(180);
  147.     WayPoint += 1;
  148.   }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement