// Elektronik switch denemesi
#include <avr/sleep.h>
#include <avr/interrupt.h>
const int buton = 3; // Buton 3 numaralı digital girişte
const int LED = 2; // LED 2 numaralı digital çıkışta
const int out = 1; // MOSFET kontrol çıkışı 1 numaralı digital çıkışta
boolean durum = 0; // ON-OFF durumunu içeren değişken
unsigned long frame;
byte flag;
boolean ledstatus;
void setup()
{
pinMode(buton, INPUT_PULLUP);
pinMode(LED, OUTPUT);
noInterrupts();
GIMSK = B00100000; // Pin Change Interrupt Enable
PCMSK = B00001000; // PCI3 yani entegrenin 2 No'lu pininden PCI enable
interrupts();
ADCSRA &= ~_BV(ADEN); // ADC off
}
//void PCINT0_ISR()
ISR(PCINT0_vect)
{
}
void sleep()
{
GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts
PCMSK |= _BV(PCINT3); // Use PB3 as interrupt pin
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // replaces above statement
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sei(); // Enable interrupts
sleep_cpu(); // sleep
cli(); // Disable interrupts
PCMSK &= ~_BV(PCINT3); // Turn off PB3 as interrupt pin
sleep_disable(); // Clear SE bit
sei(); // Enable interrupts
}
void loop()
{
digitalWrite(LED, durum);
digitalWrite(out, durum);
flag = 0;
if(durum == 0) sleep();
if (digitalRead(buton) == LOW)
{
frame = millis();
flag = 1;
while ((millis() - frame) < 2500)
{
if (digitalRead(buton) == HIGH)
{
flag = 0;
frame = millis() - 3000;
ledstatus = 0;
}
digitalWrite(LED, ledstatus);
ledstatus = ! ledstatus;
delay(200);
}
if (flag == 1)
{
durum = !durum;
digitalWrite(LED, durum);
while (digitalRead(buton) == LOW)
{
}
}
}
}