“Joystick彩灯”的版本间的差异
853387039@qq.com(讨论 | 贡献) (Created page with "{{Language| Joystick彩灯}} {| style="width: 800px;" |- | ==目的== 通过Joystick摇杆来控制彩灯的颜色和亮度。 ==原理== ==设备== {|class="wikitable" |- |...") |
(→代码说明) |
||
(未显示3个用户的13个中间版本) | |||
第1行: | 第1行: | ||
− | |||
{| style="width: 800px;" | {| style="width: 800px;" | ||
|- | |- | ||
第7行: | 第6行: | ||
==原理== | ==原理== | ||
+ | 通过Joystick检测摇杆的状态,判断摇杆在哪个方向,然后在控制调节颜色和亮度。 | ||
+ | [[File: Joystick_colorled-sch.jpg|600px|center]] | ||
==设备== | ==设备== | ||
第13行: | 第14行: | ||
|模块||数量||功能 | |模块||数量||功能 | ||
|- | |- | ||
− | |[[ | + | |[[mCookie-CoreUSB/zh]]||1||核心板 |
|- | |- | ||
− | |[[ | + | |[[mCookie-Hub/zh]]||1||传感器转接板 |
|- | |- | ||
− | | [[Microduino- | + | |[[Microduino-Color led/zh]]||1||彩灯 |
|- | |- | ||
− | | [[Microduino-Joystick/zh]]||1||摇杆传感器 | + | |[[Microduino-Joystick/zh]]||1||摇杆传感器 |
|- | |- | ||
− | | [[ | + | |[[mCookie-BM/zh]]||1||电源管理 |
|} | |} | ||
− | [[File: Joystick_colorled.jpg|600px|center| | + | [[File: Joystick_colorled.jpg|600px|center]] |
+ | ==准备== | ||
+ | *Setup 1:将Color led背面的'''IN'''和Hub的数字口(D6)接起来,这个就是控制LED的引脚,用户可自己更改。 | ||
+ | [[file:mCookie-strandtext-sensor.JPG|600px|center]] | ||
+ | *Setup 2:再将Joystick接到A0/A1接口。 | ||
+ | [[file:mCookie-Joystick-led-sensor.JPG|600px|center]] | ||
+ | *Setup 3:将CoreUSB,Hub,Color led,Joystick连接在一起。通过USB数据线将接入电脑。 | ||
+ | [[file:mCookie-Joystick led-pc.JPG|600px|center]] | ||
+ | ==调试 == | ||
+ | *打开Arduino IDE,将下列代码复制到IDE中。 | ||
+ | <source lang="cpp"> | ||
+ | #include <Microduino_ColorLED.h> //引用彩灯库 | ||
+ | |||
+ | #define PIN 6 //彩灯引脚 | ||
+ | #define NUMPIXELS 6 //级联彩灯数量 | ||
+ | |||
+ | ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号 | ||
+ | |||
+ | |||
+ | uint32_t color_m[9][3] = | ||
+ | { | ||
+ | {0, 255, 255}, | ||
+ | {255, 0, 0}, | ||
+ | {248, 141, 30}, | ||
+ | {255, 255, 0}, | ||
+ | {0, 255, 0}, | ||
+ | {0, 127, 255}, | ||
+ | {0, 0, 255}, | ||
+ | {139, 0, 255}, | ||
+ | {255, 255, 255} | ||
+ | }; | ||
+ | uint32_t color[9] = | ||
+ | { | ||
+ | strip.Color(0, 0, 0), | ||
+ | strip.Color(255, 0, 0), | ||
+ | strip.Color(248, 141, 30), | ||
+ | strip.Color(255, 255, 0), | ||
+ | strip.Color(0, 255, 0), | ||
+ | strip.Color(0, 127, 255), | ||
+ | strip.Color(0, 0, 255), | ||
+ | strip.Color(139, 0, 255), | ||
+ | strip.Color(255, 255, 255) | ||
+ | }; | ||
+ | |||
+ | #define JoystickX_PIN A1 | ||
+ | #define JoystickY_PIN A0 | ||
+ | |||
+ | int sensorValueX, sensorValueY; | ||
+ | |||
+ | int num, color_l; | ||
+ | |||
+ | #define val_max 255 | ||
+ | #define val_min 0 | ||
+ | |||
+ | void setup() { | ||
+ | // put your setup code here, to run once: | ||
+ | Serial.begin(9600); | ||
+ | strip.begin(); | ||
+ | strip.show(); // Initialize all pixels to 'off' | ||
+ | |||
+ | pinMode(JoystickX_PIN, INPUT); | ||
+ | pinMode(JoystickY_PIN, INPUT); | ||
+ | |||
+ | for (int i = 0; i < 9; i++) | ||
+ | { | ||
+ | colorWipe(color[i]); | ||
+ | delay(300); | ||
+ | } | ||
− | + | colorWipe(color[0]); | |
− | + | } | |
− | * | + | void loop() { |
− | [[ | + | // put your main code here, to run repeatedly: |
− | + | sensorValueX = analogRead(JoystickX_PIN); | |
− | [[file: | + | sensorValueY = analogRead(JoystickY_PIN); |
− | [[ | + | |
+ | if (sensorValueY <= 10) | ||
+ | { | ||
+ | delay(500); | ||
+ | num++; | ||
+ | if (num > 8) | ||
+ | num = 0; | ||
+ | } | ||
+ | else if (sensorValueY > 800) | ||
+ | { | ||
+ | delay(500); | ||
+ | num--; | ||
+ | if (num < 0) | ||
+ | num = 8; | ||
+ | } | ||
+ | |||
+ | if (sensorValueX <= 10) | ||
+ | { | ||
+ | delay(10); | ||
+ | color_l++; | ||
+ | if (color_l > 255) | ||
+ | color_l = 255; | ||
+ | } | ||
+ | else if (sensorValueX > 800) | ||
+ | { | ||
+ | delay(10); | ||
+ | color_l--; | ||
+ | if (color_l < 0) | ||
+ | color_l = 0; | ||
+ | } | ||
+ | |||
+ | colorWipe(strip.Color(map(color_l, val_min, val_max, 0, color_m[num][0]), | ||
+ | map(color_l, val_min, val_max, 0, color_m[num][1]), | ||
+ | map(color_l, val_min, val_max, 0, color_m[num][2]))); | ||
+ | /* | ||
+ | Serial.print(num); | ||
+ | Serial.print("\t"); | ||
+ | Serial.println(color_l); | ||
+ | */ | ||
+ | } | ||
+ | |||
+ | void colorWipe(uint32_t c) { | ||
+ | for (uint16_t i = 0; i < strip.numPixels(); i++) { | ||
+ | strip.setPixelColor(i, c); | ||
+ | strip.show(); | ||
+ | } | ||
+ | }</source> | ||
+ | *选择正确的板卡和COM端口,编译通过后直接下载。 | ||
+ | [[file:upload.JPG|600px|center]] | ||
+ | [[file:uploadjoystick1.JPG|600px|center]] | ||
+ | [[file:uploadjoystick2.JPG|600px|center]] | ||
− | == | + | ==代码说明== |
− | + | * 颜色预设: “color_m[9][3]”:目前有9种颜色,用户可以更改。颜色数量需要和数组对应,如加了两个颜色,对应改成“color_m[11][3]”。 | |
− | * 颜色预设: “color_m[9][3] | ||
*Joystick控制引脚定义: | *Joystick控制引脚定义: | ||
<source lang="cpp"> | <source lang="cpp"> | ||
第44行: | 第161行: | ||
*X轴方向选择颜色 | *X轴方向选择颜色 | ||
<source lang="cpp"> | <source lang="cpp"> | ||
− | + | if (sensorValueY <= 10) | |
{ | { | ||
delay(500); | delay(500); | ||
第78行: | 第195行: | ||
*彩灯显示 | *彩灯显示 | ||
<source lang="cpp"> | <source lang="cpp"> | ||
− | colorWipe(strip.Color(map(color_l, val_min, val_max, 0, color_m[num][0]), map(color_l, val_min, val_max, 0, color_m[num][1]), map(color_l, val_min, val_max, 0, color_m[num][2]))); | + | colorWipe(strip.Color(map(color_l, val_min, val_max, 0, color_m[num][0]), |
+ | map(color_l, val_min, val_max, 0, color_m[num][1]), | ||
+ | map(color_l, val_min, val_max, 0, color_m[num][2]))); | ||
</source> | </source> | ||
==结果== | ==结果== | ||
− | + | 在X轴方向摇Joystick选择彩灯颜色,Y轴方向摇Joystick选择彩灯亮度。 | |
==视频== | ==视频== | ||
|} | |} |
2017年8月14日 (一) 07:11的最新版本
目的通过Joystick摇杆来控制彩灯的颜色和亮度。 原理通过Joystick检测摇杆的状态,判断摇杆在哪个方向,然后在控制调节颜色和亮度。 设备
准备
调试
#include <Microduino_ColorLED.h> //引用彩灯库
#define PIN 6 //彩灯引脚
#define NUMPIXELS 6 //级联彩灯数量
ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号
uint32_t color_m[9][3] =
{
{0, 255, 255},
{255, 0, 0},
{248, 141, 30},
{255, 255, 0},
{0, 255, 0},
{0, 127, 255},
{0, 0, 255},
{139, 0, 255},
{255, 255, 255}
};
uint32_t color[9] =
{
strip.Color(0, 0, 0),
strip.Color(255, 0, 0),
strip.Color(248, 141, 30),
strip.Color(255, 255, 0),
strip.Color(0, 255, 0),
strip.Color(0, 127, 255),
strip.Color(0, 0, 255),
strip.Color(139, 0, 255),
strip.Color(255, 255, 255)
};
#define JoystickX_PIN A1
#define JoystickY_PIN A0
int sensorValueX, sensorValueY;
int num, color_l;
#define val_max 255
#define val_min 0
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode(JoystickX_PIN, INPUT);
pinMode(JoystickY_PIN, INPUT);
for (int i = 0; i < 9; i++)
{
colorWipe(color[i]);
delay(300);
}
colorWipe(color[0]);
}
void loop() {
// put your main code here, to run repeatedly:
sensorValueX = analogRead(JoystickX_PIN);
sensorValueY = analogRead(JoystickY_PIN);
if (sensorValueY <= 10)
{
delay(500);
num++;
if (num > 8)
num = 0;
}
else if (sensorValueY > 800)
{
delay(500);
num--;
if (num < 0)
num = 8;
}
if (sensorValueX <= 10)
{
delay(10);
color_l++;
if (color_l > 255)
color_l = 255;
}
else if (sensorValueX > 800)
{
delay(10);
color_l--;
if (color_l < 0)
color_l = 0;
}
colorWipe(strip.Color(map(color_l, val_min, val_max, 0, color_m[num][0]),
map(color_l, val_min, val_max, 0, color_m[num][1]),
map(color_l, val_min, val_max, 0, color_m[num][2])));
/*
Serial.print(num);
Serial.print("\t");
Serial.println(color_l);
*/
}
void colorWipe(uint32_t c) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
}
}
代码说明
#define JoystickX_PIN A1
#define JoystickY_PIN A0
if (sensorValueY <= 10)
{
delay(500);
num++;
if (num > 8)
num = 0;
}
else if (sensorValueY > 800)
{
delay(500);
num--;
if (num < 0)
num = 8;
}
if (sensorValueX <= 10)
{
delay(10);
color_l++;
if (color_l > 255)
color_l = 255;
}
else if (sensorValueX > 800)
{
delay(10);
color_l--;
if (color_l < 0)
color_l = 0;
}
colorWipe(strip.Color(map(color_l, val_min, val_max, 0, color_m[num][0]),
map(color_l, val_min, val_max, 0, color_m[num][1]),
map(color_l, val_min, val_max, 0, color_m[num][2])));
结果在X轴方向摇Joystick选择彩灯颜色,Y轴方向摇Joystick选择彩灯亮度。 视频 |