Processing ile Rgb Led Kontrolü

0 322

Rgb led ismini baş harflerinden (red, greer, blue) gelen ana renklerden alır.Bu renkler karıştırılarak diğer ara renkler oluşur.Çalışma mantığı bu şekildedir.4 tane bacak bulunur.Bu bacaklardan 3 tanesi kırmızı, yeşil ve mavi renk bacakları diğer bacak ise ortak anot ise artı ortak katot ise eksi bacaktır.Bu projemizde processing ile rgb led kontrolü programdan kontrol edip istediğimiz rengi almamızı sağlayacağız.

Malzemeler:

  1. Arduino
  2. Rgb led
  3. Direnç (100×2 ,180×1)

Elektronik Kısım:

 

Arduino Rgb Kod

int Red = 6;
 int Green = 5;
 int Blue = 3;
 int r = 125;
 int g = 125;
 int b = 125;
 
void setup()
 {
 Serial.begin(9600);
 pinMode(Red,OUTPUT);
 pinMode(Green,OUTPUT);
 pinMode(Blue,OUTPUT);
 }
 void loop()
 {
 if(Serial.available()==9)
 {
 r= (Serial.read()-48)*100;
 r=r + (Serial.read()-48)*10;
 r=r + Serial.read()-48;
 g= (Serial.read()-48)*100;
 g=g+ (Serial.read()-48)*10;
 g=g + Serial.read()-48;
 b= (Serial.read()-48)*100 ;
 b=b+ (Serial.read()-48)*10;
 b=b + Serial.read()-48;
 int d = Serial.read();
 }
 analogWrite(Red,r);
 analogWrite(Green,g);
 analogWrite(Blue,b);
 }
 

Processing Kod

 

import processing.serial.*;// import the serial library
 int r =125;
 int g =125;
 int b =125 ;
 int rold,gold,bold; // define all variables
 String oldout;
 Serial port; // create the serial object
 int count=0;
 void setup()
 {
 println(Serial.list()); // prints out the serial ports available
 port = new Serial(this,Serial.list()[1],9600);// change to correct com port(1) which is displayed by the previous line
 size(200,200); // size of the window
 background(200); // color of the backround
 smooth(); // makes the lines smooth
 frameRate(10); // change this to get better but slower results
 } // frame rate is normally 60/sec but thats too fast
 void draw()
 {
 int row = 1;
 background(200); // clear the display
 fill(r,g,b); // set the color to the mixed color
 rect(50,25,100,50); // box for the mixed color
 fill(255);
 for(int j=120; j<=200 ;j=j+40)// for loop to create the row of buttons
 {
 for(int i=25;i<=175;i=i+55)//for loop to create the column of buttons
 {
 rect(i,j,40,30);
 if(row%2 !=0) // if odd numbered row
 {
 line(i+10,j+20,i+20,j+10);// arrow up
 line(i+20,j+10,i+30,j+20);
 }
 else //if an even numbered row
 {
 line(i+10,j+10,i+20,j+20);// arrow down
 line(i+20,j+20,i+30,j+10);
 }
 }
 row++; // increment row
 }// 40 wide, 10 gap // reminded as to what the button sizes were
 // Text areas
 PFont font;
 font = loadFont("AngsanaNew-25.vlw"); // import the font(tools->create)
 textFont(font); // just copy the file name into the blue part
 fill(255,0,0); //text colour to red
 String s = nf(r,3); // nf turns an int into a string with 3 numbers before the decimal point
 text(s, 30, 100, 40, 20); // print the current level of red on the screen
 fill(0,255,0); // text colour to green
 String t = nf(g,3);
 text(t,85,100,40,20); // print the current level of red on the screen
 fill(0,0,255); // text colur to blue
 String u =nf(b,3);
 text(u,140,100,40,30); // print the current level of red on the screen
 //User input
 if(mousePressed) // if any button is pressed
 {
 if((mouseX> 25 )&& (mouseX< 65)) // if the button is in the first column
 {
 if((mouseY>120 )&&(mouseY<150)) // if the top button
 {
 if(r>=255) // if the value greater than 255, set to 255(8 bits),
 r=255;
 else
 r++;
 }
 if((mouseY>160 )&&(mouseY<190)) // if the bottom button
 {
 if(r<=0) // if less than 0, set to 0(nothing)
 r=0;
 else
 r--;
 }
 }
 if((mouseX> 80 )&& (mouseX< 120)) // if the button is in the first column
 {
 if((mouseY>120 )&&(mouseY<150)) // if the top buttom
 {
 if(g>=255) // if the value greater than 255, set to 255(8bit),
 g=255;
 else
 g++;
 }
 if((mouseY>160 )&&(mouseY<190)) // if the bottom button
 {
 if(g<=0) // if less than 0, set to 0(nothing)
 g=0;
 else
 g--;
 }
 }
 if((mouseX> 135 )&& (mouseX< 175)) // if the button is in the first column
 {
 if((mouseY>120 )&&(mouseY<150)) // if the top buttom
 {
 if(b>=255) // if the value greater than 255, set to 255(8bit),
 b=255;
 else
 b++;
 }
 if((mouseY>160 )&&(mouseY<190)) // if the bottom button
 {
 if(b<=0) // if less than 0, set to 0(nothing)
 b=0;
 else
 b--;
 }
 }
 }
 // String f = "here";// debugger
 // println(f);
 String out = nf(r,3)+ nf(g,3)+ nf(b,3);//creates the string that we will send to the Arduino
 //if (count%5==0) //if you want, you can only send ever xth output, just uncomment
 //{
 println(out); // print that string to the serial monitor
 port.write(out); // send that string to the serial port
 //}
 //count++;
 }
 

Arduino Lcd Kod

 

#include <LiquidCrystal.h>
 LiquidCrystal lcd(7,8,9,10,11,12);
 int Red = 6;
 int r = 125;
 int Green = 5;
 int g = 125;
 int Blue = 3;
 int b = 125;</pre>
void setup()
{
lcd.begin(16,2);
Serial.begin(9600);
pinMode(Red,OUTPUT);
pinMode(Green,OUTPUT);
pinMode(Blue,OUTPUT);
}
void loop()
{
if(Serial.available()==9)
{
r= (Serial.read()-48)*100;
r=r + (Serial.read()-48)*10;
r=r + Serial.read()-48;
g= (Serial.read()-48)*100;
g=g+ (Serial.read()-48)*10;
g=g + Serial.read()-48;
b= (Serial.read()-48)*100 ;
b=b+ (Serial.read()-48)*10;
b=b + Serial.read()-48;
int d = Serial.read();
//Serial.flush();
}
/*for(int y =0;y<10;y++)
{
int w=Serial.read();
lcd.print(w);
delay(1000);
}*/
//Serial.println(r + g + b);
analogWrite(Red,r);
analogWrite(Green,g);
analogWrite(Blue,b);
 
lcd.setCursor(0,0);
lcd.print("Red=");
lcd.setCursor(4,0);
lcd.print(r);
lcd.setCursor(7,0);
lcd.print("Green=");
lcd.setCursor(13,0);
lcd.print(g);
lcd.setCursor(0,1);
lcd.print("Blue=");
lcd.setCursor(5,1);
lcd.print(b);
// if(r<0)
// digitalWrite(13,HIGH);
// delay(200);
// digitalWrite(13,LOW);
}
//}//nedd to remove later
Bunlar da İlgini Çekebilir

Cevap bırakın

E-posta hesabınız yayımlanmayacak.