“10K电位器”的版本间的差异
来自YFRobotwiki
(以“ 10K电位器 === 产品简介 === <br> 10K电位器 === 规格参数 === <br> *供电电压:DC5V *模块尺寸:29*2...”为内容创建页面) |
|||
| 第37行: | 第37行: | ||
'''示例代码''' | '''示例代码''' | ||
| + | <br><br> | ||
| + | <font color="darkcyan">'''示例一'''</font>:电位器值串口打印 | ||
<br> | <br> | ||
<pre > | <pre > | ||
| 第60行: | 第62行: | ||
} | } | ||
</pre> | </pre> | ||
| − | 程序下载地址:[http | + | 程序下载地址:[http://pan.baidu.com/s/1dDBFiRB PotentiometerTest] |
| − | 程序运行结果: | + | 程序运行结果: 电位器值串口打印,结果如下图: |
| + | |||
| + | [[File:电位器串口输出.png|center]] | ||
| + | |||
| + | |||
| + | <br><br> | ||
| + | <font color="darkcyan">'''示例二'''</font>:电位器控制[http://www.yfrobot.com/wiki/index.php?title=%E5%BD%A9%E8%89%B2LED%E6%A8%A1%E5%9D%97 彩色LED模块]变色 | ||
| + | <br> | ||
| + | <pre> | ||
| + | |||
| + | /* | ||
| + | Potentiometer control RGB LED change color | ||
| + | */ | ||
| + | |||
| + | #include <Adafruit_NeoPixel.h> | ||
| + | |||
| + | #define RGBLEDPin 5 | ||
| + | #define NUMPIXELS 4 | ||
| + | #define pPin A0 | ||
| + | |||
| + | // Parameter 1 = number of pixels in strip | ||
| + | // Parameter 2 = pin number (most are valid) | ||
| + | // Parameter 3 = pixel type flags, add together as needed: | ||
| + | // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) | ||
| + | // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) | ||
| + | // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) | ||
| + | // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) | ||
| + | Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, RGBLEDPin, NEO_GRB + NEO_KHZ800); | ||
| + | |||
| + | int pVal = 0; | ||
| + | int RVal = 0; | ||
| + | int GVal = 0; | ||
| + | int BVal = 0; | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | pinMode(RGBLEDPin, OUTPUT); | ||
| + | pixels.begin(); // This initializes the NeoPixel library. | ||
| + | pixels.show(); | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | pVal = analogRead(pPin); //读取A0模拟口的数值(0-5V 对应 0-1203取值) | ||
| + | if (pVal >= 0 && pVal <= 170) { | ||
| + | RVal = 255; | ||
| + | GVal = 0; | ||
| + | BVal = map(pVal, 0, 170, 0, 255); | ||
| + | } else if (pVal > 170 && pVal <= 340) { | ||
| + | RVal = map(pVal, 170, 340, 255, 0); | ||
| + | GVal = 0; | ||
| + | BVal = 255; | ||
| + | } else if (pVal > 340 && pVal <= 510) { | ||
| + | RVal = 0; | ||
| + | GVal = map(pVal, 340, 510, 0, 255); | ||
| + | BVal = 255; | ||
| + | } else if (pVal > 510 && pVal <= 680) { | ||
| + | RVal = 0; | ||
| + | GVal = 255; | ||
| + | BVal = map(pVal, 510, 680, 255, 0); | ||
| + | } else if (pVal > 680 && pVal <= 850) { | ||
| + | RVal = map(pVal, 680, 850, 0, 255); | ||
| + | GVal = 255; | ||
| + | BVal = 0; | ||
| + | } else { | ||
| + | RVal = 255; | ||
| + | GVal = map(pVal, 850, 1023, 255, 0); | ||
| + | BVal = 0; | ||
| + | } | ||
| + | lightLED(RVal, GVal, BVal); | ||
| + | } | ||
| + | |||
| + | void lightLED(int r, int g , int b) { | ||
| + | // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one. | ||
| + | for (int i = 0; i < NUMPIXELS; i++) { | ||
| + | // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 | ||
| + | pixels.setPixelColor(i, r, g, b); // Moderately bright green color. | ||
| + | pixels.show(); // This sends the updated pixel color to the hardware. | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </pre> | ||
| + | |||
| + | 程序下载地址:[http://pan.baidu.com/s/1o6EK41W ChangeColor] | ||
| + | |||
| + | 程序运行结果:转动电位器,彩色led变色。 | ||
| 第69行: | 第156行: | ||
===参考资料=== | ===参考资料=== | ||
<br> | <br> | ||
| − | |||
| − | |||
--> | --> | ||
2015年10月12日 (一) 13:17的版本
产品简介
10K电位器
规格参数
- 供电电压:DC5V
- 模块尺寸:29*20*35MM(长*宽*高)
- 模块重量:13.0g
引脚说明
- 1. G 黑线 -- Gnd(地)
- 2. V 红线 -- Vcc(电源+5V)
- 3. S 白线 -- 信号
应用示例
电路连接示意图
- 摇杆模块的 G、V、S分别连接 Arduino UNO的GND、VCC(+5V)、A0引脚。
示例代码
示例一:电位器值串口打印
/*
Potentiometer Test
*/
int pPin = A0;
int pVal = 0;
int pVal_old = 0;
void setup()
{
pinMode(pPin,INPUT);
Serial.begin(9600);
}
void loop()
{
//读取A0模拟口的数值(0-5V 对应 0-1204取值)
pVal = analogRead(pPin);
if(abs(pVal - pVal_old) >= 2 ){
Serial.println(pVal);
pVal_old = pVal;
}
}
程序下载地址:PotentiometerTest
程序运行结果:电位器值串口打印,结果如下图:
示例二:电位器控制彩色LED模块变色
/*
Potentiometer control RGB LED change color
*/
#include <Adafruit_NeoPixel.h>
#define RGBLEDPin 5
#define NUMPIXELS 4
#define pPin A0
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, RGBLEDPin, NEO_GRB + NEO_KHZ800);
int pVal = 0;
int RVal = 0;
int GVal = 0;
int BVal = 0;
void setup()
{
pinMode(RGBLEDPin, OUTPUT);
pixels.begin(); // This initializes the NeoPixel library.
pixels.show();
}
void loop()
{
pVal = analogRead(pPin); //读取A0模拟口的数值(0-5V 对应 0-1203取值)
if (pVal >= 0 && pVal <= 170) {
RVal = 255;
GVal = 0;
BVal = map(pVal, 0, 170, 0, 255);
} else if (pVal > 170 && pVal <= 340) {
RVal = map(pVal, 170, 340, 255, 0);
GVal = 0;
BVal = 255;
} else if (pVal > 340 && pVal <= 510) {
RVal = 0;
GVal = map(pVal, 340, 510, 0, 255);
BVal = 255;
} else if (pVal > 510 && pVal <= 680) {
RVal = 0;
GVal = 255;
BVal = map(pVal, 510, 680, 255, 0);
} else if (pVal > 680 && pVal <= 850) {
RVal = map(pVal, 680, 850, 0, 255);
GVal = 255;
BVal = 0;
} else {
RVal = 255;
GVal = map(pVal, 850, 1023, 255, 0);
BVal = 0;
}
lightLED(RVal, GVal, BVal);
}
void lightLED(int r, int g , int b) {
// For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
for (int i = 0; i < NUMPIXELS; i++) {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, r, g, b); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
}
}
程序下载地址:ChangeColor
程序运行结果:转动电位器,彩色led变色。
更多建议和问题欢迎反馈至 YFRobot论坛
购买方式:YFRobot 电子工作室
