Advertisement
pleasedontcode

"Proximity-controlled Outputs" rev_01

Apr 9th, 2024
97
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: "Proximity-controlled Outputs"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-04-10 01:57:07
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* f no object is within range of the sensor, the LED */
  21.     /* is not lit.  • If an object is between 5 and 20 cm */
  22.     /* away from the sensor, the LED blinks 5 times per */
  23.     /* second.  • If an object is between 20 cm and 30 cm */
  24.     /* away from the sensor, the LED is lit */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* f no object is within range of the sensor, the LED */
  27.     /* is not lit.  • If an object is between 5 and 20 cm */
  28.     /* away from the sensor, the LED blinks 5 times per */
  29.     /* second.  • If an object is between 20 cm and 30 cm */
  30.     /* away from the sensor, the LED is lit */
  31. /****** END SYSTEM REQUIREMENTS *****/
  32.  
  33. /****** DEFINITION OF LIBRARIES *****/
  34. #include <Wire.h>
  35. #include <IRSense.h>
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40. void updateOutputs(void);
  41.  
  42. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  43. const uint8_t IRsensor_LED_PIN_D2 = 2;
  44. const uint8_t led_LED_PIN_D3 = 3;
  45.  
  46. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  47. /***** used to store raw data *****/
  48. bool IRsensor_LED_PIN_D2_rawData = 0;
  49. bool led_LED_PIN_D3_rawData = 0;
  50.  
  51. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  52. /***** used to store data after characteristic curve transformation *****/
  53. float IRsensor_LED_PIN_D2_phyData = 0.0;
  54. float led_LED_PIN_D3_phyData = 0.0;
  55.  
  56. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  57. IRSense sensor1;
  58.  
  59. void setup(void)
  60. {
  61.   // put your setup code here, to run once:
  62.   Wire.begin();
  63.   sensor1.begin(Wire);
  64.  
  65.   pinMode(IRsensor_LED_PIN_D2, OUTPUT);
  66.   pinMode(led_LED_PIN_D3, OUTPUT);
  67. }
  68.  
  69. void loop(void)
  70. {
  71.   // put your main code here, to run repeatedly:
  72.   updateOutputs(); // Refresh output data
  73.   delay(200); // Delay for 200ms
  74. }
  75.  
  76. void updateOutputs(void)
  77. {
  78.   int proximity = sensor1.readProximity();
  79.  
  80.   if (proximity >= 500 && proximity <= 2000) // Object is between 5cm and 20cm
  81.   {
  82.     IRsensor_LED_PIN_D2_rawData = !IRsensor_LED_PIN_D2_rawData; // Toggle the LED state at 5 times per second
  83.   }
  84.   else if (proximity > 2000 && proximity <= 3000) // Object is between 20cm and 30cm
  85.   {
  86.     IRsensor_LED_PIN_D2_rawData = HIGH; // Turn on the LED
  87.   }
  88.   else
  89.   {
  90.     IRsensor_LED_PIN_D2_rawData = LOW; // Turn off the LED
  91.   }
  92.  
  93.   digitalWrite(IRsensor_LED_PIN_D2, IRsensor_LED_PIN_D2_rawData);
  94.   digitalWrite(led_LED_PIN_D3, led_LED_PIN_D3_rawData);
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement