pleasedontcode

Sensor Controller rev_01

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