Advertisement
safwan092

car-robot-2-color-sensor-3-colors

Feb 19th, 2022
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.26 KB | None | 0 0
  1. #define S0 2
  2. #define S1 3
  3. #define S2 4
  4. #define S3 A0
  5. #define sensorOut A1
  6. #define sensorOut2 11
  7. #define S22 12
  8. #define S23 13
  9.  
  10.  
  11. int frequency = 0;
  12. int color = 0;
  13. int frequency2 = 0;
  14. int color2 = 0;
  15.  
  16. #define BSW A2
  17. #define RSW A3
  18. #define GSW A4
  19.  
  20.  
  21.  
  22. #define MOTOR_SPEED 180 //180   0-255
  23.  
  24. //Right motor
  25. int enableRightMotor = 6;
  26. int rightMotorPin1 = 7;
  27. int rightMotorPin2 = 8;
  28.  
  29. //Left motor
  30. int enableLeftMotor = 5;
  31. int leftMotorPin1 = 9;
  32. int leftMotorPin2 = 10;
  33.  
  34. void setup()
  35. {
  36.   Serial.begin(9600);
  37.   pinMode(S0 , OUTPUT);
  38.   pinMode(S1 , OUTPUT);
  39.   pinMode(S2 , OUTPUT);
  40.   pinMode(S3 , OUTPUT);
  41.   pinMode(S22 , OUTPUT);
  42.   pinMode(S23 , OUTPUT);
  43.   pinMode(sensorOut , INPUT);
  44.   pinMode(sensorOut2 , INPUT);
  45.   //Setting frequency-scaling
  46.   digitalWrite(S0, HIGH);
  47.   digitalWrite(S1, LOW);
  48.  
  49.   pinMode(BSW, INPUT);
  50.   pinMode(RSW, INPUT);
  51.   pinMode(GSW, INPUT);
  52.  
  53.   //The problem with TT gear motors is that, at very low pwm value it does not even rotate.
  54.   //If we increase the PWM value then it rotates faster and our robot is not controlled in that speed and goes out of line.
  55.   //For that we need to increase the frequency of analogWrite.
  56.   //Below line is important to change the frequency of PWM signal on pin D5 and D6
  57.   //Because of this, motor runs in controlled manner (lower speed) at high PWM value.
  58.   //This sets frequecny as 7812.5 hz.
  59.   TCCR0B = TCCR0B & B11111000 | B00000010 ;
  60.  
  61.   // put your setup code here, to run once:
  62.   pinMode(enableRightMotor, OUTPUT);
  63.   pinMode(rightMotorPin1, OUTPUT);
  64.   pinMode(rightMotorPin2, OUTPUT);
  65.  
  66.   pinMode(enableLeftMotor, OUTPUT);
  67.   pinMode(leftMotorPin1, OUTPUT);
  68.   pinMode(leftMotorPin2, OUTPUT);
  69.  
  70.   rotateMotor(0, 0);
  71. }
  72.  
  73.  
  74. void loop()
  75. {
  76.  
  77.   color  = readColor();// 0  1  2  3
  78.   color2 = readColor2();
  79.   delay(10);
  80.   // 1 = RED
  81.   // 2 = BLACK
  82.   // 3 = GREEN
  83.   int BSW_status = digitalRead(BSW);
  84.   int RSW_status = digitalRead(RSW);
  85.   int GSW_status = digitalRead(GSW);
  86.   Serial.print(BSW_status);
  87.   Serial.print(RSW_status);
  88.   Serial.println(GSW_status);
  89.  
  90.  
  91.   if (BSW_status == 1 && RSW_status == 0 && GSW_status == 0) {
  92.     //Follow BLUE line
  93.     // 2 = BLACK
  94.     if (color == 2 && color2 == 2) {
  95.       rotateMotor(MOTOR_SPEED, MOTOR_SPEED);//FRONT
  96.     }
  97.     else if (color == 2 && color2 != 2) {
  98.       rotateMotor(MOTOR_SPEED, -MOTOR_SPEED);//LEFT
  99.     }
  100.     else if (color != 2 && color2 == 2) {
  101.       rotateMotor(-MOTOR_SPEED, MOTOR_SPEED);//RIGHT
  102.     }
  103.     else if (color != 2 && color2 != 2) {
  104.       rotateMotor(0, 0);//STOP
  105.     }
  106.   }
  107.   else if (BSW_status == 0 && RSW_status == 1 && GSW_status == 0) {
  108.     //Follow RED line
  109.     // 1 = RED
  110.     if (color == 1 && color2 == 1) {
  111.       rotateMotor(MOTOR_SPEED, MOTOR_SPEED);//FRONT
  112.     }
  113.     else if (color == 1 && color2 != 1) {
  114.       rotateMotor(MOTOR_SPEED, -MOTOR_SPEED);//LEFT
  115.     }
  116.     else if (color != 1 && color2 == 1) {
  117.       rotateMotor(-MOTOR_SPEED, MOTOR_SPEED);//RIGHT
  118.     }
  119.     else if (color != 1 && color2 != 1) {
  120.       rotateMotor(0, 0);//STOP
  121.     }
  122.   }
  123.  
  124.   else if (BSW_status == 0 && RSW_status == 0 && GSW_status == 1) {
  125.     //Follow GREEN line
  126.     // 3 = GREEN
  127.     if (color == 3 && color2 == 3) {
  128.       rotateMotor(MOTOR_SPEED, MOTOR_SPEED);//FRONT
  129.     }
  130.     else if (color == 3 && color2 != 3) {
  131.       rotateMotor(MOTOR_SPEED, -MOTOR_SPEED);//LEFT
  132.     }
  133.     else if (color != 3 && color2 == 3) {
  134.       rotateMotor(-MOTOR_SPEED, MOTOR_SPEED);//RIGHT
  135.     }
  136.     else if (color != 3 && color2 != 3) {
  137.       rotateMotor(0, 0);//STOP
  138.     }
  139.   }
  140.  
  141.   else {
  142.     rotateMotor(0, 0);
  143.   }
  144.  
  145.  
  146. }//end of LOOP
  147.  
  148.  
  149. void rotateMotor(int rightMotorSpeed, int leftMotorSpeed)
  150. {
  151.  
  152.   if (rightMotorSpeed < 0)
  153.   {
  154.     digitalWrite(rightMotorPin1, LOW);
  155.     digitalWrite(rightMotorPin2, HIGH);
  156.   }
  157.   else if (rightMotorSpeed > 0)
  158.   {
  159.     digitalWrite(rightMotorPin1, HIGH);
  160.     digitalWrite(rightMotorPin2, LOW);
  161.   }
  162.   else
  163.   {
  164.     digitalWrite(rightMotorPin1, LOW);
  165.     digitalWrite(rightMotorPin2, LOW);
  166.   }
  167.  
  168.   if (leftMotorSpeed < 0)
  169.   {
  170.     digitalWrite(leftMotorPin1, LOW);
  171.     digitalWrite(leftMotorPin2, HIGH);
  172.   }
  173.   else if (leftMotorSpeed > 0)
  174.   {
  175.     digitalWrite(leftMotorPin1, HIGH);
  176.     digitalWrite(leftMotorPin2, LOW);
  177.   }
  178.   else
  179.   {
  180.     digitalWrite(leftMotorPin1, LOW);
  181.     digitalWrite(leftMotorPin2, LOW);
  182.   }
  183.   analogWrite(enableRightMotor, abs(rightMotorSpeed));
  184.   analogWrite(enableLeftMotor, abs(leftMotorSpeed));
  185. }
  186.  
  187.  
  188.  
  189. //Read-Color [1] Function
  190.  
  191. int readColor() {
  192.  
  193.   ////////////////////////////////////////////////////////
  194.  
  195.   //Setting red filtered photodiodes to be read
  196.  
  197.   digitalWrite(S2, LOW);
  198.   digitalWrite(S3, LOW);
  199.  
  200.  
  201.   //Reading the output frequency
  202.   frequency = pulseIn(sensorOut, LOW);
  203.   int R = frequency;
  204.   //Printing the value on the serial monitor
  205.   Serial.print("R= ");  //printing name
  206.   Serial.print(frequency);  //printing RED color frequency
  207.   Serial.print("  ");
  208.   delay(50);
  209.  
  210.   ////////////////////////////////////////////////////////
  211.  
  212.   //Setting Green filtered photodiodes to be read
  213.  
  214.   digitalWrite(S2, HIGH);
  215.   digitalWrite(S3, HIGH);
  216.  
  217.   //Reading the output frequency
  218.   frequency = pulseIn(sensorOut, LOW);
  219.   int G = frequency;
  220.  
  221.   //Printing the value on the serial monitor
  222.   Serial.print("G= ");  //printing name
  223.   Serial.print(frequency);  //printing RED color frequency
  224.   Serial.print("  ");
  225.   delay(50);
  226.  
  227.   ////////////////////////////////////////////////////////
  228.  
  229.   //Setting Blue filtered photodiodes to be read
  230.  
  231.   digitalWrite(S2, LOW);
  232.   digitalWrite(S3, HIGH);
  233.  
  234.   //Reading the output frequency
  235.   frequency = pulseIn(sensorOut, LOW);
  236.   int B = frequency;
  237.  
  238.   //Printing the value on the serial monitor
  239.   Serial.print("B= ");  //printing name
  240.   Serial.print(frequency);  //printing RED color frequency
  241.   Serial.println("  ");
  242.   delay(50);
  243.  
  244.   ////////////////////////////////////////////////////////
  245.  
  246.   if (R < 57 && R > 0 && G < 110 && G > 40 && B < 80 && B > 25  ) {
  247.     color = 1; // Red ✔[18-2-2022]
  248.     Serial.println("\t\t\tRED detected!");
  249.   }
  250.  
  251.   else if (R < 111 && R > 25 && G < 74 && G > 20 && B < 70 && B > 20  ) {
  252.     color = 3; // Green ✔[19-2-2022]
  253.     Serial.println("\t\t\tGREEN detected!");
  254.   }
  255.   else if (R < 140 && R > 20 && G < 150 && G > 30 && B < 120 && B > 16  ) {
  256.     color = 2; // Black ✔[18-2-2022]
  257.     Serial.println("\t\t\tBLACK detected!");
  258.   }
  259.   else {
  260.     color = 0;
  261.   }
  262.   return color;
  263. }
  264.  
  265. ///////////////////////////////////////////////////////////////////////////////////////////////
  266.  
  267. //Read-Color [2] Function
  268.  
  269. int readColor2() {
  270.  
  271.   ////////////////////////////////////////////////////////
  272.  
  273.   //Setting red filtered photodiodes to be read
  274.  
  275.   digitalWrite(S22, LOW);
  276.   digitalWrite(S23, LOW);
  277.  
  278.  
  279.   //Reading the output frequency
  280.   frequency2 = pulseIn(sensorOut2, LOW);
  281.   int R2 = frequency2;
  282.   //Printing the value on the serial monitor
  283.   Serial.print("R2= ");  //printing name
  284.   Serial.print(frequency2);  //printing RED color frequency
  285.   Serial.print("  ");
  286.   delay(50);
  287.  
  288.   ////////////////////////////////////////////////////////
  289.  
  290.   //Setting Green filtered photodiodes to be read
  291.  
  292.   digitalWrite(S22, HIGH);
  293.   digitalWrite(S23, HIGH);
  294.  
  295.   //Reading the output frequency
  296.   frequency2 = pulseIn(sensorOut2, LOW);
  297.   int G2 = frequency2;
  298.  
  299.   //Printing the value on the serial monitor
  300.   Serial.print("G2= ");  //printing name
  301.   Serial.print(frequency2);  //printing RED color frequency
  302.   Serial.print("  ");
  303.   delay(50);
  304.  
  305.   ////////////////////////////////////////////////////////
  306.  
  307.   //Setting Blue filtered photodiodes to be read
  308.  
  309.   digitalWrite(S22, LOW);
  310.   digitalWrite(S23, HIGH);
  311.  
  312.   //Reading the output frequency
  313.   frequency2 = pulseIn(sensorOut2, LOW);
  314.   int B2 = frequency2;
  315.  
  316.   //Printing the value on the serial monitor
  317.   Serial.print("B2= ");  //printing name
  318.   Serial.print(frequency2);  //printing RED color frequency
  319.   Serial.println("  ");
  320.   delay(50);
  321.  
  322.   ////////////////////////////////////////////////////////
  323.  
  324.   if (R2 < 57 && R2 > 10 && G2 < 130 && G2 > 40 && B2 < 89 && B2 > 30  ) {
  325.     color2 = 1; // Red ✔[19-2-2022]
  326.     Serial.println("\t\t\tRED detected! 2");
  327.   }
  328.   else if (R2 < 150 && R2 > 75 && G2 < 175 && G2 > 90 && B2 < 155 && B2 > 80  ) {
  329.     color2 = 2; // Black ✔✔✔[14-2-2022]
  330.     Serial.println("\t\t\tBLUE detected! 2");
  331.   }
  332.   else if (R2 < 130 && R2 > 30 && G2 < 79 && G2 > 20 && B2 < 81 && B2 > 25  ) {
  333.     color2 = 3; // Green ✔[19-2-2022]
  334.     Serial.println("\t\t\tGREEN detected! 2");
  335.   }
  336.   else {
  337.     color2 = 0;
  338.   }
  339.   return color2;
  340. }
  341. ///////////////////////////////////////////////////////////////////////////////////////////////
  342.  
  343. /*
  344.   void followLine() {
  345.  
  346.   //If none of the sensors detects black line, then go straight
  347.   if (rightIRSensorValue == LOW && leftIRSensorValue == LOW )
  348.   {
  349.     rotateMotor(MOTOR_SPEED, MOTOR_SPEED);
  350.   }
  351.   //If right sensor detects black line, then turn right
  352.   else if (rightIRSensorValue == HIGH && leftIRSensorValue == LOW )
  353.   {
  354.     rotateMotor(-MOTOR_SPEED, MOTOR_SPEED);
  355.   }
  356.   //If left sensor detects black line, then turn left
  357.   else if (rightIRSensorValue == LOW && leftIRSensorValue == HIGH )
  358.   {
  359.     rotateMotor(MOTOR_SPEED, -MOTOR_SPEED);
  360.   }
  361.   //If both the sensors detect black line, then stop
  362.   else
  363.   {
  364.     rotateMotor(0, 0);
  365.   }
  366.  
  367.   }
  368. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement