pleasedontcode

Sensor Controller rev_02

Aug 4th, 2025
765
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 Controller
  13.     - Source Code compiled for: Arduino Opta WiFi
  14.     - Source Code created on: 2025-08-04 23:34:26
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a gas detection alert system on Arduino */
  21.     /* Opta WiFi, utilizing MQ3 sensor for alcohol */
  22.     /* detection and potentiometer for sensitivity */
  23.     /* adjustment, with RGB LED indicators for alert */
  24.     /* status. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /* START CODE */
  29. /****** DEFINITION OF LIBRARIES *****/
  30. // No external libraries are used in this project.
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void updateOutputs();
  36.  
  37. // ***** DEFINITION OF ANALOG INPUT PINS *****
  38. const uint8_t mq3_MQ3_AOUT_PIN_A0 = A0;
  39. const uint8_t pot_Potentiometer_Vout_PIN_A2 = A2;
  40.  
  41. // ***** DEFINITION OF DIGITAL OUTPUT PINS *****
  42. const uint8_t led_LEDRGB_Red_PIN_D0 = 0;
  43. const uint8_t led_LEDRGB_Green_PIN_D1 = 1;
  44. const uint8_t led_LEDRGB_Blue_PIN_D2 = 2;
  45.  
  46. // ***** DEFINITION OF OUTPUT RAW VARIABLES *****
  47. /***** used to store raw data *****/
  48. bool led_LEDRGB_Red_PIN_D0_rawData = 0;
  49. bool led_LEDRGB_Green_PIN_D1_rawData = 0;
  50. bool led_LEDRGB_Blue_PIN_D2_rawData = 0;
  51.  
  52. // ***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****
  53. /***** used to store data after characteristic curve transformation *****/
  54. float led_LEDRGB_Red_PIN_D0_phyData = 0.0;
  55. float led_LEDRGB_Green_PIN_D1_phyData = 0.0;
  56. float led_LEDRGB_Blue_PIN_D2_phyData = 0.0;
  57.  
  58. // Additional variables for gas detection
  59. float gasThreshold = 0.5; // Default threshold, can be adjusted via potentiometer
  60. float gasSensorValue = 0.0;
  61.  
  62. void setup(void)
  63. {
  64.   // Initialize serial communication for debugging
  65.   Serial.begin(9600);
  66.  
  67.   // Configure pins
  68.   pinMode(mq3_MQ3_AOUT_PIN_A0, INPUT);
  69.   pinMode(pot_Potentiometer_Vout_PIN_A2, INPUT);
  70.  
  71.   pinMode(led_LEDRGB_Red_PIN_D0, OUTPUT);
  72.   pinMode(led_LEDRGB_Green_PIN_D1, OUTPUT);
  73.   pinMode(led_LEDRGB_Blue_PIN_D2, OUTPUT);
  74. }
  75.  
  76. void loop(void)
  77. {
  78.   // Read the potentiometer for sensitivity adjustment
  79.   int potValue = analogRead(pot_Potentiometer_Vout_PIN_A2);
  80.   // Map potentiometer value (0-1023) to a threshold range (e.g., 0.1 to 1.0)
  81.   gasThreshold = map(potValue, 0, 1023, 10, 100) / 100.0;
  82.  
  83.   // Read the MQ3 sensor value
  84.   int sensorReading = analogRead(mq3_MQ3_AOUT_PIN_A0);
  85.   // Convert sensor reading to voltage (assuming 5V ADC reference)
  86.   float sensorVoltage = sensorReading * (5.0 / 1023.0);
  87.  
  88.   // For simplicity, normalize sensor voltage to a 0-1 range
  89.   gasSensorValue = sensorVoltage / 5.0;
  90.  
  91.   // Determine if gas level exceeds threshold
  92.   if (gasSensorValue >= gasThreshold)
  93.   {
  94.     // Gas detected - turn on red LED
  95.     led_LEDRGB_Red_PIN_D0_rawData = HIGH;
  96.     led_LEDRGB_Green_PIN_D1_rawData = LOW;
  97.     led_LEDRGB_Blue_PIN_D2_rawData = LOW;
  98.   }
  99.   else
  100.   {
  101.     // No gas detected - turn on green LED
  102.     led_LEDRGB_Red_PIN_D0_rawData = LOW;
  103.     led_LEDRGB_Green_PIN_D1_rawData = HIGH;
  104.     led_LEDRGB_Blue_PIN_D2_rawData = LOW;
  105.   }
  106.  
  107.   // Update physical data if needed (here we directly use rawData for simplicity)
  108.   updateOutputs();
  109.  
  110.   // Optional: print values for debugging
  111.   Serial.print("Gas Sensor Value: ");
  112.   Serial.print(gasSensorValue);
  113.   Serial.print(" Threshold: ");
  114.   Serial.println(gasThreshold);
  115.  
  116.   delay(200); // Delay for stability
  117. }
  118.  
  119. void updateOutputs()
  120. {
  121.   digitalWrite(led_LEDRGB_Red_PIN_D0, led_LEDRGB_Red_PIN_D0_rawData);
  122.   digitalWrite(led_LEDRGB_Green_PIN_D1, led_LEDRGB_Green_PIN_D1_rawData);
  123.   digitalWrite(led_LEDRGB_Blue_PIN_D2, led_LEDRGB_Blue_PIN_D2_rawData);
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment