Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- //#include <stdio.h>
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- int relay = 8;
- int relay2 = 9;
- int buzzer = 10;
- int uvSensor = A0;
- String str;
- int blueToothVal;
- int sensorValue = 0;
- float Vout = 0;
- float uvIntensity = 0;
- int timer_started_flag = 0;
- long int total_Seconds = 0;
- // Tracks the time since last event fired
- unsigned long int previoussecs = 0;
- unsigned long int currentsecs = 0;
- unsigned long currentMillis = 0;
- int interval = 1 ; // updated every 1 second
- void setup()
- {
- Serial.begin(9600);
- pinMode(relay, OUTPUT);
- pinMode(relay2, OUTPUT);
- pinMode(buzzer, OUTPUT);
- pinMode(uvSensor,INPUT);
- digitalWrite(relay, HIGH);// relay is OFF
- digitalWrite(relay2, HIGH);// relay is OFF
- digitalWrite(buzzer, LOW);
- lcd.init();
- lcd.init();
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print("UVC Kit");
- lcd.setCursor(0, 1);
- lcd.print("Intensity Sensor");
- }
- void loop()
- {
- bluetooth();
- countdown();
- }
- void bluetooth()
- {
- while (Serial.available() && timer_started_flag == 0)
- {
- {
- str = Serial.readStringUntil('\n');
- }
- blueToothVal = (str.toInt());
- if (blueToothVal > 30) {
- blueToothVal = 30;
- }
- total_Seconds = blueToothVal * 60;// convert to sec
- timer_started_flag = 1;
- //UVsensor(); // only take 1 reading when recieved order from bluetooth
- //Serial.write(abs(uvIntensity));
- lcd.clear();
- lcd.print("T Seconds:");
- lcd.setCursor(11, 0);
- lcd.print( total_Seconds );
- delay(1000);
- }
- }
- void countdown() {
- while (timer_started_flag == 1) {
- UVsensor();
- lcd.setCursor(0, 0);
- lcd.print("T Seconds:");
- lcd.setCursor(11, 0);
- lcd.print( total_Seconds );
- lcd.setCursor(0, 1);
- if ( total_Seconds > 0)
- {
- digitalWrite(relay, LOW);// turn ON relay 1
- digitalWrite(relay2, LOW);// turn ON relay 2
- //lcd.print("load ON ");
- lcd.print(abs(uvIntensity));
- }
- if ( total_Seconds <= 0)
- {
- total_Seconds = 0;
- timer_started_flag = 0;
- digitalWrite(relay, HIGH);// turn OFF Relay 1
- digitalWrite(relay2, HIGH);// turn OFF Relay 2
- //lcd.print("load OFF");
- lcd.print(abs(uvIntensity));
- //------------------------------Buzzer Tone
- digitalWrite(buzzer, HIGH);
- delay(500);
- digitalWrite(buzzer, LOW);
- delay(500);
- digitalWrite(buzzer, HIGH);
- delay(1500);
- digitalWrite(buzzer, LOW);
- delay(500);
- /////////////////////////////////////////////
- }
- currentMillis = millis();
- currentsecs = currentMillis / 1000;
- if ((unsigned long)(currentsecs - previoussecs) >= interval) {
- total_Seconds = total_Seconds - 1;
- lcd.clear();
- previoussecs = currentsecs;
- }
- }//end of while loop
- }
- void UVsensor(){
- int uvLevel = analogRead(uvSensor);//averageAnalogRead
- float outputVoltage = (( uvLevel * 5000 ) / 1023.0); //0-1023 ---> [x] mV
- float index = mapfloat(outputVoltage, 0.0, 1000, 0.0, 11.0);// UV index
- uvIntensity = mapfloat(outputVoltage/10, 1, 2.8, 0.0, 15.0); //Convert the voltage to a UV intensity level
- }
- float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
- {
- if (x == 0) {
- return 0;
- }
- //mapfloat(outputVoltage/1000.0, 1, 2.8, 0.0, 15.0);
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement