PB -3- Arduino ile RGB LED kontrolü
PureBasic ile Arduino’ya bağlı RGB ledi (ortak anod) kontrol ediyoruz.. PWM ile led’e farklı voltajlar vererek istediğimiz renkte yanmasını sağlıyoruz.
Aşağıdaki kodu Arduino’ya yüklüyoruz..
#define kirmizi 9
#define yesil 10
#define mavi 11
void setup() {
Serial.begin(19200);
}
void loop() {
if (Serial.available() == 3)
{
analogWrite(kirmizi, 255 - Serial.read());
analogWrite(yesil, 255 - Serial.read());
analogWrite(mavi, 255 - Serial.read());
}
}
PB kodu da aşağıdaki gibi oldu.
EnableExplicit
Enumeration
#win_main
#txt_lbl1
#txt_lbl2
#txt_lbl3
#btn_connect
#btn_colorReq
#btn_red
#btn_yellow
#btn_orange
#btn_green
#btn_blue
#btn_purple
#btn_magenta
#btn_cyan
#btn_white
#btn_deepPink
#btn_brown
#btn_darkGreen
#cmb_comPort
#serial_portNr
#canvas
#track_r
#track_g
#track_b
#image_red
#image_yellow
#image_orange
#image_green
#image_blue
#image_purple
#image_magenta
#image_cyan
#image_white
#image_deepPink
#image_brown
#image_darkGreen
EndEnumeration
Global PortsFound = 0, Event, COMPort.s="", DataReceived.s, Color
Global StartTime, Timeout = 1000, Result, i, Lines, Dim RGB_Data.a(2)
Global *Buffer = AllocateMemory(128)
LoadFont(0, "Arial", 9) ; , #PB_Font_Bold)
SetGadgetFont(#PB_Default, FontID(0))
CreateImage(#image_red, DesktopScaledX(35), DesktopScaledY(35), 24, RGB(255,0,0))
CreateImage(#image_yellow, DesktopScaledX(35), DesktopScaledY(35), 24, RGB(255,255,0))
CreateImage(#image_orange, DesktopScaledX(35), DesktopScaledY(35), 24, RGB(255,165,0))
CreateImage(#image_green, DesktopScaledX(35), DesktopScaledY(35), 24, RGB(0,255,0))
CreateImage(#image_blue, DesktopScaledX(35), DesktopScaledY(35), 24, RGB(0,0,255))
CreateImage(#image_purple, DesktopScaledX(35), DesktopScaledY(35), 24, RGB(75,0,130)) ; mor
CreateImage(#image_magenta, DesktopScaledX(35), DesktopScaledY(35), 24, RGB(255,0,255)) ; magenta
CreateImage(#image_cyan, DesktopScaledX(35), DesktopScaledY(35), 24, RGB(0,255,255)) ; cyan
CreateImage(#image_white, DesktopScaledX(35), DesktopScaledY(35), 24, RGB(255,255,255)) ; white
CreateImage(#image_deepPink, DesktopScaledX(35), DesktopScaledY(35), 24, RGB(255,20,147)) ; deeppink
CreateImage(#image_brown, DesktopScaledX(35), DesktopScaledY(35), 24, RGB(139,69,19)) ; brown
CreateImage(#image_darkGreen, DesktopScaledX(35), DesktopScaledY(35), 24, RGB(0,100,0)) ; darkgreen
#win_flags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(#win_main, 0, 0, 342, 250, "Arduino RGB Led", #win_flags)
ComboBoxGadget(#cmb_comPort, 10, 15, 50, 23)
TextGadget(#txt_lbl1, 10, 195, 60, 20, "R:")
TextGadget(#txt_lbl2, 120, 195, 60, 20, "G:")
TextGadget(#txt_lbl3, 230, 195, 60, 20, "B:")
ButtonGadget(#btn_connect, 67, 14, 90, 26, "CONNECT")
ButtonGadget(#btn_colorReq, 10, 150, 150, 26, "COLOR REQUESTER")
ButtonImageGadget(#btn_red, 10, 50, 33, 26, ImageID(#image_red))
ButtonImageGadget(#btn_yellow, 10, 82, 33, 26, ImageID(#image_yellow))
ButtonImageGadget(#btn_orange, 10, 114, 33, 26, ImageID(#image_orange))
ButtonImageGadget(#btn_green, 48, 50, 33, 26, ImageID(#image_green))
ButtonImageGadget(#btn_blue, 86, 50, 33, 26, ImageID(#image_blue))
ButtonImageGadget(#btn_purple, 124, 50, 33, 26, ImageID(#image_purple))
ButtonImageGadget(#btn_magenta, 48, 82, 33, 26, ImageID(#image_magenta))
ButtonImageGadget(#btn_cyan, 86, 82, 33, 26, ImageID(#image_cyan))
ButtonImageGadget(#btn_white, 124, 82, 33, 26, ImageID(#image_white))
ButtonImageGadget(#btn_deepPink, 48, 114, 33, 26, ImageID(#image_deepPink))
ButtonImageGadget(#btn_brown, 86, 114, 33, 26, ImageID(#image_brown))
ButtonImageGadget(#btn_darkGreen, 124, 114, 33, 26, ImageID(#image_darkGreen))
CanvasGadget(#canvas, 170, 15, 160, 160)
TrackBarGadget(#track_r, 5, 220, 100, 20, 0, 255)
TrackBarGadget(#track_g, 115, 220, 100, 20, 0, 255)
TrackBarGadget(#track_b, 225, 220, 100, 20, 0, 255)
Procedure RepaintCanvas(R, G, B)
RGB_Data(0) = R : RGB_Data(1) = G : RGB_Data(2) = B
SetGadgetText(#txt_lbl1, "R: " + R)
SetGadgetText(#txt_lbl2, "G: " + G)
SetGadgetText(#txt_lbl3, "B: " + B)
SetGadgetState(#track_r, R)
SetGadgetState(#track_g, G)
SetGadgetState(#track_b, B)
If StartDrawing( CanvasOutput(#canvas) )
Box(0,0,OutputWidth(),OutputHeight(),RGB(R,G,B))
StopDrawing()
EndIf
;ShowMemoryViewer(@RGB_Data(),20)
If IsSerialPort(#serial_portNr)
WriteSerialPortData(#serial_portNr, @RGB_Data(), 3)
EndIf
EndProcedure
Procedure Find_Ports()
Protected i
For i = 0 To 255
COMPort="COM"+Str(i+1)
If OpenSerialPort(i, COMPort, 19200, 0, 8, 1, #PB_SerialPort_NoHandshake, 1, 1)
AddGadgetItem(#cmb_comPort,-1, COMPort)
CloseSerialPort(i)
EndIf
Next
PortsFound=1
EndProcedure
Procedure Port_Control()
If IsSerialPort(#serial_portNr) : CloseSerialPort(#serial_portNr) : EndIf
COMPort = GetGadgetText(#cmb_comPort)
If COMPort = ""
MessageRequester("Error","First Select Port !", #PB_MessageRequester_Warning)
ProcedureReturn #False
Else
If IsSerialPort(#serial_portNr); if port open
ProcedureReturn #True
Else ; if port closed
OpenSerialPort(#serial_portNr, COMPort, 19200, 0, 8, 1, #PB_SerialPort_NoHandshake, 1, 1)
If IsSerialPort(#serial_portNr)
ProcedureReturn #True
Else
MessageRequester("Error","Port can't be opened !", #PB_MessageRequester_Error)
ProcedureReturn #False
EndIf
EndIf
EndIf
EndProcedure
Repeat
Event = WaitWindowEvent()
If PortsFound=0
Find_Ports()
EndIf
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case #btn_connect
If IsSerialPort(#serial_portNr)
SetGadgetText(#btn_connect, "CONNECT")
RepaintCanvas(0,0,0)
CloseSerialPort(#serial_portNr)
Else
SetGadgetText(#btn_connect, "DISCONNECT")
Port_Control()
EndIf
Case #btn_colorReq
Color = ColorRequester()
If Color > -1
RepaintCanvas(Red(Color), Green(Color), Blue(Color))
EndIf
Case #track_r
RepaintCanvas(GetGadgetState(#track_r),RGB_Data(1),RGB_Data(2))
Case #track_g
RepaintCanvas(RGB_Data(0),GetGadgetState(#track_g),RGB_Data(2))
Case #track_b
RepaintCanvas(RGB_Data(0),RGB_Data(1),GetGadgetState(#track_b))
Case #btn_red
RepaintCanvas(255,0,0)
Case #btn_yellow
RepaintCanvas(255,255,0)
Case #btn_orange
RepaintCanvas(255,165,0)
Case #btn_green
RepaintCanvas(0,255,0)
Case #btn_blue
RepaintCanvas(0,0,255)
Case #btn_magenta
RepaintCanvas(255,0,255)
Case #btn_cyan
RepaintCanvas(0,255,255)
Case #btn_white
RepaintCanvas(255,255,255)
Case #btn_deepPink
RepaintCanvas(255,20,147)
Case #btn_darkGreen
RepaintCanvas(0,100,0)
Case #btn_brown
RepaintCanvas(139,69,19)
Case #btn_purple
RepaintCanvas(75,0,130)
Case #cmb_comPort
Port_Control()
SetGadgetText(#btn_connect, "DISCONNECT")
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
FreeMemory(*Buffer)
End
Youtube’da izleyebilirsin.. Yorum veya görüşlerini Youtube yorumlarına yazabilirsin..
Written on September 12, 2024