九鼎创展论坛中文版English
登录 | 立即注册 设为首页收藏本站 切换到宽版
查看: 4390|回复: 0

arduino ibox项目实战39-IfStatementConditional实验

[复制链接]
发表于 2014-10-29 19:42:11 | 显示全部楼层 |阅读模式
硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个,LED灯一个,电位器一个
软件平台:WIN7操作系统,android4.0android4.4系统
实验目标:通过电位器控制LED灯的亮与灭
打开arduino的IDE开发工具,依次点击文件->示例-> 05.Control->IfStatementConditional,IfStatementConditional的示例程序将会被打开,其源码如下:
  1. // These constants won't change:
  2. const int analogPin = A0;    // pin that the sensor is attached to
  3. const int ledPin = IO0;       // pin that the LED is attached to
  4. const int threshold = 1000;   // an arbitrary threshold level that's in the range of the analog input

  5. void setup() {
  6.   // initialize the LED pin as an output:
  7.   pinMode(ledPin, OUTPUT);
  8.   // initialize serial communications:
  9.   Serial.begin(9600);
  10. }

  11. void loop() {
  12.   // read the value of the potentiometer:
  13.   int analogValue = analogRead(analogPin);

  14.   // if the analog value is high enough, turn on the LED:
  15.   if (analogValue > threshold) {
  16.     digitalWrite(ledPin, HIGH);
  17.   }
  18.   else {
  19.     digitalWrite(ledPin, LOW);
  20.   }

  21.   // print the analog value:
  22.   Serial.println(analogValue);
  23.   delay(10);        // delay in between reads for stability
  24. }
复制代码
       从程序可以看出,程序期望通过从A0口读取ADC的值,当读取的值大于1000时,点亮LED,小于1000时熄灭LEDLED灯由IO0控制。将LED灯的正级接IO0,负级接GND,将电位器的两端接3.3VGND,中间接A0,运行程序,钮动电位器,观察LED灯是否受电位器控制。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|深圳市九鼎创展科技官方论坛 ( 粤ICP备11028681号-2  

GMT+8, 2024-4-17 05:37 , Processed in 0.020307 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表