Arduino编程语言参考大全
Arduino-语法手册

函数部分
十五、数字 I/O 15.1 pinMode() 15.2 digitalWrite() 15.3 digitalRead() 十六、模拟 I/O 16.1 analogReference() 16.2 analogRead() 16.3 analogWrite() PWM 十七、高级 I/O 17.1 tone() 17.2 noTone() 17.3 shiftOut() 17.4 shiftIn() 17.5 pulseIn() 十八、时间 18.1 millis() 18.2 micros()
Arduino 语法手册
Arduino 的程序可以划分为三个主要部分:结构、变量(变量与常量) 、函数。
结构部分
一、结构 1.1 setup() 1.2 loop() 二、结构控制 2.1 if 2.2 if...else 2.3 for 2.4 switch case 2.5 while 2.6 do... while 2.7 break 2.8 continue 2.9 return 2.10 goto 三、扩展语法 3.1 ;(分号) 3.2 {}(花括号) 3.3 //(单行注释) 3.4 /* */(多行注释) 3.5 #define 3.6 #include 四、算数运算符 4.1 =(赋值运算符) 4.2 +(加) 4.3 -(减) 4.4 *(乘) 4.5 /(除) 4.6 %(模) 五、比较运算符 5.1 ==(等于) 5.2 !=(不等于) 5.3 <(小于) 5.4 >(大于) 5.5 <=(小于等于) 5.6 >=(大于等于) 六、布尔运算符 6.1 &&(与)
6.2 ||(或) 6.3 !(非) 七、指针运算符 7.1 * 取消引用运算符 7.2 & 引用运算符 八、位运算符 8.1 & (bitwise and) 8.2 | (bitwise or) 8.3 ^ (bitwise xor) 8.4 ~ (bitwise not) 8.5 << (bitshift left) 8.6 >> (bitshift right) 九、复合运算符 9.1 ++ (increment) 9.2 -- (decrement) 9.3 += (compound addition) 9.4 -= (compound subtraction) 9.5 *= (compound multiplication) 9.6 /= (compound division) 9.6 &= (compound bitwise and) 9.8 |= (compound bitwise or)
Arduino 语法手册

#Arduino 语法setup() 初始化函数loop() 循环体函数控制语句类似于C//if if...else for switch case while do... while break continue return goto扩展语法类似于C//;(分号) {}(花括号) //(单行注释) /* */(多行注释) #define #include算数运算符类似于C//=(赋值运算符) +(加) -(减) *(乘) /(除) %(模)比较运算符类似于C//==(等于) !=(不等于) <(小于) >(大于) <=(小于等于) >=(大于等于)布尔运算符类似于C//&&(与) ||(或) !(非)指针运算符类似于C//* 取消引用运算符 & 引用运算符位运算符类似于C& (bitwise and) | (bitwise or) ^ (bitwise xor) ~ (bitwise not) << (bitshift left) >> (bitshift right)复合运算符类似于C++ (increment) -- (decrement) += (compound addition) -= (com pound subtraction) *= (compound multiplication) /= (compound divis ion) &= (compound bitwise and) |= (compound bitwise or)常量constants 预定义的常量BOOL true false引脚电压定义,HIGH和LOW【当读取(read)或写入(write)数字引脚时只有两个可能的值: HIGH 和 LOW 】HIGH(参考引脚)的含义取决于引脚(pin)的设置,引脚定义为INPUT或OUTPUT时含义有所不同。
Arduino编程知识学习大全(中册)

6.3 驱动LED点阵
O 6.3.1 LED点阵显示表情 O 6.3.2 LED点阵跑马灯 O 6.3.3 回纹灯 O 6.3.4 矩形回缩灯
6.3.1 LED点阵显示表情
6.3.2 LED点阵跑马灯
O 在6.3.5小节中实现了由三个LED组成的简 易跑马灯,在本节就在8*8的LED点阵上来 实现跑马灯。这个示例的电路不需要改动, 我们直接编写对应的程序就可以了。
6.4 使用74HC595驱动 LED
O 6.4.1 74HC595使用方式 O 6.4.2 使用74HC595驱动LED点阵
6.4.1 74HC595使用方式
O 74HC595是一个CMOS(Complementary Metal-Oxide-Semiconductor)器件
6.4.2 使用74HC595驱动LED点 阵
O pinMode(pin,mode)
6.2.1 使用数字针脚点亮LED
O 由于数字针脚只能输出高电压和低电压, 因此在不借用其他元件的情况下只可以点 亮或者熄灭LED。
1.电路图
2.程序
O int pin=10;
//指定LED连接的针脚
O void setup(){ O pinMode(pin,OUTPUT);//设置针脚模式为输出 O digitalWrite(pin,HIGH);//设置针脚输出电压
寄存器地址
D15~D12 X X X X X X X X X
D11 D10
0
0
0
0
0
0
0
0
0
1
1
1
0
D9 D8
0
0
0
1
1
0
1
1
学好Arduino编程,从新手到达人系列(一)

学好Arduino编程,从新手到达人系列(一)作者:来源:《电脑报》2018年第44期随着Arduino开源硬件的流行,越来越多非工程背景的人士能轻松借助Arduino来快速实现自己的创意,成为名副其实的创客。
如今,Arduino也逐渐走入了中国的校园,越来越多的中国学生也因此接触到了图形化的软硬件编程。
Arduino是一款便捷灵活、方便上手的开源电子原型平台。
它由板子大叔(Massimo Banzi)和他的团队创造,Arduino具有跨平台、开放性强、界面简单清晰等特点,被广泛运用于电子元器件的开发控制。
虽然Arduino已经把硬件和软件分离,软件端也做到了底层驱动代码和应用代码的隔离,用户只要学一些简单的入门编程知识,也能够快速上手,但是,代码编程对于大部分青少年来说仍然存在着技术壁垒。
因此,认识和了解简易图形化编程软件,对于想学习图形化编程软件的青少年来说就显得非常有必要了。
一、让Arduino 说出 Hello World!1.Hello World!首先先来练习一个不需要其他辅助元件,只需要一块Arduino 和一根下载线的简单实验,让我们的Arduino 说出“Hello World!”,这是一个让Arduino 和PC 机通信的实验,这也是一个入门实验,希望可以帶领大家进入Arduino 的世界。
2.实验用到的电子原件及电子模块:①Arduino板 1个②USB 数据线我们按照上面所讲的将Arduino 的驱动安装好后,我们打开Arduino 的软件,编写一段程序让Arduino 接收到我们发的指令就显示“Hello World!”字符串,当然你也可以让Arduino 不用接收任何指令就直接不断回显“Hello World!”,其实很简单,一条if()语句就可以让你的Arduino 听从你的指令了,我们再借用一下Arduino 自带的数字13 口LED,让Arduino 接收到指令时LED 闪烁一下,再显示“Hello World!”。
Arduino语法参考

(无符号整型)unsigned int常量个字节的空间,但是无符号的前缀意味着它不能存储负数,一样,也用2像Arduino语言有一些特殊意义的关键字。
例如HIGH和LOW用来表示你打开或关闭int用来设定某个特0-65535。
它的范围是INPUT(高电平或者低电平)Arduino 的一个引脚(pin),和OUTPUT定引脚是输入接口还是输出接口。
True,像它们的字面意思一样;表示一个条件或一个运算是真还是假。
longFalse(长整型)和之间的数字。
它是int的两倍大小。
能够存储-2147483648到2417483647变量变量保存在(无符号长整型)unsigned longArduino内存中,它可以用来保存数据,你可以应用这个变量的数据变量在程序中是可以随便更改的。
来计算或者将这个数据应用到程序中。
正如字面意思,0无符号长整型的范围是到4294967295是一个非常简单的微处理器,所以当你声明一个变量时,还必须指因为Arduino float定它的类型。
这意味着告诉处理器为你所要存储的数据留出多大的空间。
(浮点型)它的存储空间很大,能够存储浮点值,你能用它存储带小数的数字,每个浮点型下面是一些常见的数据类型:会用掉四个字节,所以要谨慎使用。
Boolean(布尔)只能是真或者假两种值。
double(双精度浮点型)次方。
哇,非30810的1.797 693 134 862 315 7双精度浮点数最大值为乘以常大!char(字符)Arduino?保存一个字符,如。
和任何计算机一样,将字符存储为一个数字,虽string(字符串)。
然你拷打的是文字。
当字符用数字里存储时,数值范围是-128到127字符来存储文本信息(你可以用字符串通过串口发送一条信息、或用一组ASCII加上一个。
注意:字符串的每一个字符会占用一个字节的存储空间,者在LCD显示屏上展示)个可用字零字符。
表示字符串的结束。
127ASCIIUNICODE在计算机系统里主要有两组有效字符:ASCII和。
Arduino的程序语言

Arduino的程序语言一一个Arduino脚本程式码(SKETCH)由两部分组成void setup()在这个函数范围内放置初始化Arduino 板子的程式- 在主要程式开始撰写前,使Arduino 板子装置妥当的指令。
void loop()在此放置你的Arduino脚本。
这部份的程式会一直重复的被执行,直到Arduino 板子被关闭。
特殊符号Arduino 语言用了一些符号描绘程式码,例如注释和程式区块。
; //(分号)Arduino 语言每一行程序都是以分号为结尾。
这样的语法让你可以自由地安排代码,你可以将两个指令放置在同一行,只要中间用分号隔开。
(但这样做可能降低程式的可读性。
)范例:delay(100);{}(大括号)大括号用来将程式代码分成一个又一个的区块,如以下范例所示,在loop()函数的前、后,必须用大括号括起来。
范例:void loop(){Serial.pritln("cial");}注释程式的注释就是对代码的解释和说明,编写注释有助于程式设计师(或其他人)了解代码的功能。
Arduino处理器在对程式码进行编译时会忽略注释的部份。
Arduino 语言中的编写注释有两种方式//单行注释:这整行的文字会被处理器忽略/*多行注释:在这个范围内你可以写一整首诗*/常数在Arduino语言中事先定义了一些具特殊用途的保留字。
HIGH 和LOW 用来表示你开启或是关闭了一个Arduino的脚位(pin)。
INPUT 和OUTPUT 用来指示这个Arduino的脚位(pin)是属于输入或是输出用途。
true 和false 用来指示一个条件或表示式为真或是假。
变数变数用来指定Arduino 记忆体中的一个位置,变数可以用来储存资料,程式人员可以透过脚本代码去不限次数的操作变数的值。
因为Arduino 是一个非常简易的微处理器,但你要宣告一个变数时必须先定义他的资料型态,好让微处理器知道准备多大的空间以储存这个变数值。
Arduino-语法手册

Arduino 语法手册Arduino 的程序可以划分为三个主要部分:结构、变量(变量与常量)、函数。
结构部分一、结构1.1 setup()1.2 loop()二、结构控制2.1 if2.2 if...else2.3 for2.4 switch case2.5 while2.6 do... while2.7 break2.8 continue2.9 return2.10 goto三、扩展语法3.1 ;(分号)3.2 {}(花括号)3.3 //(单行注释)3.4 /* */(多行注释)3.5 #define3.6 #include四、算数运算符4.1 =(赋值运算符)4.2 +(加)4.3 -(减)4.4 *(乘)4.5 /(除)4.6 %(模)五、比较运算符5.1 ==(等于)5.2 !=(不等于)5.3 <(小于)5.4 >(大于)5.5 <=(小于等于)5.6 >=(大于等于)六、布尔运算符6.1 &&(与)6.2 ||(或)6.3 !(非)七、指针运算符7.1 * 取消引用运算符7.2 & 引用运算符八、位运算符8.1 & (bitwise and)8.2 | (bitwise or)8.3 ^ (bitwise xor)8.4 ~ (bitwise not)8.5 << (bitshift left)8.6 >> (bitshift right)九、复合运算符9.1 ++ (increment)9.2 -- (decrement)9.3 += (compound addition)9.4 -= (compound subtraction)9.5 *= (compound multiplication)9.6 /= (compound division)9.6 &= (compound bitwise and)9.8 |= (compound bitwise or)变量部分十、常量10.1 HIGH|LOW(引脚电压定义)10.2 INPUT|OUTPUT(数字引脚(Digital pins)定义)10.3 true | false(逻辑层定义)10.4 integer constants(整数常量)10.5 floating point constants(浮点常量)十一、数据类型11.1 void11.2 boolean(布尔)11.3 char(有号数据类型)11.4 unsigned char(无符号数据类型)11.5 byte(无符号数)11.6 int(整型)11.7 unsigned int(无符号整型)11.8 word11.9 long(长整数型)11.10 unsigned long(无符号长整数型)11.11 float(浮点型数)11.12 double(双精度浮点数)11.13 string(char array/字符串)11.14 String object(String类)11.15 array (数组)十二、数据类型转换12.1 char()12.2 byte()12.3 int()12.4 word()12.5 long()12.6 float()十三、变量作用域& 修饰符13.1 variable scope(变量的作用域)13.2 static(静态变量)13.3 volatile13.4 const十四、辅助工具14.1 sizeof()函数部分十五、数字I/O15.1 pinMode()15.2 digitalWrite()15.3 digitalRead()十六、模拟I/O16.1 analogReference()16.2 analogRead()16.3 analogWrite() PWM十七、高级I/O17.1 tone()17.2 noTone()17.3 shiftOut()17.4 shiftIn()17.5 pulseIn()十八、时间18.1 millis()18.2 micros()18.3 delay()18.4 delayMicroseconds() 十九、数学运算19.1 min()19.2 max()19.3 abs()19.4 constrain()19.5 map()19.6 pow()19.7 sqrt()19.8 ceil()19.9 exp()19.10 fabs()19.11 floor()19.12 fma()19.13 fmax()19.14 fmin()19.15 fmod()19.16 ldexp()19.17 log()19.18 log10()19.19 round()19.20 signbit()19.21 sq()19.22 square()19.23 trunc()二十、三角函数20.1 sin()20.2 cos()20.3 tan()20.4 acos()20.5 asin()20.6 atan()20.7 atan2()20.8 cosh()20.9 degrees()20.10 hypot()20.11 radians()20.12 sinh()20.13 tanh()二十一、随机数21.1 randomSeed()21.2 random()二十二、位操作22.1 lowByte()22.2 highByte()22.3 bitRead()22.4 bitWrite()22.5 bitSet()22.6 bitClear()22.7 bit()二十三、设置中断函数23.1 attachInterrupt()23.2 detachInterrupt()二十四、开关中断24.1 interrupts()(中断)24.2 noInterrupts()(禁止中断)二十五、通讯25.1 Serial25.1.1 if (Serial)25.1.2 Serial.available()25.1.3 Serial.begin()25.1.4 Serial.end()25.1.5 Serial.find()25.1.6 Serial.findUntil()25.1.7 Serial.flush()25.1.8 Serial.parseFloat()25.1.9 Serial.parseInt()25.1.10 Serial.peek()25.1.11 Serial.print()25.1.12 Serial.println()25.1.13 Serial.read()25.1.14 Serial.readBytes()25.1.15 Serial.readBytesUntil()25.1.16 Serial.setTimeout()25.1.17 Serial.write()25.1.18 Serial.SerialEvent()25.2 Stream二十六、USB(仅适用于Leonardo 和Due)26.1 Mouse(键盘)26.2 Keyboard(鼠标)结构部分一、结构1.1 setup()在Arduino中程序运行时将首先调用setup() 函数。
arduino语言参考

Arduino程序可以分为三个主要部分:结构, 数值 (变量与常量) 和函数.结构∙setup()∙loop()控制结构∙if∙if...else∙for∙switch case∙while∙do... while∙break∙continue∙return∙goto进阶语法∙; (semicolon)∙{} (curly braces)∙// (single line comment)∙/* */ (multi-line comment)∙#define∙#include算术运算符∙= (assignment operator)∙+ (addition)∙- (subtraction)∙* (multiplication)∙/ (division)∙% (modulo)比较运算符∙== (equal to)∙!= (not equal to)∙< (less than)∙> (greater than)∙<= (less than or equal to) ∙>= (greater than or equal to)布尔运算符∙&& (and)∙|| (or)∙! (not)指针运算符∙* dereference operator∙& reference operator位运算符∙& (bitwise and)∙| (bitwise or)∙^ (bitwise xor)∙~ (bitwise not)∙<< (bitshift left)∙>> (bitshift right)符合运算符∙++ (increment)∙-- (decrement)∙+= (compound addition)∙-= (compound subtraction)∙*= (compound multiplication) ∙/= (compound division)∙&= (compound bitwise and) ∙|= (compound bitwise or)数值常量∙HIGH | LOW∙INPUT | OUTPUT|INPUT_PULLUP ∙true | false∙integer constants∙floating point constants数据类型∙void∙boolean∙char∙unsigned char∙byte∙int∙unsigned int∙word∙long∙unsigned long∙float∙double∙string - char array∙String - object∙array转换∙char()∙byte()∙int()∙word()∙long()∙float()变量范围与限定符∙variable scope∙static∙volatile∙constUtilities函数数字I/O∙pinMode()∙digitalWrite()∙digitalRead()模拟I/O∙analogReference() ∙analogRead()∙analogWrite() - PWM 高级I/O∙tone()∙noTone()∙shiftOut()∙shiftIn()∙pulseIn()时间∙millis()∙micros()∙delay()∙delayMicroseconds() 数学∙min()∙max()∙abs()∙constrain()∙map()∙pow()三角函数∙sin()∙cos()∙tan()随机数∙randomSeed()∙random()位与字节∙lowByte()∙highByte()∙bitRead()∙bitWrite()∙bitSet()∙bitClear()∙bit()外部中断∙attachInterrupt()∙detachInterrupt()中断∙interrupts()∙noInterrupts()通信∙Serial∙StreamUSB(仅适用于Leonardo、Due、Micro)∙Keyboard∙Mouse。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1 Language Reference 目录Arduino programs can be divided in three main parts: structure values variables and constants and functions. 一Structure setup loop 1.1Control Structures if if...else for switch case while do... while break continue return goto 1.2Further Syntax semicolon curly braces // single line comment / / multi-line comment define include 1.3Arithmetic Operators assignment operator addition - subtraction multiplication / division modulo 1.4Comparison Operators equal to not equal to lt less than gt greater than lt less than or equal to gt greater than or equal to 1.5Boolean Operators ampamp and2 or not 1.6Pointer Access Operators dereference operator amp reference operator 1.7 Bitwise Operators amp bitwise and bitwise or bitwise xor bitwise not ltlt bitshift left gtgt bitshift right 1.8 Compound Operators increment -- decrement compound addition - compound subtraction compound multiplication / compound division amp compound bitwise and compound bitwise or 二Variables 2.1Constants HIGH LOW INPUT OUTPUTINPUT_PULLUP true false integer constants floating point constants 2.2Data Types void boolean char unsigned char byte int unsigned int word long unsigned long float double string - char array String - object3 array 2.3 Conversion char byte int word long float 2.4 Variable Scopeamp Qualifiers variable scope static volatile const 2.5 Utilities sizeof 三Functions 3.1Digital I/O pinMode digitalWrite digitalRead 3.2 Analog I/O analogReference analogRead analogWrite - PWM 3.3 Advanced I/O tone noTone shiftOut shiftIn pulseIn 3.4 Time millis micros delay delayMicroseconds 3.5 Math min max abs constrain map pow sqrt 4 3.6 Trigonometry sin cos tan 3.7 Random Numbers randomSeed random 3.8 Bits and Bytes lowByte highByte bitRead bitWrite bitSet bitClear bit 3.9 External Interrupts attachInterrupt detachInterrupt 3.10 Interrupts interrupts noInterrupts 3.11 Communication Serial Stream 3.12 Leonardo Specific Keyboard Mouse Looking for something else See the libraries page for interfacing with particular types of hardware. Try the list of community-contributed code. The Arduino language is based on C/C. It links against AVR Libc and allows the use of any of its functions see its user manual for details. 5 Structure setup loop setup The setup function is called when a sketch starts. Use it to initialize variables pin modes start using libraries etc. The setup function will only run once after each powerup or reset of the Arduino board. Example int buttonPin 3 void setupSerial.begin9600 pinModebuttonPin INPUT void loop // ... loop After creating a setup function which initializes and sets theinitial values the loop function does precisely what its name suggests and loops consecutively allowing your program to change and respond. Use it to actively control the Arduino board. Example int buttonPin 3 // setup initializes serial and the button pin void setup beginSerial9600 pinModebuttonPin INPUT // loop checks the button pin each time // and will send serial if it is pressed 6 void loop if digitalReadbuttonPin HIGH serialWriteH else serialWriteL delay1000 Control Structures if if...else for switch case while do... while break continue return goto if conditional and lt gt comparison operators if which is used in conjunction with a comparison operator tests whether a certain condition has been reached such as an input being abovea certain number. The format for an if test is: if someVariable gt50 // do something here The program tests to see if someVariable is greater than 50. If it is the program takes a particular action. Put another way if the statement in parentheses is true the statements inside the brackets are run. If not the program skips over the code. 7 The brackets may be omitted after an if statement. If this is done the next line defined by the semicolon becomes the only conditional statement. if x gt 120 digitalWriteLEDpin HIGH if x gt 120 digitalWriteLEDpin HIGH if x gt 120 digitalWriteLEDpin HIGH if x gt 120digitalWriteLEDpin1 HIGH digitalWriteLEDpin2 HIGH // all are correct The statements being evaluated inside the parentheses require the use of one or more operators: Comparison Operators: x y x is equal to y x y x is not equal to y x lt y x is less than y x gt y x is greater than y x lt y x is less than or equal to y x gt y x is greater than or equal to y Warning: Beware of accidentally using the single equal sign e.g. if x 10 . The single equal sign is the assignment operator and sets x to 10 puts the value 10 into the variable x. Instead use the double equal sign e.g. if x 10 which is the comparison operator and tests whether x is equal to 10 or not. The latter statement is only true if x equals 10 but the former statement will always be true. This is because C evaluates the statement if x10 as follows: 10 is assigned to x remember that the single equal sign is the assignment operator so x now contains 10. Then the if conditional evaluates 10 which always evaluates to TRUE since any non-zero number evaluates to TRUE. Consequently if x 10 will always evaluate to TRUE which is not the desired result when using an if statement. Additionally the variable x will be set to 10 which is also not a desired action. if can also be part of a branching control structure using the if...else construction. Reference Home if / else if/else allows greater control over theflow of code than the basic if statement by allowing multiple tests to be grouped together. For example an analog input could be tested and one action taken if the input was less than 500 and another action taken if the input was 500 or greater. The code would look like this: 8 if pinFiveInput lt 500 // action A else // action B else can proceed another if test so that multiple mutually exclusive tests can be run at the same time. Each test will proceed to the next one until a true test is encountered. When a true test is found its associated block of code is run and the program then skips to the line following the entire if/else construction. If no test proves to be true the default else block is executed if one is present and sets the default behavior. Note that an else if block may be used with or without a terminating else block and vice versa. An unlimited number of such else if branches is allowed. if pinFiveInput lt 500 // do Thing A else if pinFiveInput gt 1000 // do Thing B else // do Thing C Another way to express branching mutually exclusive tests is with the switch case statement. for statements Desciption The for statement is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. The for statement is useful for any repetitive operation and is often used in combination with arraysto operate on collections of data/pins. There are three parts to the for loop header: for initialization condition increment//statements 9 The initialization happens first and exactly once. Each time through the loop the condition is tested if its true the statement block and the increment is executed then the condition is tested again. When the condition becomes false the loop ends. Example // Dim an LED using a PWM pin int PWMpin 10 // LED in series with 470 ohm resistor on pin 10 void setup // no setup needed void loop for int i0 i lt 255 i analogWritePWMpin i delay10 Coding Tips The C for loop is much more flexible than for loops found in some other computer languages including BASIC. Any or all of the three header elements may be omitted although the semicolons are required. Also the statements for initialization condition and increment can be any valid C statements with unrelated variables and use any C datatypes including floats. These types of unusual for statements may provide solutions to some rare programming problems. For example using a multiplication in the increment line will generate a logarithmic progression: forint x 2 x lt 100 x x 1.5 printlnx 10 Generates: 23469131928426394 Another example fade an LED up and down with one for loop: void loop int x 1 for int i 0 i gt -1 i i x analogWritePWMpin i if i 255 x -1 //switch direction at peak delay10 switch / case statements Like if statements switch...case controls the flow of programs by allowing programmers to specify different code that should be executed in various conditions. In particular a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable the code in that case statement is run. The break keyword exits the switch statement and is typically used at the end of each case. Without a break statement the switch statement will continue executing the following expressions quotfalling-throughquot until a break or the end of the switch statement is reached. Example switch var case 1: //do something when var equals 1 break case 2: //do something when var equals 2 break default: // if nothing else matches do the default // default is optional Syntax switch var case label: // statements break case label: // statements break default: 11 // statements Parameters var: the variable whose value to compare to the various cases label: a value to compare the variable to while loops Description while loops will loop continuously and infinitely until the expression inside the parenthesis becomes false. Something must change the tested variable or the while loop will never exit. This could be in your code such as anincremented variable or an external condition such as testing a sensor. Syntax whileexpression // statements Parameters expression - a boolean C statement that evaluates to true or false Example var 0 whilevar lt 200 // do something repetitive 200 times var do - while The do loop works in the same manner as the while loop with the exception that the condition is tested at the end of the loop so the do loop will always run at least once. do // statement block while test condition Example do delay50 // wait for sensors to stabilize x readSensors // check the sensors while x lt 100 12 break break is used to exit from a do for or while loop bypassing the normal loop condition. It is also used to exit from a switch statement. Example for x 0 x lt 255 x digitalWritePWMpin x sens analogReadsensorPin if sens gt threshold // bail out on sensor detect x 0 break delay50 continue The continue statement skips the rest of the current iteration of a loop do for or while. It continues by checking the conditional expression of the loop and proceeding with any subsequent iterations. Example for x 0 x lt 255 x if x gt 40 ampamp x lt 120 // create jump in values continue digitalWritePWMpin x delay50 return Terminate a function and return a value from a function to the calling function if desired. Syntax: return return value // both forms are valid Parameters value: any variable or constanttype Examples: A function to compare a sensor input to a threshold 13 int checkSensor if analogRead0 gt 400 return 1 else return 0 The return keyword is handy to test a section of code without having to quotcomment outquot large sections of possibly buggy code. void loop // brilliant code idea to test here return // the rest of a dysfunctional sketch here // this code will never be executed goto Transfers program flow to a labeled point in the program Syntax label: goto label // sends program flow to the label Tip The use of goto is discouraged in C programming and some authors of C programming books claim that the goto statement is never necessary but used judiciously it can simplify certain programs. The reason that many programmers frown upon the use of goto is that with the unrestrained use of goto statements it is easy to create a program with undefined program flow which can never be debugged. With that said there are instances where a goto statement can come in handy and simplify coding. One of these situations is to break out of deeply nested for loops or if logic blocks on a certain condition. Example forbyte r 0 r lt 255 r forbyte g 255 g gt -1 g-- forbyte b 0 b lt 255 b if analogRead0 gt 250 goto bailout // more statements ... bailout: 14 Further Syntax semicolon curly braces // single line comment / / multi-linecomment define include semicolon Used to end a statement. Example int a 13 Tip Forgetting to end a line in a semicolon will result in a compiler error. The error text may be obvious and refer to a missing semicolon or it may not. If an impenetrable or seemingly illogical compiler error comes up one of the first things to check is a missing semicolon in the immediate vicinity preceding the line at which the compiler complained Curly Braces Curly braces also referred to as just quotbracesquot or as quotcurly bracketsquot are a major part of the C programming language. They are used in several different constructs outlined below and this can sometimes be confusing for beginners. An opening curly brace quotquot must always be followed by a closing curly brace quotquot. This is a condition that is often referred to as the braces being balanced. The Arduino IDE integrated development environment includes a convenient feature to check the balance of curly braces. Just select a brace or even click the insertion point immediately following a brace and its logical companion will be highlighted. At present this feature is slightly buggy as the IDE will often find incorrectly a brace in text that has been quotcommented out.quot 15 Beginning programmers and programmers coming to C from the BASIC language often find using braces confusing or daunting.After all the same curly braces replace the RETURN statement in a subroutine function the ENDIF statement in a conditional and the NEXT statement in a FOR loop. Because the use of the curly brace is so varied it is good programming practice to type the closing brace immediately after typing the opening brace when inserting a construct which requires curly braces. Then insert some carriage returns between your braces and begin inserting statements. Your braces and your attitude will never become unbalanced. Unbalanced braces can often lead to cryptic impenetrable compiler errors that can sometimes be hard to track down in a large program. Because of their varied usages braces are also incredibly important to the syntax of a program and moving a brace one or two lines will often dramatically affect the meaning of a program. The main uses of curly braces Functions void myfunctiondatatype argument statementss Loops while boolean expression statements do statements while boolean expression for initialisation termination condition incrementing expr statements Conditional statements if boolean expression statements else if boolean expression statements else 16 statements Comments Comments are lines in the program that are used to inform yourself or others about the way the program works. The.。