{"id":7166,"date":"2021-12-24T01:37:55","date_gmt":"2021-12-24T01:37:55","guid":{"rendered":"https:\/\/www.qec.tw\/?p=7166"},"modified":"2022-01-11T09:37:45","modified_gmt":"2022-01-11T09:37:45","slug":"sketch","status":"publish","type":"post","link":"https:\/\/www.qec.tw\/zh\/86duino-reference\/sketch\/","title":{"rendered":"Sketch"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this tutorial, you\u2019ll learn how each part of that sketch works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sketch<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A&nbsp;<em>sketch<\/em>&nbsp;is the name that Arduino and 86Duino use for a program. It\u2019s the unit of code that is uploaded to and run on an Arduino or 86Duino board.<\/p>\n\n\n<hr>\n\n\n<h2 class=\"wp-block-heading\">Comments<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The first few lines of the&nbsp;<a href=\"http:\/\/arduino.cc\/en\/Tutorial\/Blink\" target=\"_blank\" rel=\"noopener\">Blink<\/a>&nbsp;sketch are a&nbsp;<em>comment<\/em>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\/*\n* Blink\n*\n* The basic Arduino example.  Turns on an LED on for one second,\n* then off for one second, and so on...  We use pin 13 because,\n* depending on your Arduino board, it has either a built-in LED\n* or a built-in resistor so that you need only an LED.\n*\n* http:\/\/www.arduino.cc\/en\/Tutorial\/Blink\n*\/<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Everything between the&nbsp;<code>\/*<\/code>&nbsp;and&nbsp;<code>*\/<\/code>&nbsp;is ignored by the Arduino\/86Duino when it runs the sketch (the&nbsp;<code>*<\/code>&nbsp;at the start of each line is only there to make the comment look pretty, and isn\u2019t required). It\u2019s there for people reading the code: to explain what the program does, how it works, or why it\u2019s written the way it is. It\u2019s a good practice to comment your sketches, and to keep the comments up-to-date when you modify the code. This helps other people to learn from or modify your code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There\u2019s another style for short, single-line comments. These start with&nbsp;<code>\/\/<\/code>&nbsp;and continue to the end of the line. For example, in the line:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>int ledPin = 13; \/\/ LED connected to digital pin 13<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">the message \u201cLED connected to digital pin 13\u201d is a comment.<\/p>\n\n\n<hr>\n\n\n<h2 class=\"wp-block-heading\">Variables<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A&nbsp;<em>variable<\/em>&nbsp;is a place for storing a piece of data. It has a name, a type, and a value. For example, the line from the Blink sketch above declares a variable with the name&nbsp;<code>ledPin<\/code>, the type&nbsp;<code>int<\/code>, and an initial value of 13. It\u2019s being used to indicate which Arduino\/86Duino pin the LED is connected to. Every time the name&nbsp;<code>ledPin<\/code>&nbsp;appears in the code, its value will be retrieved. In this case, the person writing the program could have chosen not to bother creating the&nbsp;<code>ledPin<\/code>&nbsp;variable and instead have simply written 13 everywhere they needed to specify a pin number. The advantage of using a variable is that it\u2019s easier to move the LED to a different pin: you only need to edit the one line that assigns the initial value to the variable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Often, however, the value of a variable will change while the sketch runs. For example, you could store the value read from an input into a variable. There\u2019s more information in the&nbsp;<a href=\"http:\/\/www.86duino.com\/?p=3124\" target=\"_blank\" rel=\"noopener\">Variables tutorial<\/a>.<\/p>\n\n\n<hr>\n\n\n<h2 class=\"wp-block-heading\">Functions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A&nbsp;<em>function<\/em>&nbsp;(otherwise known as a&nbsp;<em>procedure<\/em>&nbsp;or&nbsp;<em>sub-routine<\/em>) is a named piece of code that can be used from elsewhere in a sketch. For example, here\u2019s the definition of the&nbsp;<code>setup()<\/code>&nbsp;function from the Blink example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>void setup()\n{\npinMode(ledPin, OUTPUT);      \/\/ sets the digital pin as output\n}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The first line provides information about the function, like its name, \u201csetup\u201d. The text before and after the name specify its return type and parameters: these will be explained later. The code between the&nbsp;<code>{<\/code>&nbsp;and&nbsp;<code>}<\/code>&nbsp;is called the&nbsp;<em>body<\/em>&nbsp;of the function: what the function does.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can&nbsp;<em>call<\/em>&nbsp;a function that\u2019s already been defined (either in your sketch or as part of the&nbsp;<a href=\"https:\/\/www.qec.tw\/86duino\/86duino-language-reference\/\">86Duino language<\/a>). For example, the line&nbsp;<code>pinMode(ledPin, OUTPUT);<\/code>&nbsp;calls the&nbsp;<code>pinMode()<\/code>&nbsp;function, passing it two parameters:&nbsp;<code>ledPin<\/code>&nbsp;and&nbsp;<code>OUTPUT<\/code>. These parameters are used by the&nbsp;<code>pinMode()<\/code>&nbsp;function to decide which pin and mode to set.<\/p>\n\n\n<hr>\n\n\n<h3 class=\"wp-block-heading\">pinMode(), digitalWrite(), and delay()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>pinMode()<\/code>&nbsp;function configures a pin as either an input or an output. To use it, you pass it the number of the pin to configure and the constant&nbsp;<code>INPUT<\/code>&nbsp;or&nbsp;<code>OUTPUT<\/code>. When configured as an input, a pin can detect the state of a sensor like a pushbutton (refer to the&nbsp;<a href=\"http:\/\/arduino.cc\/en\/Tutorial\/DigitalReadSerial\" target=\"_blank\" rel=\"noopener\">Arduino tutorial<\/a>). As an output, it can drive an actuator like an LED.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>digitalWrite()<\/code>&nbsp;functions outputs a value on a pin. For example, the line:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>digitalWrite(ledPin, HIGH);<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">set the&nbsp;<code>ledPin<\/code>&nbsp;(pin 13) to&nbsp;<code>HIGH<\/code>, or 5 volts. Writing a&nbsp;<code>LOW<\/code>&nbsp;to pin connects it to ground, or 0 volts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>delay()<\/code>&nbsp;causes the 86Duino to wait for the specified number of milliseconds before continuing on to the next line. There are 1000 milliseconds in a second, so the line:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>delay(1000);<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">creates a delay of one second.<\/p>\n\n\n<hr>\n\n\n<h3 class=\"wp-block-heading\">setup() and loop()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">There are two special functions that are a part of every 86Duino sketch:&nbsp;<code>setup()<\/code>&nbsp;and&nbsp;<code>loop()<\/code>. The&nbsp;<code>setup()<\/code>&nbsp;is called once, when the sketch starts. It\u2019s a good place to do setup tasks like setting pin modes or initializing libraries. The&nbsp;<code>loop()<\/code>&nbsp;function is called over and over and is heart of most sketches. You need to include both functions in your sketch, even if you don\u2019t need them for anything.<\/p>\n\n\n<hr>\n\n\n<h2 class=\"wp-block-heading\">See also<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>[Language]&nbsp;<a href=\"https:\/\/www.qec.tw\/86duino-reference\/setup\/\">setup<\/a>()<\/li><li>[Language]&nbsp;<a href=\"https:\/\/www.qec.tw\/86duino-reference\/loop\/\">loop<\/a>()<\/li><li>[Language]&nbsp;<a href=\"https:\/\/www.qec.tw\/86duino-reference\/pinmode\/\">pinMode<\/a>()<\/li><li>[Language]&nbsp;<a href=\"https:\/\/www.qec.tw\/86duino-reference\/digitalwrite\/\">digitalWrite<\/a>()<\/li><li>[Language] <a href=\"https:\/\/www.qec.tw\/86duino-reference\/digitalread\/\">digitalRead<\/a>()<\/li><li>[Language]&nbsp;<a href=\"https:\/\/www.qec.tw\/86duino-reference\/delay\/\">delay<\/a>()<\/li><\/ul>\n\n\n<hr>\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.qec.tw\/86duino\/86duino-language-reference\/\">Language Reference Home<\/a><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em>The text of the 86Duino reference is a modification of&nbsp;<a href=\"http:\/\/arduino.cc\/en\/Reference\/HomePage\" target=\"_blank\" rel=\"noopener\">the Arduino reference<\/a>&nbsp;and is licensed under a&nbsp;<a href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/3.0\/\" target=\"_blank\" rel=\"noopener\">Creative Commons Attribution-ShareAlike 3.0 License<\/a>. Code samples in the reference are released into the public domain.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you\u2019ll learn how each part of that sketch works. Sketch A&nbsp;sketch&nbsp;is the name that Arduino and 86Duino use for a program. It\u2019s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[195],"tags":[162],"class_list":["post-7166","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-86duino-reference","tag-86duino"],"_links":{"self":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/7166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/comments?post=7166"}],"version-history":[{"count":0,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/7166\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media\/7167"}],"wp:attachment":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media?parent=7166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/categories?post=7166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/tags?post=7166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}