#define Adj_delay A0
#define Motor 2
#define UV_lamp 3
#define Photo_Sensor 4
#define Encoder 5
const int ledPin = 13;
#define OFF LOW
#define ON HIGH
int SensorState = 0;
int sensorValue = 0;
long outputValue = 0;
unsigned long Count = 0;
int buttonState;
int ledState = HIGH;
unsigned long duration;
int lamp_on =0;
long count_delay =0;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup(){
pinMode(Motor,OUTPUT);
pinMode(UV_lamp,OUTPUT);
pinMode(Photo_Sensor, INPUT_PULLUP);
pinMode(Encoder,INPUT_PULLUP);
digitalWrite(Motor, ON);
Serial.begin(9600);
}
void loop(){
SensorState = digitalRead(Photo_Sensor);
int reading = digitalRead(Photo_Sensor);
sensorValue = analogRead(Adj_delay);
outputValue = map(sensorValue, 0, 1023, 100, 3000);
//Serial.print(outputValue);
//Serial.println(" ms");
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == LOW) {
ledState = !ledState;
lamp_on =1;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
lastButtonState = reading;
duration = pulseIn(Encoder, HIGH, 50000);
if(duration>0){
if(lamp_on ==1)
{
digitalWrite(UV_lamp, ON);
delay(5);
count_delay++;
if(count_delay>=outputValue)
{
count_delay =0;
lamp_on =0;
}
}
else
{
digitalWrite(UV_lamp, OFF);
}
}
if(duration==0){
lamp_on =0;
digitalWrite(UV_lamp, OFF);
count_delay =0;
}
// float time=duration/100;
// Serial.print("DelT=");
// Serial.print(duration);
// Serial.println(" ms");
// Serial.print("sensor = ");
// Serial.println(sensorValue);
// Serial.println(buttonState);
// delay(100);
}
รีวิว
ยังไม่มีบทวิจารณ์