// Dosya adı: LDR.ino
#include <PWM.h>//eklendi
int led = 9; // eklendi
int32_t frequency = 50; //eklendi frequency (in Hz)
int state = 0;
int old=0;
int buttonPoll=0;
int pwm_acik=0;
int isik=0;
int pwm_test=0;
int isik_esik=840;
#define esik_ayar A1
#define button 3
#define inPin0 A0 // giriş A0 pini
#define LED1 4
#define LED2 5
#define LED3 6
#define LED4 7
#define LED5 8
#define LED6 10
#define LED7 11
void setup() {
Serial.begin(9600);
InitTimersSafe();
//sets the frequency for the specified pin
bool success = SetPinFrequencySafe(led, frequency);
if (!success) {
Serial.println("Failed to set frequency!");
}
pinMode(led,OUTPUT);
pinMode (button,INPUT);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
pinMode (LED1,OUTPUT);
pinMode (LED2,OUTPUT);
pinMode (LED3,OUTPUT);
pinMode (LED4,OUTPUT);
pinMode (LED5,OUTPUT);
pinMode (LED6,OUTPUT);
pinMode (LED7,OUTPUT);
// fotodiyot için giriş pininde Pull_up etkinleştirdim.
// fotodiyotu pullup ile kullanmalısınız, Eğer okuduğunuz değer düşükse ve
// çözünürlük yetmiyorsa ADC için referansı değiştirebilirsiniz
// adc'den 50 okuduğunuza göre daha uygun olacaktır
pinMode (inPin0,INPUT);
analogReference(INTERNAL); //1.1V ADC referansı
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED6,LOW);
//GetPinResolution(uint8_t pin);
isik=analogRead(inPin0);
Serial.println(isik);
}
void loop() {
buttonPoll =digitalRead(button);
if (buttonPoll ==1){
delay(50);
buttonPoll=digitalRead(button);
if (buttonPoll ==0){
state =old+1;
}
}
else{
delay(100);
}
isik_esik=100+analogRead(esik_ayar)/4;
isik=analogRead(inPin0);
// aşağıdaki kod seri monitörden durumu okumak içindir
Serial.println("isik state pwm esik");
Serial.print(isik);Serial.print(" ");
Serial.print(state);Serial.print(" ");
Serial.print(pwm_test);Serial.print(" ");
Serial.println(isik_esik);
if(isik>=isik_esik){
pwm_acik=1;
}
else{
pwm_acik=0;
pwmWrite(led, 0);
}
switch (state){
case 1:
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED7,LOW);
if(pwm_acik)
{
pwm_test=26;
pwmWrite(led,pwm_test);
}
old=state;
break;
case 2:
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED7,LOW);
//pwmWrite(led, 44);
if(pwm_acik)
{
pwm_test=51;
pwmWrite(led, pwm_test);
}
old=state;
break;
case 3:
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED7,LOW);
//pwmWrite(led, 48);
if(pwm_acik)
{
pwm_test=76;
pwmWrite(led, pwm_test);
}
old=state;
break;
case 4:
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED7,LOW);
//pwmWrite(led, 52);
if(pwm_acik)
{
pwm_test=102;
pwmWrite(led, pwm_test);
}
old=state;
break;
case 5:
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,HIGH);
digitalWrite(LED6,LOW);
digitalWrite(LED7,LOW);
//pwmWrite(led, 58);
if(pwm_acik)
{
pwm_test=127;
pwmWrite(led, pwm_test);
}
old=state;
break;
case 6:
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,HIGH);
digitalWrite(LED7,LOW);
if(pwm_acik)
{
pwm_test=153;
pwmWrite(led, pwm_test);
}
old=state;
break;
case 7:
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED7,HIGH);
//pwmWrite(led, 78);
if(pwm_acik)
{
pwm_test=179;
pwmWrite(led, pwm_test);
}
old=state;
break;
default:
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED7,LOW);
pwm_test=0;
pwmWrite(led, pwm_test);
old=0;
break;
}
}