超声波模块如何控制LED的闪烁频率以及亮度···-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 8185|回复: 5

[已解决] 超声波模块如何控制LED的闪烁频率以及亮度···

[复制链接]
发表于 2012-4-24 21:19 | 显示全部楼层 |阅读模式
在奈何老师的帮助下通过电位器可以实现了···
const int analogInPin = A0;  // Analog input pin that the potentiometeris attached to
const int analogOutPin = 9; // Analogoutput pin that the LED is attached to
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analogout)
void setup() {
  //initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop()
{
sensorValue = analogRead(analogInPin);            
outputValue = map(sensorValue, 0, 1023, 0, 255);  
analogWrite(analogOutPin, outputValue);
delay(sensorValue);
analogWrite(analogOutPin, 0);
    delay(sensorValue);
}
超声波模块反馈的是一个电平 ···
如何用超声波模块替换掉电位器实现呢???


发表于 2012-4-24 21:22 | 显示全部楼层
传送门:http://www.arduino.cn/thread-1003-1-1.html


另外不要叫老师额~~~
 楼主| 发表于 2012-4-24 22:10 | 显示全部楼层
#include "SR04.h"
#define TRIG_PIN 2
#define ECHO_PIN 3
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}
void loop()
{
  sensorValue = sr04.Distance();            
  outputValue = map(sensorValue, 0, 1023, 0, 255);  
  analogWrite(analogOutPin, outputValue);
  delay(sensorValue);
  analogWrite(analogOutPin, 0);
    delay(sensorValue);
}
实现···偶也···
发表于 2012-4-24 22:36 | 显示全部楼层
切猫猫 发表于 2012-4-24 22:10
#include "SR04.h"
#define TRIG_PIN 2
#define ECHO_PIN 3

 楼主| 发表于 2012-4-24 22:43 | 显示全部楼层
哈哈 ··你懂的··哈哈····从你给的那个连接里面找到的例子  直接改的··哈哈
 楼主| 发表于 2012-4-24 22:44 | 显示全部楼层
#include "SR04.h"
#define TRIG_PIN 2
#define ECHO_PIN 3
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}
void loop()
{
  sensorValue = sr04.Distance();            
  outputValue = map(sensorValue, 0, 1023, 0, 255);
if(sensorValue<20){
analogWrite(analogOutPin, 0);
}
if(sensorValue>20)
{
analogWrite(analogOutPin, outputValue);
  delay(500);
  analogWrite(analogOutPin, 0);
delay(500);
analogWrite(analogOutPin,outputValue);
  delay(500);
  analogWrite(analogOutPin, 0);
delay(500);
analogWrite(analogOutPin, outputValue);
  delay(500);
  analogWrite(analogOutPin, 0);
    delay(sensorValue);
}
}
接着又改成了这样··哈哈 第一次写IF语句··哈哈····开心咯···
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-30 23:55 , Processed in 0.100416 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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