前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Arduino初体验

Arduino初体验

作者头像
luxuantao
发布2021-02-24 11:22:20
5370
发布2021-02-24 11:22:20
举报
文章被收录于专栏:Fdu弟中弟Fdu弟中弟

自从退了问题求解之后,感觉一身轻松,每天只有一个字:“闲”。这两天大佬们都走上了ACM远征的道路,留下我们这批小佬暗自神伤。不过还算好,Arduino这个新伙伴让我开心了几天。

小灯+蜂鸣器

代码语言:javascript
复制
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(3, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  Serial.println("ok");
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
  
  long frequency = 300; //频率, 单位Hz  
   //用tone()函数发出频率为frequency的波形  
   tone(3, frequency );  
   delay(1000); //等待1000毫秒  
   noTone(3);//停止发声  
   delay(2000); //等待2000毫秒  
}

舵机自动旋转

代码语言:javascript
复制
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
int pos = 0;    // variable to store the servo position 

void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

rotation sensor控制舵机

代码语言:javascript
复制
#include <Servo.h> // 声明调用Servo.h库
Servo myservo; // 创建一个舵机对象
int potpin = 0; // 连接到模拟口0
int val; //变量val用来存储从模拟口0读到的值

void setup() {
Serial.begin(9600);
 myservo.attach(9); //将引脚9上的舵机与声明的舵机对象连接起来
}

void loop() {
 val = analogRead(potpin); //从模拟口0读值,并通过val记录
 val = map(val, 0, 1023, 0, 179); //通过map函数进行数值转换
 myservo.write(val); // 给舵机写入角度
 Serial.println("OK");
 delay(15); // 延时15ms让舵机转到指定位置
}

PS:md竟然不支持Arduino语法。。我用的c++实现高亮。。

​ 在此特别鸣谢两位白羊座小朋友提供的技术支持!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-10-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档