14 Mayıs 2015 Perşembe

BUTONLA LED KONTROLÜ



KODU:

int sensorPin = 0;   
const int button2Pin = 2;//Buton u 2. pine bağla
const int button3Pin = 3;//Buton u 3. pine bağla
const int button4Pin = 6;//Buton u 6. pine bağla
int ledd1 = 11;// 1. Led i 2. pine bağla
int ledd2 = 10;//2. Led i 2. pine bağla
int ledd3 = 5;// 3. Led i 2. pine bağla

void setup()      //Setup() ın içinde bağladığımız pinlerin modunu belirliyoruz
{
  pinMode(button2Pin, INPUT);// Butonlardan input alacağimız için butonları input olarak ayarladık
  pinMode(button3Pin, INPUT);
  pinMode(button4Pin, INPUT);
  pinMode(ledd1, OUTPUT);// Led lerden output alacağimız için butonları output olarak ayarladık
  pinMode(ledd2, OUTPUT);
  pinMode(ledd3, OUTPUT);
}
void loop() 
{
  int button2State;int button3State;int button4State;
  int sensorValue;
  button2State = digitalRead(button2Pin);//Butona basılıp basılmadığını anlamak için tutuğumuz 
   button3State = digitalRead(button3Pin);//değişkene digitalRead() le okuduğumuz değeri atıyoruz
    button4State = digitalRead(button4Pin);

// if statement larında butona basılıp basılmadığında yapılacak işlemleri if  lerde belirliyoruz


if (((button2State == LOW) && (button3State == HIGH))  
      && ((button4State == HIGH))) 
                                                      
  {
  sensorValue = analogRead(sensorPin); //Potansiyometreden ayarlanan değeri okuyoruz 0ile 1023                                                                      //arasında
   analogWrite(ledd1, sensorValue/4);   //sensorden okuduğumuz değer kadar led lerin parlaklığını                                                                      //ayarlıyoruz
  delay(sensorValue);                            // ayrıca sensorden okuduğumuz değer kadar bekletiyoruz        
  analogWrite(ledd1, 0);  
  delay(sensorValue);
  }
  else if(((button2State == HIGH) && (button3State == LOW))  
      && ((button4State == HIGH)) )
  {
   sensorValue = analogRead(sensorPin);
   analogWrite(ledd2, sensorValue/4); 
   delay(sensorValue);  
   analogWrite(ledd2, 0); 
   delay(sensorValue); 
  } 
else if(((button2State ==HIGH ) && (button3State == HIGH)) 
      && ((button4State == LOW)))
  {
   sensorValue = analogRead(sensorPin);
   analogWrite(ledd3, sensorValue/4); 
   delay(sensorValue);  
   analogWrite(ledd3, 0); 
   delay(sensorValue); 
  }   
}

12 Mayıs 2015 Salı

PHOTORESISTER İLE SERVO KONTROL



#include <Servo.h>
Servo servo;
int lightLevel;//Photoresister den okuduğumuz değeri tutmak için değişken tanımladık
const int sensorPin = 1; //Photoresister yi 1. pine bağla
const int RED_PIN = 6;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;

void setup() {
  servo.attach(9);//Servo yu 9. pin e bağla
pinMode(RED_PIN, OUTPUT);//RGB Led i output olarak belirledik
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}

void loop() {
lightLevel = analogRead(sensorPin);//1. Pin e bağladığımız Photoresister ün değerini lightLevel e                                                                  //atıyoruz
       if (lightLevel >310){//lightLevel 310 dan büyükse
         servo.write(0);        //Servo yu 0. dereceye döndür
         digitalWrite(RED_PIN, HIGH);//Servo 0. dereceydeyken yani hareket etmezken
                                                             //RGB led in 6.pine bağı olan ayağını aktif ediyoruz
         digitalWrite(GREEN_PIN, LOW);//RGB led in diğer pinerini LOW yapıyoruz
         digitalWrite(BLUE_PIN, LOW);
         delay(100);
         servo.write(0);
         digitalWrite(RED_PIN, LOW);
         digitalWrite(GREEN_PIN, LOW);
         digitalWrite(BLUE_PIN, LOW);
         delay(100);
         }
     else if (lightLevel <200){////lightLevel 200 den küçükse
         servo.write(180);          //Servo yu 180. dereceye döndür
         digitalWrite(RED_PIN, LOW);
         digitalWrite(GREEN_PIN, HIGH);//RGB led den yeşil ışık yanması için GREEN_PIN i aktif                                                                    //yap
         digitalWrite(BLUE_PIN, LOW);
         delay(100);      //Delay ın içine yazdığımız süre boyunca beklettikten sonra
         servo.write(0);  //Servo yu 0. dereceye döndür
         digitalWrite(RED_PIN, LOW);
         digitalWrite(GREEN_PIN, LOW);
         digitalWrite(BLUE_PIN, LOW);
         delay(100);
         }
       
         }

2 Mayıs 2015 Cumartesi

BUTON, POTANSİYOMETRE VE LEDLER






                                                                            

                                                                            
KODU:

int sensorPin = 0;  //Potansiyometreyi A0 a bağla
const int button2Pin = 3;//Butonu 3. Pin e bağla
int ledPin = 13;// Led leri bağladığımız pin leri ayarlıyoruz
int ledd = 12;

void setup()
{
  pinMode(button2Pin, INPUT);//Butonu input olarak tanıttık
  pinMode(ledPin, OUTPUT);//LEdleri output oarak tanımladık
  pinMode(ledd, OUTPUT);
}
void loop()
{
  int buttonState;
  int sensorValue;
  buttonState = digitalRead(button2Pin);//Butona basılıp basılmadığını buttonState değişkenine                                                                                 //atıyoruz

if (buttonState == LOW)
  {
  sensorValue = analogRead(sensorPin);  //Potansiyometre nin değerini oku
  digitalWrite(ledPin, HIGH);    //mavi led i yak
  delay(sensorValue);            //Potansiyometreden okuduğumuz değer kadar bekle
  digitalWrite(ledPin, LOW);//mavi ledi yakma
  delay(sensorValue);  //Potansiyometreden okuduğumuz değer kadar bekle
  }
  else
  {
   sensorValue = analogRead(sensorPin);//Potansiyometre nin değerini oku
   digitalWrite(ledd, HIGH);//kırmızı Led i yak
   delay(sensorValue);//Potansiyometreden okuduğumuz değer kadar bekle
   digitalWrite(ledd, LOW);
   
 delay(sensorValue);
  }
}

BUTON VE SERVO DENEYİ

KODU:

#include <Servo.h>
  Servo servo;
  const int button2Pin = 3; //Butonu 3. pine bağla
  void setup() {
    servo.attach(9);//Servo yu 9.pine bağla
    pinMode(button2Pin, INPUT);//butonu input olarak belirle
  }

       
void loop() {
int button2State;
button2State = digitalRead(button2Pin);
 if (button2State == LOW){
     servo.write(90);    // Servo yu 90 dereceye döndür

delay(100);         // Delay ' da belirlediğimiz süre işlemi duruduruz

servo.write(180);   // Servo yu 180 dereceye çevir

delay(100);         // Delay ' da belirlediğimiz süre işlemi duruduruz

servo.write(0);     // Servo yu 0 dereceye döndür

delay(100);

 }

if (button2State == HIGH){// Buton a basılmadığında servoyu hareket ettirme
  delay(100);      

servo.write(0);   // Servo yu 0 dereceye döndür

delay(100);
  }}

TIMER VE LCD DENEYİ ------ BUZZER DENEYİ ----- TEMPERATURE VE PHOTORESİSTER İLE RGB LED KONTROLÜ




TIMER VE LCD DENEYİ






KODU:


#include <TimerOne.h>//Timer ı kullanmak için bu kütüphaneyi projemize ekliyoruz
#include <LiquidCrystal.h>//LCD yi  kullanmak için bu kütüphaneyi projemize ekliyoruz

LiquidCrystal lcd(12,11,5,4,3,2);//LCD için pinleri belirliyoruz
const int temperaturePin = 0;  //temperature sensorü A0a bağlıyoruz


void setup()
{
lcd.begin(16, 2);
lcd.clear();
lcd.print("hello, world!");//LCD ye ilk basılan değer
Timer1.initialize(8000000); // Timer ın  lengthi 800000 microseconds (or 0.1 sec - or 10Hz => printa() da yaptığımız olayı 800000 microseconds da bir tekrarlıyacak
  Timer1.attachInterrupt( printa ); //Timer a pinta() methodunu attach ediyoruz
}


void loop()
{
   
}

void printa(){//Bu methodu timer a yazdığınız değer sürede tekrarlarız
  lcd.setCursor(0,1);
  float voltage, degreesC, degreesF;
  voltage = getVoltage(temperaturePin);//temperaturePin den okuduğumuz değeri LCD ye yazmak                                                                      //için temperature ın değerini alıyoruz
  degreesC = (voltage - 0.5) * 100.0;  //temperature ın değerini C' olarak LCD ye bas
  degreesF = degreesC * (9.0/5.0) + 32.0;
  lcd.print(degreesC);
 }

float getVoltage(int pin)
{
  
  return (analogRead(pin) * 0.004882814);

}


BUZZER DENEYİ












KODU:

int sensorPin = 0;
int button1Pin = 2;
int button2Pin = 3;
const int buzzerPin = 9;
const int songLength = 18;
char notes[] = "cdfda ag cdfdg gf ";
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
int tempo;
int count = 0;
char notes_2[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b' };
void setup() {
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int button1State, button2State;
  tempo = analogRead(sensorPin)/4;
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
  if(button1State == LOW)
  {
    tone(buzzerPin, frequency(notes_2[count]), 100);
    count++;
    count %= 7;
    delay(500);
  }
  else if(button2State == LOW )
  {
    play_song();
    delay(100);
    }
  Serial.println(analogRead(sensorPin));
}
void play_song()
{
    int i, duration;

  for (i = 0; i < songLength; i++) // step through the song arrays
  {
    duration = beats[i] * tempo;  // length of note/rest in ms

    if (notes[i] == ' ')          // is this a rest?
    {
      delay(duration);            // then pause for a moment
    }
    else                          // otherwise, play the note
    {
      tone(buzzerPin, frequency(notes[i]), duration);
      delay(duration);            // wait for tone to finish
    }
    delay(tempo/10);              // brief pause between notes
  }
}
int frequency(char note)
{
  // This function takes a note character (a-g), and returns the
  // corresponding frequency in Hz for the tone() function.

  int i;
  const int numNotes = 8;  // number of notes we're storing



  // For the "char" (character) type, we put single characters
  // in single quotes.

  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};

  // Now we'll search through the letters in the array, and if
  // we find it, we'll return the frequency for that note.

  for (i = 0; i < numNotes; i++)  // Step through the notes
  {
    if (names[i] == note)         // Is this the one?
    {
      return(frequencies[i]);     // Yes! Return the frequency
    }
  }
  return(0);  // We looked through everything and didn't find it,
              // but we still need to return a value, so return 0.
}



TEMPERATURE VE PHOTORESİSTER İLE RGB LED KONTROLÜ





KODU:

const int RED_PIN = 9;//RGB ledin pinlerini set ediyoruz
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;//RGB led 9, 10, 11. Pinleri kullanıyor output göndermek için
const int sensorPin = 0;//Photoresister ü A0 a bağladık
const int sensorPin1= 1;//Temperature  sensör ü A1 e bağladık


int lightLevel, high = 0, low = 1023;//


void setup()
{
pinMode(RED_PIN, OUTPUT);//RGB ledi output olarak atadık
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
Serial.begin(9600);
}


void loop()
{


mainColors();// Bir döngü içinde süreki bu method çağırılacak



}


void mainColors()
{
   float voltage, C;//Temperature sensorden aldığımız değerleri tutmak için değişkenler tutuk


  voltage = getVoltage(sensorPin1);//Temperature sensörün değerini getVotage() methoduyla okuyoruz 
  lightLevel = analogRead(sensorPin);// Photoresistorün değerini okuyoruz
  manualTune(); 
  

  
  C = (voltage - 0.5) * 100.0;//Temperature  sensörden adıgımız değeri C' ye çeviryoruz
  if(C<=0){//Eğer Temperature sensörün çevirdiğimiz değeri 0 sa bu if koşulunu yapacağızz
    analogWrite(BLUE_PIN, lightLevel);//Blue rengin parlaklığını photo resistorden aldığımız değer kadar yapıyoruz
    digitalWrite(RED_PIN, LOW);
    digitalWrite(GREEN_PIN, LOW);
    digitalWrite(BLUE_PIN, HIGH);//ve sadece RGB ledin mavi rengini aktif yapıyoruz 
    delay(lightLevel);              //Işık şiddeti kadar bekledikten sonra RGB ledi Pinleri LOW yaparak söndürüyoruz
    digitalWrite(RED_PIN, LOW);
    digitalWrite(GREEN_PIN, LOW);
    digitalWrite(BLUE_PIN, LOW);
    delay(lightLevel);
    
    }
    if(0<C && C<1){// Temperature sensörün değeri 0-1 aralığındaysa bu koşolu yapacak program
      analogWrite(GREEN_PIN, lightLevel);//Green rengin parlaklığını photo resistorden aldığımız değer kadar yapıyoruz
      digitalWrite(RED_PIN, LOW);
    digitalWrite(GREEN_PIN, HIGH);//ve sadece RGB ledin yeşil rengini aktif yapıyoruz
    digitalWrite(BLUE_PIN, LOW);//Diğer renkleri LOW yaparak söndürüyoruz sadece yeşil yanması için 
  delay(lightLevel);              //Işık şiddeti kadar bekledikten sonra RGB ledi Pinleri LOW yaparak söndürüyoruz
  digitalWrite(RED_PIN, LOW);
    digitalWrite(GREEN_PIN, LOW);
    digitalWrite(BLUE_PIN, LOW);
    delay(lightLevel);            //Işık şiddeti kadar bekledikten sonra RGB ledi Pinleri LOW yaparak söndürüyoruz
  }
    if(C>=2){              // Temperature sensörün değeri 2 den büyükse  bu koşolu yapacak program
      analogWrite(RED_PIN, lightLevel);//RED rengin parlaklığını photo resistorden aldığımız değer kadar yapıyoruz
      digitalWrite(RED_PIN, HIGH);  //ve sadece RGB ledin RED rengini aktif yapıyoruz
    digitalWrite(GREEN_PIN, LOW);
    digitalWrite(BLUE_PIN, LOW);
    delay(lightLevel);          //Işık şiddeti kadar bekledikten sonra RGB ledi Pinleri LOW yaparak söndürüyoruz
    digitalWrite(RED_PIN, LOW);
    digitalWrite(GREEN_PIN, LOW);
    digitalWrite(BLUE_PIN, LOW);
    delay(lightLevel);
    }
    
   
      Serial.print(C);//Serial dan C değerini yazıp doğrumu ölçtük diye bakıyoruz
      Serial.println();
    
}
void manualTune()
{
  lightLevel = map(lightLevel, 500, 800, 0, 255);
  lightLevel = constrain(lightLevel, 0, 255);



float getVoltage(int pin)
{
  // BU FONKSİYONUN 1 PARAMETRESİ VAR ,ODA ANALOG PİN NUMARASI
  // BU PİNİ OKUMAK İÇİN BU PARAMETREYİ KULLANACAĞIZ. You might notice that this function does not have
  //  BU METHOD VOİD DEĞİL ÇÜNKÜ floating NUMBER DÖNDERECEK 
  // point DEĞERİ, DOĞRU VOLTAGE DEĞERİ (0 to 5V).


  return (analogRead(pin) * 0.004882814);

  // BU EŞİTLİK analogRead() DEN ALINAN  0 to 1023 DEĞERİNİ  ÇEVİRMEMİZİ SAĞLIYOR
  // returns,  0.0 DAN 5.0 E KADAR DOĞRU VOLTAGE OKUMAMIZ İÇİN
  // PİNDEN DOĞRU DEĞER OKUMAK İÇİN
}