Advertisement
pleasedontcode

**Sensor Control** rev_01

May 11th, 2025
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Sensor Control**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-05-11 19:33:21
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* find an obstacle by using ultrasonic sensor which */
  21.     /* is placed on servomotor and go in front of */
  22.     /* obstacle at distance of 5 cm */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
  29. #include <Servo.h>  //https://github.com/arduino-libraries/Servo
  30.  
  31. // ----- COLOR SENSOR DEFINITIONS -----
  32. #define S0 6
  33. #define S1 7
  34. #define S2 8
  35. #define S3 9
  36. #define OUT 10
  37.  
  38. // ----- MOTOR PIN DEFINITIONS -----
  39. #define ML_Ctrl 13
  40. #define ML_PWM 11
  41. #define MR_Ctrl 12
  42. #define MR_PWM 3
  43.  
  44. int redFreq, greenFreq, blueFreq;
  45.  
  46. // ----- ULTRASONIC SENSOR DEFINITIONS -----
  47. Ultrasonic ultrasonic(2, 3); // Trigger pin 2, Echo pin 3
  48. const int desiredDistance = 5; // Desired distance in cm
  49.  
  50. /****** FUNCTION PROTOTYPES *****/
  51. void setup(void);
  52. void loop(void);
  53. void findObstacleAndMove(void);
  54.  
  55. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  56. const uint8_t ultrasonic_HC-SR04_Echo_PIN_D3        = 3; // Note: This conflicts with MR_PWM
  57.  
  58. // ***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  59. const uint8_t myLED_LED_PIN_D4      = 4;
  60.  
  61. // ***** DEFINITION OF PWM OUTPUT PINS *****/
  62. const uint8_t servomotor_Servomotor_PWMSignal_PIN_D5        = 5; // No conflict
  63.  
  64. // ***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  65. /***** used to store raw data *****/
  66. bool    ultrasonic_HC-SR04_Trigger_PIN_D2_rawData       = 0;
  67. bool    myLED_LED_PIN_D4_rawData        = 0;
  68. uint8_t servomotor_Servomotor_PWMSignal_PIN_D5_rawData      = 0;
  69.  
  70. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  71. /***** used to store data after characteristic curve transformation *****/
  72. float   ultrasonic_HC-SR04_Trigger_PIN_D2_phyData       = 0.0;
  73. float   myLED_LED_PIN_D4_phyData        = 0.0;
  74. float   servomotor_Servomotor_PWMSignal_PIN_D5_phyData      = 0.0;
  75.  
  76. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  77.  
  78. void setup(void)
  79. {
  80.     // put your setup code here, to run once:
  81.  
  82.     pinMode(ultrasonic_HC-SR04_Echo_PIN_D3, INPUT);
  83.     pinMode(myLED_LED_PIN_D4,    OUTPUT);
  84.     pinMode(servomotor_Servomotor_PWMSignal_PIN_D5,  OUTPUT);
  85.  
  86.     // TCS3200 Color Sensor Setup
  87.     pinMode(S0, OUTPUT);
  88.     pinMode(S1, OUTPUT);
  89.     pinMode(S2, OUTPUT);
  90.     pinMode(S3, OUTPUT);
  91.     pinMode(OUT, INPUT);
  92.     digitalWrite(S0, HIGH);
  93.     digitalWrite(S1, LOW);
  94.  
  95.     // Motor Setup
  96.     pinMode(ML_Ctrl, OUTPUT);
  97.     pinMode(ML_PWM, OUTPUT);
  98.     pinMode(MR_Ctrl, OUTPUT);
  99.     pinMode(MR_PWM, OUTPUT);
  100.  
  101.     stopMotors(); // Initialize motors to stop
  102. }
  103.  
  104. void loop(void)
  105. {
  106.     // put your main code here, to run repeatedly:
  107.     updateOutputs(); // Refresh output data
  108.     readColor(); // Read color sensor data
  109.     Serial.print("R: "); Serial.print(redFreq);
  110.     Serial.print(" G: "); Serial.print(greenFreq);
  111.     Serial.print(" B: "); Serial.println(blueFreq);
  112.  
  113.     findObstacleAndMove(); // Check for obstacles and move accordingly
  114.  
  115.     delay(1000);
  116. }
  117.  
  118. void findObstacleAndMove() {
  119.     int distance = ultrasonic.read(); // Read distance from ultrasonic sensor
  120.     if (distance < desiredDistance) {
  121.         Serial.println("Obstacle detected! Moving back...");
  122.         stopMotors();
  123.         delay(1000); // Pause before moving
  124.         moveForwardDistance(5); // Move back 5 cm
  125.     } else {
  126.         Serial.println("No obstacle detected. Moving forward.");
  127.         moveForward(); // Continue moving forward
  128.     }
  129. }
  130.  
  131. void updateOutputs()
  132. {
  133.     digitalWrite(ultrasonic_HC-SR04_Trigger_PIN_D2, ultrasonic_HC-SR04_Trigger_PIN_D2_rawData);
  134.     digitalWrite(myLED_LED_PIN_D4, myLED_LED_PIN_D4_rawData);
  135.     analogWrite(servomotor_Servomotor_PWMSignal_PIN_D5, servomotor_Servomotor_PWMSignal_PIN_D5_rawData);
  136. }
  137.  
  138. // ----- COLOR SENSOR FUNCTIONS -----
  139. void readColor() {
  140.     digitalWrite(S2, LOW);
  141.     digitalWrite(S3, LOW);
  142.     redFreq = pulseIn(OUT, LOW);
  143.  
  144.     digitalWrite(S2, HIGH);
  145.     digitalWrite(S3, HIGH);
  146.     greenFreq = pulseIn(OUT, LOW);
  147.  
  148.     digitalWrite(S2, LOW);
  149.     digitalWrite(S3, HIGH);
  150.     blueFreq = pulseIn(OUT, LOW);
  151. }
  152.  
  153. bool isRed() {
  154.     return redFreq < greenFreq * 0.6 && redFreq < blueFreq * 0.6;
  155. }
  156.  
  157. bool isYellow() {
  158.     return redFreq < greenFreq * 1.2 && redFreq > greenFreq * 0.8 &&
  159.            blueFreq > redFreq * 1.5;
  160. }
  161.  
  162. bool isGreen() {
  163.     return greenFreq < redFreq * 0.7 && greenFreq < blueFreq * 0.7;
  164. }
  165.  
  166. // ----- MOTOR CONTROL FUNCTIONS -----
  167. void moveForward() {
  168.     digitalWrite(ML_Ctrl, HIGH);
  169.     analogWrite(ML_PWM, 150);
  170.  
  171.     digitalWrite(MR_Ctrl, HIGH);
  172.     analogWrite(MR_PWM, 150);
  173. }
  174.  
  175. void stopMotors() {
  176.     analogWrite(ML_PWM, 0);
  177.     analogWrite(MR_PWM, 0);
  178. }
  179.  
  180. void turnRight90() {
  181.     digitalWrite(ML_Ctrl, HIGH);
  182.     analogWrite(ML_PWM, 150);
  183.  
  184.     digitalWrite(MR_Ctrl, LOW);
  185.     analogWrite(MR_PWM, 150);
  186.  
  187.     delay(600);  // Adjust for 90° turn
  188.     stopMotors();
  189. }
  190.  
  191. void turn180() {
  192.     digitalWrite(ML_Ctrl, HIGH);
  193.     analogWrite(ML_PWM, 150);
  194.  
  195.     digitalWrite(MR_Ctrl, LOW);
  196.     analogWrite(MR_PWM, 150);
  197.  
  198.     delay(1100);  // Adjust for 180° turn
  199.     stopMotors();
  200. }
  201.  
  202. void moveForwardDistance(int cm) {
  203.     // 150 PWM ≈ 1 cm = ~60 ms (depends on tank)
  204.     int ms = cm * 60;
  205.     moveForward();
  206.     delay(ms);
  207.     stopMotors();
  208. }
  209.  
  210. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement