int led1 = 13;
int led2 = 12;
int led3 = 11;
int sw1 = 2;
int sw2 = 3;
bool sw_state1=0,last_sw_state1,buttonstate1=1,change=0;
bool sw_state2=0,last_sw_state2,buttonstate2=1,change2=0;
bool state1=1,state2=1;
unsigned long time1, time2;
int debounceDelay=40;
bool LED1State=0;
unsigned long delayTime1;
unsigned long delayA0;
void setup()
{
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(sw1,INPUT);
pinMode(sw2,INPUT);
Serial.begin(115200);
}
void checkSw1() {
sw_state1 = digitalRead(sw1);
if (sw_state1 != last_sw_state1) {
time1 = millis();
}
if ((millis() - time1) > debounceDelay) {
if (buttonstate1 != sw_state1) {
buttonstate1 = sw_state1;
change = 1;
}
}
if ((buttonstate1 == 0) && (change == 1)) {
state1 = !state1;
change = 0;
}
if ((buttonstate1 == 1) && (change == 1)) {
change = 0;
}
last_sw_state1 = sw_state1;
}
void checkSw2() {
sw_state2 = digitalRead(sw2);
if (sw_state2 != last_sw_state2) {
time2 = millis(); // Store the current time for switch 2
}
if ((millis() - time2) > debounceDelay) {
if (buttonstate2 != sw_state2) {
buttonstate2 = sw_state2;
change2 = 1; // Use change2 for switch 2
}
}
if ((buttonstate2 == 0) && (change2 == 1)) {
state2 = !state2;
change2 = 0;
}
if ((buttonstate2 == 1) && (change2 == 1)) {
change2 = 0;
}
last_sw_state2 = sw_state2;
}
void loop()
{
checkSw1();
if(state1==0){ digitalWrite(led2,LOW);}
else{ digitalWrite(led2,HIGH);}
checkSw2();
if(state2==0){ digitalWrite(led3,LOW);}
else{ digitalWrite(led3,HIGH);}
if ((millis() - delayTime1) <= 1000)
{
digitalWrite(led1, LED1State);
}
else
digitalWrite(led1,false);
if (millis() - delayTime1 > 2000)
{
delayTime1 = millis();
LED1State = !LED1State;
}
if ((millis() - delayA0) > 500)
{
int sensorValue = analogRead(A0);
float volt = sensorValue*5.0/1024;
int outputPWM = map(sensorValue,0,1023,0,255);
analogWrite(6,outputPWM);
Serial.print("ADC:");
Serial.print(sensorValue);
Serial.print(" volt:");
Serial.println(volt);
delayA0=millis();
}
}