arduino编程笔记.
arduino编程笔记 简单介绍了编程入门 方便初学者快速使用arduino编程 虽然是全英文的 但很实用contentsstructurestructuresetuploopfunctionst curly bracessemicolon/.*/ block comments// line comments10variablesvariablesvariable declaration12variable scopedatatypesbytelongfloat14arraysarithmeticarithmeticcompound assignmentscomparison operatorslogical operatorsconstantsconstantstrue/falsehigh lowinput/outputLow controlifif. else122for9012hiledo.. while22digital i/opin Mode(pin, mode23digitalRead(pin24digitalWrite(pin, value)analog i/oanalog Read(pin)25analogWrite(pin, value)26timedelay(ms)2millis77mathmin(x, y)27max(x, y)27randomrandom Seed (seed)random(min, maxserialSerial begin(rate)29Serial printIn(data)29appendixdigital output32digital input33high current output34pwm output35potentiometer input36variable resistor input37servo output38prefaceThis notebook serves as a convenient, easy to use programming reference forthe command structure and basic syntax of the arduino microcontroller. To keepit simple, certain exclusions were made that make this a beginner's referencebest used as a secondary source alongside other websites, books, workshopsor classes. This decision has lead to a slight emphasis on using the Arduinostandalone purposes and, for example, excludes the more complex uses of orarrays or advanced forms of serial communicationBeginning with the basic structure of Arduino's c derived programminglanguage this notebook continues on to describe the syntax of the mostcommon elements of the language and illustrates their usage with examples andcode fragments. This includes many functions of the core library followed by anappendix with sample schematics and starter programs. The overall formatcompliments O'Sullivan and Igoe's Physical Computing where possibleFor an introduction to the arduino and interactive design refer to banzisGetting Started with Arduino, aka the Arduino Booklet For the brave fewinterested in the intricacies of programming in C, Kernighan and Ritchies The cProgramming language, second edition as well as prinz and crawford s c in aNutshell, provide some insight into the original programming syntaxAbove all else, this notebook would not have been possible without the greatcommunity of makers and shear mass of original material to be found at theArduinowebsiteplaygroundandforumathttp://www.arduino.ccstructureThe basic structure of the Arduino programming language is fairly simple andruns in at least two parts. These two required parts, or functions, enclose blocksof statementsvoid setup()statementsvoid loop(statementsWhere setup( is the preparation, loop( is the execution. Both functions arerequired for the program to workThe setup function should follow the declaration of any variables at the verybeginning of the program. It is the first function to run in the program, is run onlyonce, and is used to set pin Mode or initialize serial communicationThe loop function follows next and includes the code to be executedcontinuously -reading inputs, triggering outputs, etc. This function is the core ofall Arduino programs and does the bulk of the worksetupThe setup function is called once when your program starts. Use it to initializepin modes, or begin serial. It must be included in a program even if there are nostatements to runvoid setup()pinMode(pin, OUTPUT)// sets the pinas outputloopAfter calling the setup( function, the loop function does precisely what itsname suggests, and loops consecutively, allowing the program to change,respond and control the arduino boardvoid loop (digitalWrite(pin, HIGH); // turns pinondelay (1000)// pauses for one seconddigitalwrite(pin, LOW); / turns 'pin' offdelay (1000)pauses for one secondstructure 7functionsa function is a block of code that has a name and a block of statements that areexecuted when the function is called. The functions void setup( and void loophave already been discussed and other built-in functions will be discussed laterCustom functions can be written to perform repetitive tasks and reduce clutter ina program. Functions are declared by first declaring the function type. This is thetype of value to be returned by the function such as int' for an integer typefunction. If no value is to be returned the function type would be void After typedeclare the name given to the function and in parenthesis any parameters beingpassed to the functiontype functionName(parameters)statementsThe following integer type function delay valo is used to set a delay value in aprogram by reading the value of a potentiometer. It first declares a local variablev, sets v to the value of the potentiometer which gives a number between 01023, then divides that value by 4 for a final value between 0-255, and finallyreturns that value back to the main programint delaval()int vi/ create temporary variable vanalogread (pot); / read potentiometer valueV/=4/ converts 0-1023 to 0-255return v:/ return final valuestructuret curly bracesCurly braces (also referred to as just"braces"or"curly brackets")define thebeginning and end of function blocks and statement blocks such as the voidloop function and the for and if statementstype function()statementsAn opening curly brace( must always be followed by a closing curly braceThis is often referred to as the braces being balanced Unbalanced braces canoften lead to cryptic, impenetrable compiler errors that can sometimes be hardto track down in a large programhe arduino environment includes a convenient feature to check the balance ofcurly braces. Just select a brace, or even click the insertion point immediatelyfollowing a brace, and its logical companion will be highlightedsemicolonA semicolon must be used to end a statement and separate elements of theprogram. A semicolon is also used to separate elements in a for loopint x=13; //declares variable x as the integer 13Note: Forgetting to end a line in a semicolon will result in a compiler error. Theerror text may be obvious, and refer to a missing semicolon or it may not. If animpenetrable or seemingly illogical compiler error comes up, one of the firstthings to check is a missing semicolon, near the line where the compilercomplainedstructure 9F.*/ block commentsBlock comments, or multi-line comments, are areas of text ignored by theprogram and are used for large text descriptions of code or comments that helpothers understand parts of the program. they begin with /*and end with */andcan span multiple lines/ this is an enclosed block commentdont forget the closing commentthey have to be balanced!Because comments are ignored by the program and take no memory spacethey should be used generously and can also be used to"comment out blocksof code for debugging purposesNote: While it is possible to enclose single line comments within a blockcomment, enclosing a second block comment is not allowedI line commentsSingle line comments begin with //and end with the next line of code. Like blockcomments, they are ignored by the program and take no memory space// this is a single line commentSingle line comments are often used after a valid statement to provide moreinformation about what the statement accomplishes or to provide a futurereminder10 structure
下载地址
用户评论