Created
October 22, 2023 17:53
-
-
Save MartinRGB/2136a29c4d760df2883c4f6da6ca87b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "SevSeg.h" | |
| SevSeg sevseg; //Initiate a seven segment controller object | |
| // constants won't change. They're used here to set pin numbers: | |
| const int SENSOR_PIN = A4; // the Arduino's input pin that connects to the sensor's SIGNAL pin | |
| const int LED_PIN = A5; | |
| const int FAN_PIN = A0; | |
| const int VOLTAGE_PIN = A2; | |
| const int BUTTON_PIN = 1; | |
| #define MQ2pin (A1) | |
| // Variables will change: | |
| int lastState = LOW; // the previous state from the input pin | |
| int currentState; // the current reading from the input pin | |
| int brightness = 0; | |
| int fadeAmount = 25; | |
| int btnCount = 0; | |
| int btnCurrentState; | |
| int btnLastState = LOW; | |
| int btnLedState = LOW; | |
| float sensorValue; //variable to store sensor value | |
| float voltageSensorVal; | |
| void setup() { | |
| // initialize serial communication at 9600 bits per second: | |
| Serial.begin(9600); | |
| // initialize the Arduino's pin as aninput | |
| pinMode(SENSOR_PIN, INPUT); | |
| pinMode(LED_PIN,OUTPUT); | |
| pinMode(FAN_PIN,OUTPUT); | |
| pinMode(BUTTON_PIN, INPUT); | |
| byte numDigits = 4; | |
| byte digitPins[] = {2, 3, 4, 5}; | |
| byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13}; | |
| bool resistorsOnSegments = true; | |
| // variable above indicates that 4 resistors were placed on the digit pins. | |
| // set variable to 1 if you want to use 8 resistors on the segment pins. | |
| sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins, resistorsOnSegments); | |
| sevseg.setBrightness(90); | |
| } | |
| void loop() { | |
| btnCurrentState = digitalRead(BUTTON_PIN); | |
| if (btnLastState == HIGH && btnCurrentState == LOW) { | |
| btnLedState = !btnLedState; | |
| if(btnCount < 2){ | |
| btnCount++; | |
| } | |
| else{ | |
| btnCount = 0; | |
| } | |
| Serial.println(btnCount); | |
| } | |
| btnLastState = btnCurrentState; | |
| if(btnCount == 2){ | |
| voltageSensorVal = analogRead(VOLTAGE_PIN); | |
| float Vout = (voltageSensorVal/40.92); | |
| Serial.print("Voltage = "); | |
| Serial.print(Vout); | |
| Serial.println("V"); | |
| sevseg.setNumber(Vout*100 , 2); | |
| sevseg.refreshDisplay(); // Must run repeatedly | |
| } | |
| if(btnCount == 1){ | |
| float gassensorValue = analogRead(MQ2pin); // read analog input pin 0 | |
| Serial.print("Sensor Value: "); | |
| Serial.print(gassensorValue); | |
| if(gassensorValue > 300) | |
| { | |
| Serial.print(" | Smoke detected!"); | |
| } | |
| sevseg.setNumber(gassensorValue*100 , 2); | |
| sevseg.refreshDisplay(); // Must run repeatedly | |
| } | |
| if(btnCount == 0){ | |
| currentState = digitalRead(SENSOR_PIN); | |
| analogWrite(LED_PIN, brightness); | |
| sevseg.setNumber(brightness , 0); | |
| sevseg.refreshDisplay(); // Must run repeatedly | |
| analogWrite(FAN_PIN,brightness); | |
| if(lastState == LOW && currentState == HIGH){ | |
| Serial.println("The sensor is touched"); | |
| // change the brightness for next time through the loop: | |
| brightness = brightness + fadeAmount; | |
| // reverse the direction of the fading at the ends of the fade: | |
| if (brightness <= 0 || brightness >= 250) { | |
| fadeAmount = -fadeAmount; | |
| } | |
| } | |
| // save the the last state | |
| lastState = currentState; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment