Scanning for Line Following Robot

Finally worked out ‘scanning’ for the 2.S994 final project (linefollowing robot) – now to integrate this into the while loops that check the sensors! I’ll try to figure out how to make wordpress and Arduino code play nicely later … but this should work for now.

Also, I want to make this into a function that can be called. That would be neat.

Revision: All the code was initially in this post but now it is “after the jump” – click continue reading to get to the code.

  1. /** Scan test
  2. Date: 05 14 2012
  3. Used on … 2.S994 Linefollowing robot (Spring 2012)
  4. Jacqueline Sly **/
  5. //Variables – Motor Speeds
  6. int slow = 100;
  7. //Variables – Time
  8. int time_short = 200;
  9. int time_medium = 250;
  10. //Variables – Indices
  11. int iThreshold = 10;
  12. int iThreshold_upper = 25;
  13. int iThreshold_upper2 = 35;
  14. int j = 0;
  15. /** MOTOR DRIVER **/
  16. // Pololu motor driver pin assignments
  17. int PWMA    = 11;
  18. int AIN2    = 10;
  19. int AIN1    = 9;
  20. // Pololu drive A
  21. int PWMB    = 5;
  22. int BIN1    = 7;
  23. // Pololu drive B
  24. int BIN2    = 6;
  25. int STDBY   = 8;
  26. void setup() {
  27. /**MOTOR DRIVER **/
  28. pinMode(AIN1, OUTPUT);
  29. pinMode(AIN2, OUTPUT);
  30. pinMode(PWMA, OUTPUT);
  31. pinMode(BIN1, OUTPUT);
  32. pinMode(BIN2, OUTPUT);
  33. pinMode(PWMB, OUTPUT);
  34. digitalWrite(AIN1, HIGH);
  35. digitalWrite(AIN2, HIGH);
  36. digitalWrite(PWMA, LOW);
  37. digitalWrite(BIN1, HIGH);
  38. digitalWrite(BIN2, HIGH);
  39. digitalWrite(PWMB, LOW);
  40. digitalWrite(STDBY,HIGH);
  41. /**Serial Comms**/
  42. Serial.begin(9600);
  43. }
  44. /**motorWrite function**/
  45. void motorWrite(int motorSpeed, int xIN1, int xIN2, int PWMx) {
  46. motorSpeed = constrain(motorSpeed, -255, 255);   // Just in case…
  47. if (motorSpeed > 0)          // it’s forward
  48. {
  49. digitalWrite(xIN1, LOW);
  50. digitalWrite(xIN2, HIGH);
  51. analogWrite(PWMx, motorSpeed);
  52. }
  53. else                         // it’s reverse
  54. {
  55. digitalWrite(xIN1, HIGH);
  56. digitalWrite(xIN2, LOW);
  57. analogWrite(PWMx, abs(motorSpeed));
  58. }
  59. }
  60. void loop() {
  61. while(j<4){
  62. int i = 0;
  63. while(i<iThreshold){
  64. Serial.println(i);
  65. delay(20);
  66. Serial.println(“help – first”);
  67. delay(20);
  68. motorWrite(-slow, AIN1, AIN2, PWMA); //right
  69. motorWrite(slow, BIN1, BIN2, PWMB); //left
  70. delay(time_short);
  71. i++;
  72. }
  73. while((i>iThreshold || i == iThreshold) && i<iThreshold_upper){
  74. Serial.println(i);
  75. delay(20);
  76. Serial.println(“help – second”);
  77. delay(20);
  78. motorWrite(slow, AIN1, AIN2, PWMA); //right
  79. motorWrite(-slow, BIN1, BIN2, PWMB); //left
  80. delay(time_short);
  81. i++;
  82. }
  83. while((i>iThreshold_upper || i ==iThreshold_upper) && i<iThreshold_upper2){
  84. Serial.println(i);
  85. delay(20);
  86. Serial.println(“help – third”);
  87. delay(20);
  88. motorWrite(-slow, AIN1, AIN2, PWMA); //right
  89. motorWrite(slow, BIN1, BIN2, PWMB); //left
  90. delay(time_short);
  91. i++;
  92. }
  93. j++;
  94. Serial.print(“j=”);
  95. Serial.println(j);
  96. delay(20);
  97. }
  98. // stop motor
  99. Write(0, AIN1, AIN2, PWMA); //right
  100. motorWrite(0, BIN1, BIN2, PWMB); //left
  101. }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s