初识arduino

我是第一次接触arduino。然后以日志的形式记录学习arduino的过程和遇到的问题。

首先是在windows上安装IDE(集成开发环境),在这款软件上编程需要使用arduino的语言。在arduino的官方网站上可以下载这款官方设计的软件及源码,教程和文档。那么下载好了之后就开始学习过程。首先呢会看到这样的代码。他没有实际内容,是一个框架。

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}


arduino程序的框架分为三部分。

1.声明变量及接口名称

2.setup()。在aduino程序运行时首先调用setup()函数,用于初始化变量,设置引脚的输入输出类型,配置串口,引入类库文件等。

3.loop()。在setup()函数中初始化和定义变量,然后执行loop()函数。该函数在程序运行过程中不断循环,根据反馈相应的改变执行情况。通过该函数动态控制arduino主控板。

我编写的第一个程序是闪灯程序。

int delayTime=1000;
void setup() {
  // put your setup code here, to run once:
  pinMode(13,OUTPUT);//将13号引脚设置为输出引脚
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(13,HIGH);//点亮小灯
  delay(delayTime);//延时
  digitalWrite(13,LOW);//熄灭小灯
  delay(delayTime);
  delayTime=delayTime-100;//小灯亮灯时间越来越短
  if(delayTime<100)
  {
    delayTime=1000;//当时间不足100ms时重置延时时间为1000ms,loop循环
    }
}
那么对arduino编程语言有了初步了解之后,我学习了arduino的输入输出函数。
1.pinMode(pin, mode)

将数位脚位(digital pin)指定为输入或输出。

例如

pinMode(7,INPUT); // 将脚位 7 设定为输入模式

2.Serial.begin();  

开启串口,通常置于setup()函数中。

Serial.begin(speed);  

Serial.begin(speed,config);  

speed: 波特率,一般取值 9600.

那么后面的我要引用一部分奈何cal的博文了,里面的资料非常详尽,实际上关于arduino的资料网上已经太多的可以看。

3.read和peek输入方式的差异 串口接收到的数据都会暂时存放在接收缓冲区中,使用read()与peek()都是从接收缓冲区中读取数据。不同的是,使用read()读取数据后,会将该数据从接收缓冲区移除;而使用peek()读取时,不会移除接收缓冲区中的数据。

01
02
03
04
05
06
07
08
09
10
11
12
13
char col;
void setup() {
   Serial.begin(9600);
}
 
void loop() {
   while (Serial.available()>0){
     col=Serial.read();
     Serial.print( "Read: " );
     Serial.println(col);
     delay(1000);
   }
}
4.输出不同进制的文本 我们可以是用 Serial.print(val, format)的形式输出不同进制的文本 参数val 是需要输出的数据 参数format 是需要输出的进制形式,你可以使用如下参数: BIN(二进制) DEC(十进制) OCT(八进制) HEX(十六进制) 例如,使用Serial.print(123,BIN),你可以在串口调试器上看到1111011 使用Serial.print(123,HEX),你可以在串口调试器上看到7B
在查找资料和学习了函数知识之后,我开始了对Servo库的学习,它主要应用于会舵机的控制上。
servo类下有以下部分成员函数 attach()//连接舵机 write()//角度控制 read()//读取舵机转动角度
而官方给的例程呢是这样的程序
#include <Servo.h>

 Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
   val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
   val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
   myservo.write(val);                  // sets the servo position according to the scaled value
   delay(15);                           // waits for the servo to get there
}
奈何cal博主有一份很好的代码是这样的,
#include <Servo.h> 
Servo myservo;  //创建一个舵机控制对象
int pos = 0;    // 该变量用与存储舵机角度位置
void setup() 
{ 
  myservo.attach(9);  // 该舵机由arduino第九脚控制
} 
void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // 从0度到180度运动 
  {                               // 每次步进一度
    myservo.write(pos);        // 指定舵机转向的角度
    delay(15);                       // 等待15ms让舵机到达指定位置
  } 
  for(pos = 180; pos>=1; pos-=1)   //从180度到0度运动  
  {                                
    myservo.write(pos);         // 指定舵机转向的角度 
    delay(15);                        // 等待15ms让舵机到达指定位置 
  } 
}

我的学习过程是将这每一份代码都烧进去一次,通过观察舵机的变化来理解。最后我也有编一份代码。

char col,a='a',b='b';
#include<Servo.h>
Servo myServo;
void setup() { 
  Serial.begin(9600); 
  myServo.attach(9);
} 
 
void loop() { 
  while(Serial.available()>0){
    col=Serial.read();
    if(col==a)
   { myServo.write(180);
    Serial.print("Read: "); 
Serial.println(col);
delay(1000);}
    else if(col==b)
    {myServo.write(90);
    Serial.print("Read: "); 
    Serial.println(col);
    delay(1000);}
  } 
}

这份代码的功能是在串口监视器输入一串字母a和b,之后电脑会挨个读入和判断舵机旋转的角度。

要注意的是需要将变量a,b,定义成字符,我在这里就有过出错。







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值