{"id":4387,"date":"2024-01-15T09:00:55","date_gmt":"2024-01-15T08:00:55","guid":{"rendered":"https:\/\/cncgraf.com\/?p=4387"},"modified":"2024-02-02T19:35:05","modified_gmt":"2024-02-02T18:35:05","slug":"cnc-programming-g-code-learning-part2","status":"publish","type":"post","link":"https:\/\/cncgraf.com\/en\/2024\/01\/15\/cnc-programmierung-g-code-lernen-teil2\/","title":{"rendered":"Learning G-Code Part 2\/2: Advanced CNC programming"},"content":{"rendered":"<div class=\"wp-block-group alignfull has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h1 class=\"wp-block-heading alignfull has-text-align-center has-base-background-color has-background has-large-font-size\" id=\"g-code-lernen-teil-2-2-fortgeschrittene-cnc-programmierung\">Learn G-Code Part 2\/2: <br>Advanced CNC programming<\/h1>\n\n\n\n<p>In the first part of the series \u201e<a href=\"https:\/\/cncgraf.com\/en\/2023\/12\/20\/cnc-programming-g-code-learn-commands\/\" data-type=\"post\" data-id=\"3742\">CNC programming: Learn G-code - Quick and easy<\/a>\u201c we learnt the basics of G-code. In this article, you will learn more commands such as subroutines, loops, IF statements and R parameters.<\/p>\n\n\n\n<p><strong>Motivation<\/strong>In CNC manufacturing, it is often necessary to produce similar parts with slight variations. Creating a new G-code file for every small change using CAD\/CAM software can be time-consuming. A good solution is to use a customisable G-code file. With parameters, changes in the G-code file can be implemented quickly and easily, which saves time and increases flexibility. In this article, you will learn how parameterised G-code programming makes your CNC processes more efficient.<\/p>\n\n\n\n<p>For CNC programming (G-code), we use our CNC control software as a free G-code simulator. Click here <a href=\"https:\/\/cncgraf.com\/en\/download-cncgraf-cnc-control-software\/\" data-type=\"page\" data-id=\"24\">here<\/a>, to download cncGraF for free.<\/p>\n\n\n\n<p>To find out more about the free G-Code Simulator, click on the blog article <br>\u201e<a href=\"https:\/\/cncgraf.com\/en\/2023\/12\/20\/free-g-code-simulator-cnc-emulator\/\" data-type=\"post\" data-id=\"3822\">cncGraF: Free G-code simulator and CNC machine emulator<\/a>\u201e.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h4>Overview: Advanced CNC programming<\/h4><nav><ul><li class=\"\"><a href=\"#g-code-unterprogramme\">G-Code: Subroutines<\/a><ul><li class=\"\"><a href=\"#unterprogramm-verwaltung\">Sub-programme management<\/a><\/li><\/ul><\/li><li class=\"\"><a href=\"#g-code-schleifen\">G-Code: Loops<\/a><\/li><li class=\"\"><a href=\"#was-ist-ein-r-parameter\">What is an R parameter?<\/a><\/li><li class=\"\"><a href=\"#was-ist-eine-if-anweisung\">What is an IF statement?<\/a><\/li><li class=\"\"><a href=\"#komplettes-g-code-beispiel\">Complete G-code example<\/a><\/li><li class=\"\"><a href=\"#zusammenfassung\">Summary<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group alignfull has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"g-code-unterprogramme\">G-Code: Subroutines<\/h2>\n\n\n\n<p>First, we add a subroutine. We will use the G-code example from the first part of this series \u201e<a href=\"https:\/\/cncgraf.com\/en\/2023\/12\/20\/cnc-programming-g-code-learn-commands\/\" data-type=\"post\" data-id=\"3742\">CNC programming: Learn G-code - Quick and easy<\/a>\u201e. The extended G-code lines are colour-coded in <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">Light red <\/mark><\/strong>highlighted. The G-code then looks like this:<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-group alignfull has-tertiary-background-color has-background has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<hr class=\"wp-block-separator alignfull has-text-color has-cyan-bluish-gray-color has-alpha-channel-opacity has-cyan-bluish-gray-background-color has-background\"\/>\n\n\n\n<p><em>; G-code file: Production of a rectangle on 22\/12\/2023<\/em><br><em>; Update on 23.01.2024<\/em><br><em><br>; Tool number 1 is selected<\/em><br><strong>T1<br><\/strong><em>; Lift the tool at rapid speed to the height Z = 10 mm above the zero point<\/em><strong><br>G00 Z10<br><\/strong><em>; Move to position X = 10 and Y = 10 at rapid speed<\/em><strong><br>G00 X10 Y10<\/strong><br><em>; Switch on the work spindle with M3 at a speed of 2000 rpm<\/em><br><strong>M3 S20000<br><\/strong><em>; Wait 5 seconds until the spindle speed is reached<\/em><strong><br>G04 H5<\/strong><br><br><em>; Relative dimensioning <em>(chain dimension)<\/em>, Command G91 is active<\/em><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">G91<\/mark><br><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">P1 <\/mark><\/strong><em>; Subprogramme 1 is called up<\/em><br><strong><br><\/strong><em>; End of programme<\/em><strong><br>M5 M30<br><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\"> <\/mark><\/strong><em>; Start of sub-programme 1 <\/em><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\"><br>M99 P1<br><\/mark><\/strong><em>; Move to the centre of the rectangle<\/em><strong><br>G00 X50 Y30<br><\/strong><em>; plunge 2 mm into the workpiece at a feed rate of 600 mm per minute<\/em><strong><br>G01 Z-12 F600<br><\/strong><em>; Circle (d=20mm, centre 60\u00d760) clockwise<br>; travelling at a feed rate of 600 mm per minute<\/em><strong><br>G02 I0 J20<br><\/strong><em>; Lift tool at rapid speed : Z = 10mm above the zero point<\/em><strong><br>G00 Z12<br><\/strong><em>; Move to position X = 10 and Y = 10 at rapid speed<\/em><strong><br>G00 X-50 Y-30<br><\/strong><em>; plunge 2 mm into the workpiece at a feed rate of 600 mm per minute<\/em><strong><br>G01 Z-12<br><\/strong><em>; Traverse rectangle 100\u00d7100 mm at a feed rate of 600 mm per minute<\/em><strong><br>G01 X100<br>G01 Y100<br>G01 X-100<br>G01 Y-100<br><\/strong><em>; Lift tool at rapid speed : Z = 10mm above the zero point<\/em><strong><br>G00 Z12<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">M99<\/mark> <\/strong><em>; End of sub-programme 1<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"270\" height=\"279\" src=\"https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-teil2-datei-unterprogramm.jpg\" alt=\"G-code file: Rectangle and circle\" class=\"wp-image-4993\" srcset=\"https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-teil2-datei-unterprogramm.jpg 270w, https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-teil2-datei-unterprogramm-12x12.jpg 12w\" sizes=\"auto, (max-width: 270px) 100vw, 270px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator alignfull has-text-color has-cyan-bluish-gray-color has-alpha-channel-opacity has-cyan-bluish-gray-background-color has-background\"\/>\n<\/div>\n\n\n\n<div class=\"wp-block-group alignfull has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p>The G-code (see above) describes the processing of a rectangle and a circle, whereby the two shapes are described in the sub-programme <strong>1 <\/strong>are defined. The sub-programme is started with the command <strong>P1 <\/strong>in the main programme. <\/p>\n\n\n\n<p>The definition of the sub-programme begins with the command <strong>M99 P1<\/strong> and ends with <strong>M99<\/strong>. All subroutines are located at the end of the main programme, i.e. after the command <strong>M30<\/strong>, which marks the end of the main programme. The number of the sub-programme is indicated by the number after \u201a<strong>P<\/strong>\u201a labelled - in this case <strong>1<\/strong>.<\/p>\n\n\n\n<p>G-code subroutines have the following structure:<br><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">G91<\/mark> <\/strong><em>; Relative dimensioning (incremental dimension) is activated<\/em><br><em>; Main programme<\/em><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">P1 <\/mark><\/strong><em>; Subprogramme 1 is called up<\/em><br><em>; Main programme<\/em><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">M30<\/mark><em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\"> <\/mark><\/em><\/strong><em>; End of the main programme<\/em><\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">M99 P1<\/mark><\/strong><br><em>; Contents of the sub-programme<\/em> 1<br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">M99<\/mark><\/strong><\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"24\" height=\"24\" class=\"wp-image-1775\" style=\"width: 24px;\" src=\"https:\/\/cncgraf.com\/wp-content\/uploads\/2023\/08\/info.png\" alt=\"\"> <strong>Note:<\/strong> Please note that at the beginning of the main programme the command <strong>G91 <\/strong>must be set to activate relative dimensioning (incremental dimension). All subsequent coordinate specifications are therefore relative values. This is necessary so that the sub-programmes can be placed at any position.<\/p>\n\n\n\n<h3 class=\"wp-block-heading has-large-font-size\" id=\"unterprogramm-verwaltung\">Sub-programme management<\/h3>\n\n\n\n<p>The CNC control software <a href=\"https:\/\/cncgraf.com\/en\/cncgraf-8-cnc-control-software-range-of-functions\/\" data-type=\"page\" data-id=\"8\">cncGraF<\/a> has an integrated sub-programme management function. All subroutines can be saved there. In such a case, the subroutine does not have to be contained in the G-code file; instead, the subroutine is only called in the main programme with the G-code command \u201a<strong>P<\/strong>\u201a and the sub-programme number (here <strong>P1<\/strong>).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"967\" height=\"769\" src=\"https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-unterprogramm-verwaltung.jpg\" alt=\"CNC programming: G-code sub-programme management integrated in CNC control system\" class=\"wp-image-4704\" srcset=\"https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-unterprogramm-verwaltung.jpg 967w, https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-unterprogramm-verwaltung-600x477.jpg 600w, https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-unterprogramm-verwaltung-300x239.jpg 300w, https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-unterprogramm-verwaltung-768x611.jpg 768w, https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-unterprogramm-verwaltung-15x12.jpg 15w\" sizes=\"auto, (max-width: 967px) 100vw, 967px\" \/><\/figure>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"24\" height=\"24\" class=\"wp-image-1775\" style=\"width: 24px;\" src=\"https:\/\/cncgraf.com\/wp-content\/uploads\/2023\/08\/info.png\" alt=\"\"> <strong>Note:<\/strong> If a sub-programme with the same sub-programme number (the number after \u201a<strong>P<\/strong>\u201a) is present both in the G-code file and in the subroutine management, the subroutine from the G-code file is used. The software first searches for the subroutine in the file and only then in the G-code management. This makes it possible to \u201aoverwrite\u2018 the subroutine in the administration.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-group alignfull has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"g-code-schleifen\">G-Code: Loops<\/h2>\n\n\n\n<p>To duplicate the shapes defined in the subroutine, the subroutine must be called several times. This is done with the G-code commands<strong> G25 Q<\/strong> and <strong>G26 <\/strong>realised for loops. <br>The G-code section in the main programme then looks as follows: <\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">G25 Q4<\/mark><\/strong><br><strong>P1<\/strong><em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\"><strong> <\/strong><\/mark>; Subprogramme 1 is called up<\/em><br><strong>G00 X110<\/strong><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">G26<\/mark><\/strong><\/p>\n\n\n\n<p>With <strong>G25 Q<\/strong> and <strong>G26 <\/strong>a loop is programmed. <strong>G25 <\/strong>defines the start of the loop, and with <strong>G26 <\/strong>the loop is ended. The parameter <strong>Q<\/strong> defines the number of runs. In our example <strong>4<\/strong> runs are defined. After each sub-programme call, there is a relative position shift in X for the next sub-programme with the line: <strong>G00 X110<\/strong>.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-group alignfull has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"was-ist-ein-r-parameter\">What is an R parameter?<\/h2>\n\n\n\n<p><strong>An R parameter is a placeholder (variable) in the range from R1 to R999 in which a value is saved. <\/strong>Example: <strong>R10 <\/strong>= 99.567. This line defines the parameter <strong>R10<\/strong>, which represents the value <strong>99.567<\/strong> contains. <br>By calling the R parameter (here <strong>R10<\/strong>), its value can be accessed in the G code. Please note the following when using the R parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Basic arithmetic operations Addition, subtraction, multiplication and division<\/strong> can be executed within a parameter. When calculating the values, the rule \u201e<strong><mark style=\"background-color:#ff7d7d\" class=\"has-inline-color has-custom-hintergrund-1-b-5-e-3-ff-color\">Dot before dash<\/mark><\/strong>\u201c is applied. The brackets are not supported. <br>Example:<br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">R56<\/mark> <\/strong><em>= 10\/2 - 2<\/em>*2<br>The result is <strong>1<\/strong><\/li>\n\n\n\n<li>R parameters can be used within the calculation. <br>Example:<br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">R55<\/mark><\/strong> = 10 <br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\"><strong>R56<\/strong> = <strong>R55<\/strong><\/mark>\/2 - 2*2<br>The result is <strong>1<\/strong><\/li>\n\n\n\n<li>R parameters can be assigned in the following places in the G code:<br>Command G00, example G00 X<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">=R10<\/mark><\/strong><br>Command G01, example G01 X<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">=R10<\/mark><\/strong> Y<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">=R11 + 6 \/ 2<\/mark><\/strong><br>Command G02\/G03 X\/Y (arc commands), example: G02 I20 J20 X<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">=R10<\/mark><\/strong> Y<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">=R10<\/mark><\/strong><br>Command G25 Q (loop), example: G25 Q<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">=<strong>R4<\/strong><\/mark><br>Command G53-G60 X\/Y\/Z, example G54 X<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">=<strong>R20<\/strong><\/mark><br>IF statement command, example: $IF <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">R100<\/mark><\/strong>==1<\/li>\n\n\n\n<li>R parameters are global parameters, i.e. if they are defined at the beginning of the G-code file, they are available for the entire file.<\/li>\n\n\n\n<li>R parameters can also be used in sub-programmes.<\/li>\n\n\n\n<li>The values of the R parameters are displayed at the bottom of the status bar of the text editor (see screenshot below)<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"923\" height=\"352\" src=\"https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-parameter.jpg\" alt=\"\" class=\"wp-image-4734\" srcset=\"https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-parameter.jpg 923w, https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-parameter-600x229.jpg 600w, https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-parameter-300x114.jpg 300w, https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-parameter-768x293.jpg 768w, https:\/\/cncgraf.com\/wp-content\/uploads\/2024\/01\/g-code-parameter-18x7.jpg 18w\" sizes=\"auto, (max-width: 923px) 100vw, 923px\" \/><\/figure>\n\n\n\n<p>In the following, we add parameters to our loop. The G-code snippet then looks like this in the main programme:<br><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">R4=4<\/mark><\/strong><em> ; <em>R4<\/em><\/em> <em>Parameter (placeholder) with the value 4 as the number of passes<\/em> <em>create<\/em><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">R5=110<\/mark><\/strong> <em>; R5 parameter (placeholder) with the value 110 for new X position of the sub-programme<\/em><br><br><em>; Assignment of the value via parameter R4<\/em><br><strong>G25 Q<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">=R4<\/mark><\/strong><br><strong>P1<\/strong><em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\"><strong> <\/strong><\/mark>; Subprogramme 1 is called up<\/em><br><strong>G00 X<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">=R5<\/mark><\/strong> <em>; Relative displacement, defined in the R5 parameter<\/em><br><strong>G26<\/strong><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-group alignfull has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"was-ist-eine-if-anweisung\">What is an IF statement?<\/h2>\n\n\n\n<p>The <strong>IF statement <\/strong>is used to check a condition. If this condition is fulfilled, the commands within the condition are executed. The following operations are available:<br>equal <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">==<\/mark><\/strong> <br>unequal<strong> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">!=<\/mark><\/strong> <br>greater than or equal to <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">&gt;=<\/mark><\/strong> <br>less than or equal to <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">&lt;=<\/mark><\/strong><\/p>\n\n\n\n<p>The IF statement consists of the following commands <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">$IF $ENDIF $ELSE $ELSEIF<\/mark><\/strong>. The IF statement must stand alone in the line. IF statement can be contained in the main programme and in the sub-programme.<\/p>\n\n\n\n<p><strong>Example 1: $IF $ENDIF<\/strong><\/p>\n\n\n\n<p><strong>R57=10<\/strong> <em>; Create parameter R57 (placeholder) with the value 10<\/em><br><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">$IF R57== 10<\/mark><\/strong><br><em>; This G-code content is executed because parameter R57 has the value 10 (equals is fulfilled)<\/em><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">$ENDIF<\/mark><\/strong><\/p>\n\n\n\n<p><strong>Example 2: $IF $ELSEIF $ENDIF<\/strong><\/p>\n\n\n\n<p><strong>R57=9<\/strong><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\"><strong>$IF<\/strong> <strong>R57<\/strong>&lt;=9<\/mark><br><em>; This content is executed because parameter R57=9 (less than or equal to is fulfilled)<\/em><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">$ELSEIF R57&gt;=10<\/mark><\/strong><br><em>; This content is NOT executed because parameter R57 has the value 9.<\/em><br><em>; Changing the value to 10 or higher in parameter R57 means that <\/em><br>; <em>this content is executed<\/em>.<br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">$ENDIF<\/mark><\/strong><\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"24\" height=\"24\" class=\"wp-image-1775\" style=\"width: 24px;\" src=\"https:\/\/cncgraf.com\/wp-content\/uploads\/2023\/08\/info.png\" alt=\"\"> <strong>Please note!<\/strong> An IF statement within another IF statement is not supported (see table below).<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>WRONG<\/th><th>CORRECT<\/th><\/tr><\/thead><tbody><tr><td><strong><mark style=\"background-color:#ffd57c\" class=\"has-inline-color\">$IF R200==0<\/mark><\/strong><br><em>; G-Code: Contents<\/em><br><mark style=\"background-color:#ffd57c\" class=\"has-inline-color\"><strong>$IF R1==1<\/strong> <br><em>; G-Code: Contents<\/em><br><strong>$ENDIF<\/strong><\/mark><br><em>; G-Code: Contents<\/em><br><strong><mark style=\"background-color:#ffd57c\" class=\"has-inline-color\">$ENDIF<\/mark><\/strong><\/td><td><strong>$IF R200==0<\/strong><br><em>; G-Code: Contents<\/em><br><strong>$ENDIF<\/strong><br><strong>$IF R1==1<\/strong><br><em>; G-Code: Contents<\/em><br><strong>$ENDIF<\/strong><\/td><\/tr><tr><td><\/td><td><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">An IF statement within another IF statement is not permitted!<\/figcaption><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-group alignfull has-tertiary-background-color has-background has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<hr class=\"wp-block-separator alignfull has-text-color has-cyan-bluish-gray-color has-alpha-channel-opacity has-cyan-bluish-gray-background-color has-background\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-tertiary-background-color has-background has-large-font-size\" id=\"komplettes-g-code-beispiel\">Complete G-code example<\/h2>\n\n\n\n<p>Now that we have learnt about programming subroutines, loops, R parameters and IF statements, we will expand our example to include these commands. <br>Our G-code then looks like this:<\/p>\n\n\n\n<p><em>; G-code file: Production of a rectangle on 15\/01\/2024<\/em><br><em>; Update on 23.01.2024<\/em><br><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">R3=3<\/mark> <\/strong><em><strong>;<\/strong> Set the number of passes in Y<\/em><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">R4=4<\/mark> <\/strong><em>; Set number of runs in X<\/em><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">R5=110<\/mark><\/strong> <em>; X Shift for new sub-programme<\/em> set<br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">R6=110<\/mark><\/strong>; <em>Y Shift for new sub-programme<\/em> set<br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">R7=1<\/mark><\/strong> <em>; Specify whether a circle is to be created, 1 - yes, 0 - no<\/em><\/p>\n\n\n\n<p><em>; Tool number 1 is selected<\/em><br><strong>T1<\/strong><br><em>; Lift the tool at rapid speed to the height Z = 10 mm above the zero point<\/em><br><strong>G00 Z10<\/strong><br><em>; Move to position X = 10 and Y = 10 at rapid speed<\/em><br><strong>G00 X10 Y10<\/strong><br><em>; Switch on the work spindle with M3 at a speed of 2000 rpm<\/em><br><strong>M3 S2000<\/strong><br><em>; Wait 5 seconds until the spindle speed is reached<\/em><br><strong>G04 H5<\/strong><br><br><em>; Relative dimensioning (incremental dimension) is activated<\/em><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">G91<\/mark><\/strong><br><br><em>; Assignment of the value via parameter R3<\/em><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">G25 Q=R3<\/mark><\/strong><br><em>; Assignment of the value via parameter R4<\/em><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">G25 Q=R4<\/mark><\/strong><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">P1<\/mark><em> <\/em><\/strong><em>; Subprogramme 1 is called up<\/em><br><strong>G00 X<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">=R5<\/mark><\/strong> <em>; Relative displacement, defined in the R5 parameter<\/em><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">G26<\/mark><\/strong><br><strong>G00 Y<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">=R6<\/mark><\/strong> <em>; Relative displacement in X, defined in the R6 parameter<\/em><br><strong>G00 X<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">=-R5*R4<\/mark><\/strong> <em>; Relative displacement in X to starting position<\/em><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">G26<\/mark><\/strong><\/p>\n\n\n\n<p><em>; End of programme<\/em><br><strong>M5 M30<\/strong><\/p>\n\n\n\n<p><em>; Start of sub-programme 1<\/em><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">M99 P1<\/mark><\/strong><br><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">$IF R7==1<\/mark><\/strong><br><em>; Move to the centre of the rectangle<\/em><br><strong>G00 X50 Y30<\/strong><br><em>; plunge 2 mm into the workpiece at a feed rate of 600 mm per minute<\/em><br><strong>G01 Z-12 F600<\/strong><br><em>; Circle (d=20mm, centre 60\u00d760) clockwise<br>; travelling at a feed rate of 600 mm per minute<\/em><br><strong>G02 I0 J20<\/strong><br><em>; Lift tool at rapid speed : Z = 10mm above the zero point<\/em><br><strong>G00 Z12<\/strong><br><em>; Move to position X = 10 and Y = 10 at rapid speed<\/em><br><strong>G00 X-50 Y-30<\/strong><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">$ENDIF<\/mark><\/strong><br><br><em>; plunge 2 mm into the workpiece at a feed rate of 600 mm per minute<\/em><br><strong>G01 Z-12<\/strong><br><em>; Traverse rectangle 100\u00d7100 mm at a feed rate of 600 mm per minute<\/em><br><strong>G01 X100<br>G01 Y100<br>G01 X-100<br>G01 Y-100<\/strong><br><em>; Lift tool at rapid speed : Z = 10mm above the zero point<\/em><br><strong>G00 Z12<\/strong><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">M99<\/mark> <\/strong><em>; End of sub-programme 1<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"836\" src=\"https:\/\/cncgraf.com\/wp-content\/uploads\/2023\/12\/g-code-teil2-datei-komplette-g-code-datei-1024x836.jpg\" alt=\"G-Code Simulator cncGraF: Use of subroutines in G-Code\" class=\"wp-image-4998\" srcset=\"https:\/\/cncgraf.com\/wp-content\/uploads\/2023\/12\/g-code-teil2-datei-komplette-g-code-datei-1024x836.jpg 1024w, https:\/\/cncgraf.com\/wp-content\/uploads\/2023\/12\/g-code-teil2-datei-komplette-g-code-datei-600x490.jpg 600w, https:\/\/cncgraf.com\/wp-content\/uploads\/2023\/12\/g-code-teil2-datei-komplette-g-code-datei-300x245.jpg 300w, https:\/\/cncgraf.com\/wp-content\/uploads\/2023\/12\/g-code-teil2-datei-komplette-g-code-datei-768x627.jpg 768w, https:\/\/cncgraf.com\/wp-content\/uploads\/2023\/12\/g-code-teil2-datei-komplette-g-code-datei-15x12.jpg 15w, https:\/\/cncgraf.com\/wp-content\/uploads\/2023\/12\/g-code-teil2-datei-komplette-g-code-datei.jpg 1045w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator alignfull has-text-color has-cyan-bluish-gray-color has-alpha-channel-opacity has-cyan-bluish-gray-background-color has-background\"\/>\n<\/div>\n\n\n\n<div class=\"wp-block-group alignfull has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p>Several R parameters, loops and IF statements are used in the G code (see above). The G-code file can be changed very easily using the R parameters. The R parameters <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">R3 <\/mark><\/strong>and <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">R4 <\/mark><\/strong>define the number of parts in X and Y. The R parameters <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">R5 <\/mark><\/strong>and <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\">R6 <\/mark><\/strong>determine the distance between the parts in X and Y, and the last parameter, <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-hintergrund-3-ff-7-d-7-d-color\"><strong>R7<\/strong><\/mark>, defines whether a circle is to be output or not (see IF statement in the subroutine).<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-group alignfull has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"zusammenfassung\">Summary<\/h2>\n\n\n\n<p>In this blog article, we have looked at the various facets of CNC programming. Starting with the basics, which are explained in the article <br>\u201a<a href=\"https:\/\/cncgraf.com\/en\/2023\/12\/20\/cnc-programming-g-code-learn-commands\/\" data-type=\"post\" data-id=\"3742\">CNC programming: Learn G-code - Quick and easy<\/a>\u201a to advanced topics such as R parameters, loops and IF statements. We have thus covered the breadth and depth of CNC programming.<br><br>We hope that this blog article will help you to achieve the desired success.<br>Yours sincerely, Your BOENIGK-electronics Team<br><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/www.cnc-controller.eu\/\" target=\"_blank\" rel=\"noopener\">To the online shop<\/a><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>In the first part of the series \u201eCNC programming: learning G-code - quick and easy\u201c, we learnt the basics of G-code. In this article, you will learn more commands such as subroutines, loops, IF statements and R parameters.<\/p>","protected":false},"author":1,"featured_media":4996,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,1],"tags":[34,14,30],"class_list":["post-4387","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-g-code","category-allgemein","tag-cnc-programmierung","tag-g-code","tag-g-code-simulator"],"_links":{"self":[{"href":"https:\/\/cncgraf.com\/en\/wp-json\/wp\/v2\/posts\/4387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cncgraf.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cncgraf.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cncgraf.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cncgraf.com\/en\/wp-json\/wp\/v2\/comments?post=4387"}],"version-history":[{"count":0,"href":"https:\/\/cncgraf.com\/en\/wp-json\/wp\/v2\/posts\/4387\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cncgraf.com\/en\/wp-json\/wp\/v2\/media\/4996"}],"wp:attachment":[{"href":"https:\/\/cncgraf.com\/en\/wp-json\/wp\/v2\/media?parent=4387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cncgraf.com\/en\/wp-json\/wp\/v2\/categories?post=4387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cncgraf.com\/en\/wp-json\/wp\/v2\/tags?post=4387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}