楼主: 驴友花雕
|
[教程] 一块扩展板完成Arduino的10类37项实验(代码+图形+仿真) |
07 RS232_AD:串口读取 A0 电位器采集的数值 [mw_shl_code=applescript,true]/* Eagler8实验程序列表 07 RS232_AD:串口读取 A0 电位器采集的数值(旋转角度270°,输出0-3.3V/5V电压信号,电位器阻值10K) */ void setup() { Serial.begin(9600); // 打开串口,设置波特率为9600 bps } void loop() { int val; val=analogRead(0); //传感器接于模拟口0 Serial.println(val,DEC);//从串口发送数据并换行 delay(100); }[/mw_shl_code] |
09 RC_Motor:A0 电位器控制 D7 输出口的舵机,控制其转动角度(舵机需自备,Arduino 需要外部供电) [mw_shl_code=c,true]/* Eagler8实验程序列表 09 RC_Motor:A0 电位器控制 D7 输出口的舵机,控制其转动角度(舵机需自备,Arduino 需要外部供电) */ #include <Servo.h> Servo myservo; int analogPin = 0; int val; void setup() { myservo.attach(7); // 7号引脚输出电机控制信号 Serial.begin(9600); } void loop() { Serial.print("servo:"); Serial.println(val); delay(15); val = analogRead(analogPin); // 读取来自可变电阻的模拟值(0到1023之间) val = map(val, 0, 1023, 0, 179); // 利用“map”函数缩放该值,得到伺服电机需要的角度(0到180之间) myservo.write(val); // 设定伺服电机的位置 delay(15); // 等待电机旋转到目标角度 }[/mw_shl_code] |
10 LED_RGB_Text:全彩 LED 基本颜色变化 [mw_shl_code=c,true]/* Eagler8实验程序列表 10 LED_RGB_Text:全彩 LED 基本颜色变化 */ #define LEDR 9 #define LEDG 10 #define LEDB 11 int i = 0; void setup() { pinMode(LEDG,OUTPUT); pinMode(LEDB,OUTPUT); pinMode(LEDR,OUTPUT); } void loop() { analogWrite(LEDR,0); analogWrite(LEDB,0); analogWrite(LEDG,0); //off analogWrite(LEDR,255); delay(500); //hong analogWrite(LEDG,100); delay(500); //cheng analogWrite(LEDR,255); analogWrite(LEDG,255); delay(500); //huang analogWrite(LEDR,0); analogWrite(LEDG,255); delay(500); //lv analogWrite(LEDB,255); delay(500); //qing analogWrite(LEDG,0); analogWrite(LEDB,255); delay(500); //lan analogWrite(LEDR,255); analogWrite(LEDG,0); delay(500); //zi analogWrite(LEDG,255); analogWrite(LEDB,255); analogWrite(LEDR,255); delay(500); //bai }[/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed