Advertisement
Guest User

Marcus Pérez Cervantes. MTI. Prof. Mark Gross. Fall 2010. Carnegie Mellon

a guest
Oct 27th, 2010
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. //07 Mini-Project
  2.  
  3. // Declaring constants:
  4. // sensorPins refer to the Sharp 2Y0A21 distance sensors
  5. // ledPins refer to the LEDs
  6. // motorPins refer the Motors
  7. // sensorValues are the voltage that the each sensor detects and maps it to the distance range (0 to 500)
  8. // lightValues map the light value range (0 to 1023)
  9.  
  10. int sensorPin = 5;
  11. int sensorPin2 = 4;
  12. int ledPin = 7;
  13. int ledPin2 = 2;
  14. int motorPin1A = 9;
  15. int motorPin2A = 10;
  16. int motorPin3A = 11;
  17. int motorPin4A = 12;
  18.  
  19. int sensorValue = 0;
  20. int sensorValue2 = 0;
  21.  
  22. int lightValue = 0;
  23. int lightValue2 = 0;
  24.  
  25. // Defining the setup Function:
  26. // sensorPins are INPUTS, this means that they receive information (data in)
  27. // ledPins and motorPins are OUTPUTS, they are writing information (data out)
  28. void setup()
  29. {
  30. Serial.begin(9600);
  31. pinMode (sensorPin, INPUT);
  32. pinMode (sensorPin2, INPUT);
  33. pinMode (ledPin, OUTPUT);
  34. pinMode (ledPin2, OUTPUT);
  35. pinMode (motorPin1A, OUTPUT);
  36. pinMode (motorPin2A, OUTPUT);
  37. pinMode (motorPin3A, OUTPUT);
  38. pinMode (motorPin4A, OUTPUT);
  39.  
  40. }
  41. // Defining the loop Function:
  42. // 1) In order to map the distance range the information has to be read through the sensor pins.
  43. // 2) If both sensor values are bigger than 400, then the car will move and the LEDs will be off
  44. // 3) If sensorValue is bigger than 400 and sensorValue2 is less than 400, the car will moved away from what is sensing near sensorValue2. This will move the car to the left and the LED light associated with sensorValue2 will be on. Meanwhile the other LED light will be off.
  45. // 4) If sensorValue2 is bigger than 400 and sensorValue is less than 400, the car will moved away from what is sensing near sensorValue. This will move the car to the right and the LED light associated with sensorValue will be on. Meanwhile the other LED light will be off.
  46. // 5) If both sensor values are smaller than 400, then the car will completely stop and both LEDs will be on.
  47. // 6) Serial print will give you the different sensor values that the distance sensors are inputting. It will also map it to the light values to determine whether or not the LEDs will be on or off.
  48. void loop ()
  49. {
  50. sensorValue = analogRead(sensorPin);
  51. sensorValue2 = analogRead(sensorPin2);
  52.  
  53. if (sensorValue < 400 && sensorValue2 < 400)
  54. {
  55. digitalWrite(motorPin1A, HIGH);
  56. digitalWrite (motorPin2A, LOW);
  57. digitalWrite(motorPin3A, LOW);
  58. digitalWrite (motorPin4A, HIGH);
  59. digitalWrite (ledPin, LOW);
  60. digitalWrite (ledPin2, LOW);
  61. }
  62.  
  63. else if (sensorValue > 400 && sensorValue2 < 400)
  64. {
  65. digitalWrite(motorPin1A, HIGH);
  66. digitalWrite (motorPin2A, LOW);
  67. digitalWrite(motorPin3A, HIGH);
  68. digitalWrite (motorPin4A, LOW);
  69. digitalWrite (ledPin, HIGH);
  70. digitalWrite (ledPin2, LOW);
  71. }
  72.  
  73. else if (sensorValue < 400 && sensorValue2 > 400)
  74. {
  75. digitalWrite (motorPin1A, LOW);
  76. digitalWrite (motorPin2A, HIGH);
  77. digitalWrite (motorPin3A, LOW);
  78. digitalWrite (motorPin4A, HIGH);
  79. digitalWrite (ledPin, LOW);
  80. digitalWrite (ledPin2, HIGH);
  81. }
  82.  
  83. else if (sensorValue > 400 && sensorValue2 > 400)
  84. {
  85. digitalWrite (motorPin1A, LOW);
  86. digitalWrite (motorPin2A, LOW);
  87. digitalWrite (motorPin3A, LOW);
  88. digitalWrite (motorPin4A, LOW);
  89. digitalWrite (ledPin, HIGH);
  90. digitalWrite (ledPin2, HIGH);
  91. }
  92.  
  93. Serial.print(sensorValue);
  94. Serial.print("(");
  95. Serial.print(lightValue);
  96. Serial.print(")");
  97. Serial.print(" ");
  98. Serial.print(sensorValue2);
  99. Serial.print("(");
  100. Serial.print(lightValue2);
  101. Serial.println(")");
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement