{********************************************************************** * Project : LED BOX System Arduino Primer * App Name : 301_LED_Arduino3.txt, delphi_leds.pde :#locs=303 * Purpose : Demonstrates cPort LED COM with Arduino * Date : #sign>max: MAXBOX8: 12/15/2014 11:35:35 AM * History : convert CPort Library to maXbox Aug 2012 : communicates with COM to USB Arduino LED lights; **********************************************************************} Program Arduino_LED3_Compact; {@BOOL WINAPI MessageBeep( __in UINT uType );} function MessageBoxTimeOut(hWnd: HWND; lpText: PChar; lpCaption: PChar; uType: UINT; wLanguageId: WORD; dwMilliseconds: DWORD): Integer; external 'MessageBoxTimeoutA@user32.dll stdcall'; Const BACKMAP = '\examples\images\bmp47.bmp'; //bmp33.bmp SIG ='<--------->'; COMPORT = 4; var chkled1, chkled2, chkled3, chkled4, chkled5: TCheckBox; btn_cnt, btn_Setup, btn_loop: TBitBtn; sBar: TStatusBar; comFrm: TForm; cPort: TComPort; procedure LED_Checker(ab: boolean; checker: boolean); begin if checker then begin chkled1.Checked:= ab; chkled2.Checked:= ab; chkled3.Checked:= ab; chkled4.Checked:= ab; chkled5.Checked:= ab end else begin chkled1.Enabled:= ab; chkled2.Enabled:= ab; chkled3.Enabled:= ab; chkled4.Enabled:= ab; chkled5.Enabled:= ab; end; end; procedure TFrmbtn_connClk(Send: TObject); begin if cPort.Connected then begin btn_cnt.Caption:= 'Connect'; // Toggle caption of Connection Button ! btn_Setup.Enabled:= True; // If not connected, lets enable Setup Button btn_loop.Enabled:= false; // Knight Ride demo button disabled first //This block resets state of all Leds to Off //According to Arduino Code Chars A,B,C,D,E are used to set Pins(2-6) to LOW with cPort do begin WriteStr('A'); WriteStr('B'); WriteStr('C'); WriteStr('D'); WriteStr('E'); end; LED_Checker(false, true); cPort.Close; sBar.Panels[1].Text:='Disconnected'; sBar.Panels[0].Text:='Port Deselect '+cPort.Port; // This block disables check boxes // so user can't change them if COM Port is disconnected LED_Checker(false, false); //enabler end else begin btn_cnt.Caption:='Disconnect'; //Toggle caption of Connection Button btn_Setup.Enabled:=False; // If not connected, lets disable Setup btn_loop.Enabled:=true; // Now that Rider demo button is enabled cPort.Open; // COM Port in use is finally opened sBar.Panels[1].Text:='Connected'; sBar.Panels[0].Text:='Connect: '+cPort.Port; LED_Checker(true, false); end end; procedure TFrmbtn_SetupClk(Send: TObject); begin cPort.ShowSetupDialog; //Opens predefined Setup Dialog (part of component) sBar.Panels[0].Text:= 'Port in use ' + cPort.Port; end; procedure TFrmbtn_loopClk(Send: TObject); begin //turns off all Led Checks to allow clean state of knight ride LED_Checker(false, true); try with cPort do begin for it:= 1 to 5 do begin WriteStr('1'); Sleep(50); WriteStr('A'); Sleep(50); WriteStr('1'); Sleep(150); WriteStr('A'); Sleep(150); WriteStr('1'); Sleep(450); WriteStr('A'); Sleep(250); WriteStr('1'); Sleep(150); WriteStr('A'); Sleep(150); WriteStr('1'); Sleep(50); WriteStr('A'); Sleep(1000); end; end; except Showmessage('ComPort not in use - connect first'); end; end; procedure TFrmCreateCom(Send: TObject); begin cPort:= TComPort.Create(self); with cPort do begin BaudRate:= br9600; Port:= 'COM'+InttoStr(COMPORT); Parity.Bits:= prNone; StopBits:= sbOneStopBit; DataBits:= dbEight; {FlowControl.OutCTSFlow:= False; FlowControl.OutDSRFlow:= False; FlowControl.ControlDTR:= dtrDisable; FlowControl.XonXoffIn:= False;} end; sBar.Panels[0].Text:='Port in use '+cPort.Port; if cPort.Connected=true then sBar.Panels[1].Text:= 'Connected' else sBar.Panels[1].Text:= 'Disconnected' end; // Sending the predifined vars thru serial comm (on byte at a time) // Ports 2,3,4,5,6 are turned ON by sending it corresponding var 1,2,3,4,5 // and they are turned OFF by sending it corresponding var A,B,C,D,E function writeToCOM(achk: TCheckBox; sig1,sig2: char): boolean; begin if achk.checked=true then cPort.WriteStr(sig1) else cPort.WriteStr(sig2) end; procedure chk_led1Clk(Send: TObject); begin writeToCOM(chkled1,'1','A') writeln('debug: com led 1 signal out'); end; procedure chk_led2Clk(Send: TObject); begin writeToCOM(chkled2,'2','B') end; procedure chk_led3Clk(Send: TObject); begin writeToCOM(chkled3,'3','C') end; procedure chk_led4Clk(Send: TObject); begin writeToCOM(chkled4,'4','D') end; procedure chk_led5Clk(Send: TObject); begin writeToCOM(chkled5,'5','E') end; Procedure LabelFactory(a,b,c,d: byte; title: shortstring); begin with TLabel.create(self) do begin parent:= comfrm; SetBounds(a,b,c,d); Caption:= title end; end; Procedure CheckBoxFactory(a,b,c,d: byte; title: shortstring; var abox: TCheckbox; anevent: TNotifyEvent); begin abox:= TCheckBox.create(self) with abox do begin parent:= comfrm; SetBounds(a,b,c,d) Caption:= title; Enabled:= False; OnClick:= anevent as TNotifyEvent; end; end; Procedure BtnFactory(a,b,c,d: smallint; title,apic: string; var abtn: TBitBtn; anEvent: TNotifyEvent); begin abtn:= TBitBtn.create(comfrm); with abtn do begin parent:= comfrm; setBounds(a,b,c,d) font.size:= 12; glyph.LoadFromResourceName(HINSTANCE, apic); mXButton(5,5,width, height,12,12,handle); caption:= title; onClick:= anEvent As TNotifyEvent; end; end; //******************************* Form Builder******************************* procedure TFrm_Close(Send: TObject; var Action: TCloseAction); begin if cPort.Connected then begin cPort.WriteStr('A'); // If application is closed cPort.WriteStr('B'); cPort.WriteStr('C'); // we reset all leds to OFF cPort.WriteStr('D'); cPort.WriteStr('E'); cPort.Close; end; comFrm.Release; writeln('LEDs reset & port closed & Form released'); end; procedure InitComPortForm; var lbls: byte; begin comFrm:= TForm.create(self); with comFrm do begin //FormStyle := fsStayOnTop; Position:= poScreenCenter; caption:='COM Port meets Arduino LED RGB'; width:= 700; height:= 560; //onCreate:= @TFrm_FormCreate; onClose:= @TFrm_Close; Show; canvas.brush.bitmap:= getBitmapObject(Exepath+BACKMAP); Canvas.FillRect(Rect(600,400,210,100)); end; //Constructors & settings BtnFactory(500,440,150,55,'&COM Setup','LEDbulbon',btn_Setup,@TFrmbtn_SetupClk); BtnFactory(180,440,150,55,'C&onnect Nod','CL_MPNEXT',btn_cnt,@TFrmbtn_connClk); BtnFactory(340,440,150,55,'&Knight Ride','CL_MPPLAY',btn_loop,@TFrmbtn_loopClk); CheckBoxFactory(16,42,57,17, 'Led 1',chkled1,@chk_led1Clk); CheckBoxFactory(16,66,57,17, 'Led 2',chkled2,@chk_led2Clk); CheckBoxFactory(16,90,57,17, 'Led 3',chkled3,@chk_led3Clk); CheckBoxFactory(16,114,57,17, 'Led 4',chkled4,@chk_led4Clk); CheckBoxFactory(16,138,57,17, 'Led 5',chkled5,@chk_led5Clk); with TLabel.create(self) do begin parent:= comfrm; setBounds(16,12,69,13) Caption:= 'LED Control'; Font.Color:= clMaroon; Font.Size:= 13; Font.Style:= [fsBold]; end; with TLabel.create(self) do begin parent:= comfrm; setBounds(155,12,69,13) Caption:= 'Arduino PIN'; Font.Color:= clNavy; Font.Size:= 13; Font.Style:= [fsBold]; end; lbls:= 42; for it:= 1 to 5 do begin LabelFactory(80,lbls,39,13, SIG); lbls:= lbls+24 end; lbls:= 42; for it:= 1 to 5 do begin LabelFactory(156,lbls,38,13,'Digit '+inttoStr(it+1)); lbls:= lbls+24 end; with TDateTimePicker.Create(self) do begin parent:= comfrm; Date; top:= 190; left:= 15; calAlignment:= albottom; end; sBar:= TStatusBar.create(self); with sBar do begin parent:= comFrm; //simplepanel:= true; showhint:= true; hint:= 'this is LED BOX State'; Panels.add; panels.items[0].width:= 200; Panels.add; panels.items[1].width:= 150; end; TFrmCreateCom(self); end; //*********************End Form Build************************ begin //main of LED BOX //InifileRead; //ProcessMessagesOFF; writeln('Thread ID :'+intToStr(CurrentThreadID)) writeln('Process ID :'+intToStr(CurrentProcessID)) writeln('machine name: '+getHostName) writeln('user name: '+getUserName) writeln(DateTimeToStr(GetFileCreationTime(exepath+'maxbox3.exe'))); //ExecuteCommand('cmd','/k dir *.*') InitComPortForm; maxform1.mxNavigator.color:= clyellow; //printimage(getbitmapObject(Exepath+backmap),bscentered); //getEnvironmentInfo; End. //----------------------------------------------------------------------- * Controls the state (ON/OFF) of 5 LEDs connected to an Arduino Board * on Digital Pins 2,3,4,5,6 thru the serial comm * * Created April 02 2009 * copyleft 2009 Roberto Ramirez * Full Source code at http://www.thepenguincult.com/proyectos/arduino-delphi-control/ *} /* * Delphi LEDs Control * ----------------- * Turns on and off 5 light emitting diodes(LED) connected to digital * pins 2 thru 6. The LEDs will be controlled using check boxes on Delphi * that sends serial data to Arduino Board. * * IMPORTANT!!: Don't forget to download the Delphi PC Example that controls the leds connected to arduino board. * * Created April 02 2009 * copyleft 2009 Roberto Ramirez * Full Source code at http://www.thepenguincult.com/proyectos/arduino-delphi-control/ * */ int val = 0; // variable to store the data from the serial port int ledPin1 = 2; // LED connected to digital pin 2 int ledPin2 = 3; // LED connected to digital pin 3 int ledPin3 = 4; // LED connected to digital pin 4 int ledPin4 = 5; // LED connected to digital pin 5 int ledPin5 = 6; // LED connected to digital pin 6 void setup() { pinMode(ledPin1,OUTPUT); // declare the LED's pin as output pinMode(ledPin2,OUTPUT); // declare the LED's pin as output pinMode(ledPin3,OUTPUT); // declare the LED's pin as output pinMode(ledPin4,OUTPUT); // declare the LED's pin as output pinMode(ledPin5,OUTPUT); // declare the LED's pin as output Serial.begin(9600); // connect to the serial port } void loop () { val = Serial.read(); // read the serial port if (val !=-1){ if (val=='1'){ digitalWrite(ledPin1,HIGH); } else if (val=='A'){ digitalWrite(ledPin1,LOW); } if (val=='2'){ digitalWrite(ledPin2,HIGH); } else if (val=='B'){ digitalWrite(ledPin2,LOW); } if (val=='3'){ digitalWrite(ledPin3,HIGH); } else if (val=='C'){ digitalWrite(ledPin3,LOW); } if (val=='4'){ digitalWrite(ledPin4,HIGH); } else if (val=='D'){ digitalWrite(ledPin4,LOW); } if (val=='5'){ digitalWrite(ledPin5,HIGH); } else if (val=='E'){ digitalWrite(ledPin5,LOW); } //Serial.println(); } } ---------------------------------------------------------------------- //----------------------------------Code Notes 2------------------------ /* Blink LEDMAX_Delphi Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain. */ int ledPin1 = 11; int ledPinA = 11; int ledPin2 = 13; int counter =0; int change = 1; int val = 0; // variable to store the data from the serial port void setup() { // initialize the digital pin as an output. // Pin 13 has an LED connected on most Arduino boards: Serial.begin(9600); //pinMode(ledPin1, OUTPUT); pinMode(ledPinA, OUTPUT); pinMode(ledPin2, OUTPUT); } int changeCounter() { if (counter ==255) change=-1; if (counter ==0) change= 1; counter = counter + change; return counter; } void ledmax(){ digitalWrite(ledPin1, HIGH); // set the LED on delay(500); // wait for a second digitalWrite(ledPin1, LOW); // set the LED off delay(500); // wait for a second digitalWrite(ledPin1, HIGH); // set the LED on delay(500); // wait for a second digitalWrite(ledPin1, LOW); // set the LED off delay(500); // wait for a second digitalWrite(ledPin1, HIGH); // set the LED off delay(500); // wait for a second digitalWrite(ledPin1, LOW); // set the LED off delay(500); // wait for a second } void loop() { //digitalWrite(ledPin1, HIGH); // set the LED on //delay(4000); // wait for a second //digitalWrite(ledPin1, LOW); // set the LED off //delay(1000); // wait for a second val = Serial.read(); // read the serial port if (val !=-1){ if (val=='1'){ digitalWrite(ledPin2,HIGH); } else if (val=='A'){ digitalWrite(ledPin2,LOW); } } //counter = changeCounter(); //analogWrite(ledPin2, counter); //delay(10); //ledmax(); // digitalWrite(13, HIGH); // set the LED off //delay(100); // wait for a second //digitalWrite(13, LOW); // set the LED off //delay(100); // wait for a second // digitalWrite(13, LOW); // set the LED off //delay(1000); // wait for a second } unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, CPort, CPortCtl, Menus; ------------------------------------------------ Windows crashed again. I am the Blue Screen of Death. No one hears your screams. Three things are certain: Death, taxes, and lost data. Guess which has occurred. There is no place like 127.0.0.1 http://en.wikipedia.org/wiki/Sneakers_%281992_film%29 procedure RotateBitmap(var htmpBitmapDC: TBitmap; var lWidth : Longint; var lHeight : Longint; lRadians : real; origmap: TBitmap); var I,J : Longint; // loop counter //htmpBitmapDC : Longint; // DC of the new bitmap lSine,lCosine : extended; // sine,cosine used in rotation X1,X2,X3,Y1,Y2,Y3 : Longint; // used in calculating new bitmap dimensions lMinX,lMaxX,lMinY,lMaxY: Longint; lNewWidth,lNewHeight : Longint; // width of new bitmap lSrcX,lSrcY : Longint; // x,y pixel coord we are blitting from the source image myrect: TRect; begin // create a compatible DC from the one just brought // compute the sine/cosinse of the radians used to // rotate this image lSine := Sin(lRadians); lCosine := Cos(lRadians); // compute the size of the new bitmap being created X1 := Round(-lHeight * lSine); Y1 := Round(lHeight * lCosine); X2 := Round(lWidth * lCosine - lHeight * lSine); Y2 := Round(lHeight * lCosine + lWidth * lSine); X3 := Round(lWidth * lCosine); Y3 := Round(lWidth * lSine); // figure out the max/min size of the new bitmap lMinX := Min(0, Min(X1, Min(X2, X3))); lMinY := Min(0, Min(Y1, Min(Y2, Y3))); lMaxX := Max(X1, Max(X2, X3)); lMaxY := Max(Y1, Max(Y2, Y3)); // set the new bitmap width/height lNewWidth:= lMaxX - lMinX; lNewHeight:= lMaxY - lMinY; // create a new bitmap based upon the new width/height of the // rotated bitmap //hNewBitmap := CreateCompatibleBitmap(hBitmapDC, lNewWidth, lNewHeight); htmpBitmapDC.height:= lNewHeight; htmpBitmapDC.width:= lNewWidth; //myRect:= Rect(0,0,lwidth,lheight); //bfrm.Canvas.BrushCopy(MyRect, Image1.Picture.Bitmap, MyRect, clred); For I:= 0 To lNewWidth do begin For J:= 0 To lNewHeight do begin lSrcX:= Round((J + lMinX) * lCosine + (I + lMinY) * lSine); lSrcY:= Round((I + lMinY) * lCosine - (J + lMinX) * lSine); If (lSrcX >= 0) And (lSrcX <= lWidth) And (lSrcY >= 0) And (lSrcY <= lHeight) Then begin BitBlt(htmpBitmapDC.canvas.handle,J,I,lnewheight,lnewwidth,origmap.canvas.handle, lSrcX,lSrcY, SRCCOPY); end; end; end; //Image1.Refresh; lWidth:= htmpBitmapDC.width; lHeight:= htmpBitmapDC.height; End; procedure maXCalc_demo; begin printF('this is %.6f',[maXcalc('ln(2)+fact(388)+2!')]); printF('this is %.6f',[maXcalc('(4!)^(3!)')]); printF('this is %.6f',[maXcalc('4!+4!')]); printF('this is %.6f',[maXcalc('log(22)')]); printF('this is logN %.6f',[maXcalc('2%256')]); writeln('ln(e): '+floattostr(maXcalc('ln(e)'))) writeln(floattostr(maXcalc('e+10^6'))) printF('addition theorem %.18f ',[maXcalc('sin(2.5/2)')]) printF('addition theorem %.18f ',[maXcalc('sqrt(1/2*(1-cos(2.5)))')]) printF('addition theorem2 %22.18f ',[maXcalc('cos(2.5/2)')]) printF('addition theorem2 %22.18f ',[maXcalc('sqrt(1/2*(1+cos(2.5)))')]) maXcalcF('2%256+2^10'); end; 50000 = 0.693137180659968 100000 = 0.693142180584982 500000 = 0.693146180561005 10^6 = 0.693146680560255 5*10^6 = 0.693147080560068 50*10^6= 0.693147170560399 //Runtime: 0:28:22.422 ln(2) = 0.693147180559945 (* zeta:= 0; for i:= 1 to 50000 do if i mod 2 = 0 then zeta:= zeta - 1/i else zeta:= zeta + 1/i; writeln('harmonic alternate to ln(2): '+floattostr(zeta)) writeln(floattostr(maxcalc('ln(2)'))) writeln(floattostr(ln2)) *) http://www.hitekdev.com/delphi/BITMAPANIMATION.html processing.app.SerialException: Serial port 'COM5' already in use. Try quiting any programs that may be using it. at processing.app.Serial.(Serial.java:144) at processing.app.Serial.(Serial.java:91) at processing.app.SerialMonitor.openSerialPort(SerialMonitor.java:204) at processing.app.Editor.handleSerial(Editor.java:2390) at processing.app.Editor$18.actionPerformed(Editor.java:668) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.AbstractButton.doClick(AbstractButton.java:357) at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1225) at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1266) at java.awt.Component.processMouseEvent(Component.java:6263) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:6028) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4630) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) at java.awt.Container.dispatchEventImpl(Container.java:2085) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) Declaration TNotifyEvent = procedure (Sender: TObject) of object; The TNotifyEvent type is the type for events that have no parameters. These events simply notify the component that a specific event occurred. For example, OnClick, which is type TNotifyEvent, notifies control that a click event occurred on the control. myform LabelFactory(156,42,38,13, 'Digit 2') LabelFactory(156,66,38,13, 'Digit 3') LabelFactory(156,90,38,13, 'Digit 4') LabelFactory(156,114,38,13, 'Digit 5') LabelFactory(156,138,38,13, 'Digit 6')