Arduino 基本語法筆記
++x;x++;
if else 敘述
int x=20;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if ((x%2)==0)
{
Serial.println("1");
}
else
{
Serial.println("2");
}
===========================
if
else if
int x=20;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if (x>=90)
{
Serial.println("segament 1");
}
else if (x>=80)
{
Serial.println("segament 2");
}
else if (x>=70)
{
Serial.println("segament 3");
}
else if (x>=600)
{
Serial.println("segament 4");
}
else
{
Serial.println("segament 5");
}
}
=======================
for迴圈
語法
for(開始值;終止值;增量值)
{
[循環區塊]
}
int total=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i=1;i<10;i++)
{
for(int y=1;y<10;y++)
{
total=i*y;
Serial.print(i);
Serial.print("*");
Serial.print(y);
Serial.print("=");
Serial.print(total);
Serial.print(" ");
}
Serial.println();
}
}
=========================
正向
void setup() {
// put your setup code here, to run once:
for(int i=7;i<=13;i++)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=7;j<=13;j++)
{
digitalWrite(j,HIGH);
delay(3000);
}
}
===================
逆向
void setup() {
// put your setup code here, to run once:
for(int i=13;i>=1;i--)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=13;j>=1;j--)
{
digitalWrite(j,HIGH);
delay(3000);
}
}
============================
void setup() {
// put your setup code here, to run once:
for(int i=13;i>=1;i--)
{
pinMode(i,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for(int j=13;j>=1;j--)
{
digitalWrite(j,HIGH);
delay(100);
digitalWrite(j,LOW);
}
for(int j=7;j<=13;j++)
{
digitalWrite(j,HIGH);
delay(100);
digitalWrite(j,LOW);
}
}
===========
anoloaRead(變數) A0-A5/PWM ~D3
anolofWrite(接腳,類比值)
const int va=A2;
const int led=9;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
int sv=analogRead(va);
analogWrite(led,sv/4);
delay(150);
}
沒有留言:
張貼留言