LEDストリップをArduinoで動かす
ArduinoにLEDのたくさんついたLEDストリップをつけます。端子は+6V、G、R、Bの4端子があるタイプです。+6Vに5Vをつなぎ、ほかはPWMのできるデジタルピンにつなぎます。PWMは255の時、LEDが消灯し0の時点灯します。
また、今回はポテンショメータを使い、点滅の周期と色味をコントロールしてみました。
Arduino
また、今回はポテンショメータを使い、点滅の周期と色味をコントロールしてみました。
動作の様子
BlocklyDuino
ソース
Blocklyduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<xml xmlns="http://www.w3.org/1999/xhtml"><block type="initializes_setup" id="110" x="63" y="38"><statement name="CONTENT"><block type="variables_set" id="131" inline="true"><field name="TYPE">Number</field><field name="VAR">Delay_time</field><value name="VALUE"><block type="math_number" id="135"><field name="NUM">10000</field></block></value><next><block type="variables_set" id="180" inline="true"><field name="TYPE">Number</field><field name="VAR">HSV</field><value name="VALUE"><block type="math_number" id="181"><field name="NUM">360</field></block></value><next><block type="variables_set" id="204" inline="true"><field name="TYPE">Number</field><field name="VAR">color_Width</field><value name="VALUE"><block type="math_number" id="205"><field name="NUM">120</field></block></value><next><block type="variables_set" id="225" inline="true"><field name="TYPE">Number</field><field name="VAR">BluePoint</field><value name="VALUE"><block type="math_number" id="240"><field name="NUM">240</field></block></value><next><block type="variables_set" id="238" inline="true"><field name="TYPE">Number</field><field name="VAR">GreenPoint</field><value name="VALUE"><block type="math_number" id="241"><field name="NUM">120</field></block></value><next><block type="variables_set" id="257" inline="true"><field name="TYPE">Number</field><field name="VAR">RedPoint</field><value name="VALUE"><block type="math_number" id="258"><field name="NUM">0</field></block></value><next><block type="variables_set" id="277" inline="true"><field name="TYPE">Number</field><field name="VAR">RedPoint2</field><value name="VALUE"><block type="math_number" id="278"><field name="NUM">360</field></block></value><next><block type="variables_set" id="589" inline="true"><field name="TYPE">Number</field><field name="VAR">Bv</field><value name="VALUE"><block type="math_number" id="590"><field name="NUM">255</field></block></value><next><block type="variables_set" id="690" inline="true"><field name="TYPE">Number</field><field name="VAR">Gv</field><value name="VALUE"><block type="math_number" id="691"><field name="NUM">255</field></block></value><next><block type="variables_set" id="734" inline="true"><field name="TYPE">Number</field><field name="VAR">Rv</field><value name="VALUE"><block type="math_number" id="735"><field name="NUM">255</field></block></value></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></statement><next><block type="controls_while" id="55" inline="false"><value name="BOOL"><block type="logic_boolean" id="56"><field name="BOOL">TRUE</field></block></value><statement name="DO"><block type="procedures_callnoreturn" id="184"><mutation name="get_Analog_value"></mutation><next><block type="controls_if" id="415" inline="false"><mutation else="1"></mutation><value name="IF0"><block type="logic_compare" id="507" inline="true"><field name="OP">LTE</field><value name="A"><block type="math_single" id="416" inline="true"><field name="OP">ABS</field><value name="NUM"><block type="math_arithmetic" id="417" inline="true"><field name="OP">MINUS</field><value name="A"><block type="variables_get" id="418"><field name="TYPE">Number</field><field name="VAR">BluePoint</field></block></value><value name="B"><block type="variables_get" id="419"><field name="TYPE">Number</field><field name="VAR">HSV</field></block></value></block></value></block></value><value name="B"><block type="variables_get" id="526"><field name="TYPE">Number</field><field name="VAR">color_Width</field></block></value></block></value><statement name="DO0"><block type="variables_set" id="612" inline="true"><field name="TYPE">Number</field><field name="VAR">Bv</field><value name="VALUE"><block type="math_arithmetic" id="379" inline="true"><field name="OP">MULTIPLY</field><value name="A"><block type="math_single" id="300" inline="true"><field name="OP">ABS</field><value name="NUM"><block type="math_arithmetic" id="328" inline="true"><field name="OP">MINUS</field><value name="A"><block type="variables_get" id="347"><field name="TYPE">Number</field><field name="VAR">BluePoint</field></block></value><value name="B"><block type="variables_get" id="366"><field name="TYPE">Number</field><field name="VAR">HSV</field></block></value></block></value></block></value><value name="B"><block type="math_arithmetic" id="389" inline="true"><field name="OP">DIVIDE</field><value name="A"><block type="math_number" id="390"><field name="NUM">255</field></block></value><value name="B"><block type="variables_get" id="409"><field name="TYPE">Number</field><field name="VAR">color_Width</field></block></value></block></value></block></value></block></statement><statement name="ELSE"><block type="variables_set" id="613" inline="true"><field name="TYPE">Number</field><field name="VAR">Bv</field><value name="VALUE"><block type="math_number" id="622"><field name="NUM">255</field></block></value></block></statement><next><block type="controls_if" id="652" inline="false"><mutation else="1"></mutation><value name="IF0"><block type="logic_compare" id="653" inline="true"><field name="OP">LTE</field><value name="A"><block type="math_single" id="654" inline="true"><field name="OP">ABS</field><value name="NUM"><block type="math_arithmetic" id="655" inline="true"><field name="OP">MINUS</field><value name="A"><block type="variables_get" id="656"><field name="TYPE">Number</field><field name="VAR">GreenPoint</field></block></value><value name="B"><block type="variables_get" id="657"><field name="TYPE">Number</field><field name="VAR">HSV</field></block></value></block></value></block></value><value name="B"><block type="variables_get" id="658"><field name="TYPE">Number</field><field name="VAR">color_Width</field></block></value></block></value><statement name="DO0"><block type="variables_set" id="659" inline="true"><field name="TYPE">Number</field><field name="VAR">Gv</field><value name="VALUE"><block type="math_arithmetic" id="660" inline="true"><field name="OP">MULTIPLY</field><value name="A"><block type="math_single" id="661" inline="true"><field name="OP">ABS</field><value name="NUM"><block type="math_arithmetic" id="662" inline="true"><field name="OP">MINUS</field><value name="A"><block type="variables_get" id="663"><field name="TYPE">Number</field><field name="VAR">GreenPoint</field></block></value><value name="B"><block type="variables_get" id="664"><field name="TYPE">Number</field><field name="VAR">HSV</field></block></value></block></value></block></value><value name="B"><block type="math_arithmetic" id="665" inline="true"><field name="OP">DIVIDE</field><value name="A"><block type="math_number" id="666"><field name="NUM">255</field></block></value><value name="B"><block type="variables_get" id="667"><field name="TYPE">Number</field><field name="VAR">color_Width</field></block></value></block></value></block></value></block></statement><statement name="ELSE"><block type="variables_set" id="668" inline="true"><field name="TYPE">Number</field><field name="VAR">Gv</field><value name="VALUE"><block type="math_number" id="669"><field name="NUM">255</field></block></value></block></statement><next><block type="controls_if" id="736" inline="false"><mutation else="1"></mutation><value name="IF0"><block type="logic_compare" id="737" inline="true"><field name="OP">LTE</field><value name="A"><block type="math_single" id="738" inline="true"><field name="OP">ABS</field><value name="NUM"><block type="math_arithmetic" id="739" inline="true"><field name="OP">MINUS</field><value name="A"><block type="variables_get" id="740"><field name="TYPE">Number</field><field name="VAR">RedPoint</field></block></value><value name="B"><block type="variables_get" id="741"><field name="TYPE">Number</field><field name="VAR">HSV</field></block></value></block></value></block></value><value name="B"><block type="variables_get" id="742"><field name="TYPE">Number</field><field name="VAR">color_Width</field></block></value></block></value><statement name="DO0"><block type="variables_set" id="743" inline="true"><field name="TYPE">Number</field><field name="VAR">Rv</field><value name="VALUE"><block type="math_arithmetic" id="744" inline="true"><field name="OP">MULTIPLY</field><value name="A"><block type="math_single" id="745" inline="true"><field name="OP">ABS</field><value name="NUM"><block type="math_arithmetic" id="746" inline="true"><field name="OP">MINUS</field><value name="A"><block type="variables_get" id="747"><field name="TYPE">Number</field><field name="VAR">RedPoint</field></block></value><value name="B"><block type="variables_get" id="748"><field name="TYPE">Number</field><field name="VAR">HSV</field></block></value></block></value></block></value><value name="B"><block type="math_arithmetic" id="749" inline="true"><field name="OP">DIVIDE</field><value name="A"><block type="math_number" id="750"><field name="NUM">255</field></block></value><value name="B"><block type="variables_get" id="751"><field name="TYPE">Number</field><field name="VAR">color_Width</field></block></value></block></value></block></value></block></statement><statement name="ELSE"><block type="controls_if" id="754" inline="false"><mutation else="1"></mutation><value name="IF0"><block type="logic_compare" id="755" inline="true"><field name="OP">LTE</field><value name="A"><block type="math_single" id="756" inline="true"><field name="OP">ABS</field><value name="NUM"><block type="math_arithmetic" id="757" inline="true"><field name="OP">MINUS</field><value name="A"><block type="variables_get" id="758"><field name="TYPE">Number</field><field name="VAR">RedPoint2</field></block></value><value name="B"><block type="variables_get" id="759"><field name="TYPE">Number</field><field name="VAR">HSV</field></block></value></block></value></block></value><value name="B"><block type="variables_get" id="760"><field name="TYPE">Number</field><field name="VAR">color_Width</field></block></value></block></value><statement name="DO0"><block type="variables_set" id="761" inline="true"><field name="TYPE">Number</field><field name="VAR">Rv</field><value name="VALUE"><block type="math_arithmetic" id="762" inline="true"><field name="OP">MULTIPLY</field><value name="A"><block type="math_single" id="763" inline="true"><field name="OP">ABS</field><value name="NUM"><block type="math_arithmetic" id="764" inline="true"><field name="OP">MINUS</field><value name="A"><block type="variables_get" id="765"><field name="TYPE">Number</field><field name="VAR">RedPoint2</field></block></value><value name="B"><block type="variables_get" id="766"><field name="TYPE">Number</field><field name="VAR">HSV</field></block></value></block></value></block></value><value name="B"><block type="math_arithmetic" id="767" inline="true"><field name="OP">DIVIDE</field><value name="A"><block type="math_number" id="768"><field name="NUM">255</field></block></value><value name="B"><block type="variables_get" id="769"><field name="TYPE">Number</field><field name="VAR">color_Width</field></block></value></block></value></block></value></block></statement><statement name="ELSE"><block type="variables_set" id="770" inline="true"><field name="TYPE">Number</field><field name="VAR">Rv</field><value name="VALUE"><block type="math_number" id="771"><field name="NUM">255</field></block></value></block></statement></block></statement><next><block type="inout_analog_write" id="289" inline="true"><field name="PIN">6</field><value name="NUM"><block type="variables_get" id="641"><field name="TYPE">Number</field><field name="VAR">Bv</field></block></value><next><block type="inout_analog_write" id="772" inline="true"><field name="PIN">3</field><value name="NUM"><block type="variables_get" id="773"><field name="TYPE">Number</field><field name="VAR">Gv</field></block></value><next><block type="inout_analog_write" id="670" inline="true"><field name="PIN">5</field><value name="NUM"><block type="variables_get" id="671"><field name="TYPE">Number</field><field name="VAR">Rv</field></block></value><next><block type="serial_print" id="649" inline="false"><value name="CONTENT"><block type="variables_get" id="651"><field name="TYPE">Number</field><field name="VAR">Bv</field></block></value><next><block type="delay_custom" id="569" inline="false"><value name="DELAY_TIME"><block type="variables_get" id="170"><field name="TYPE">Number</field><field name="VAR">Delay_time</field></block></value><next><block type="procedures_callnoreturn" id="553"><mutation name="LED_OFF"></mutation><next><block type="delay_custom" id="571" inline="false"><value name="DELAY_TIME"><block type="variables_get" id="572"><field name="TYPE">Number</field><field name="VAR">Delay_time</field></block></value></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></statement></block></next></block><block type="procedures_defnoreturn" id="173" x="1288" y="-12"><mutation></mutation><field name="NAME">get_Analog_value</field><statement name="STACK"><block type="variables_set" id="144" inline="true"><field name="TYPE">Number</field><field name="VAR">Delay_time</field><value name="VALUE"><block type="math_custom_map" id="161" inline="false"><field name="FROMLOW">0</field><field name="FROMHIGH">1025</field><field name="TOLOW">10</field><field name="TOHIGH">1000</field><value name="VALUE"><block type="inout_analog_read" id="148"><field name="PIN">A1</field></block></value></block></value><next><block type="variables_set" id="193" inline="true"><field name="TYPE">Number</field><field name="VAR">HSV</field><value name="VALUE"><block type="math_custom_map" id="194" inline="false"><field name="FROMLOW">0</field><field name="FROMHIGH">1025</field><field name="TOLOW">0</field><field name="TOHIGH">360</field><value name="VALUE"><block type="inout_analog_read" id="195"><field name="PIN">A0</field></block></value></block></value></block></next></block></statement></block><block type="procedures_defnoreturn" id="549" x="1288" y="388"><mutation></mutation><field name="NAME">LED_OFF</field><statement name="STACK"><block type="inout_analog_write" id="541" inline="true"><field name="PIN">6</field><value name="NUM"><block type="math_number" id="542"><field name="NUM">255</field></block></value><next><block type="inout_analog_write" id="543" inline="true"><field name="PIN">5</field><value name="NUM"><block type="math_number" id="544"><field name="NUM">255</field></block></value><next><block type="inout_analog_write" id="545" inline="true"><field name="PIN">3</field><value name="NUM"><block type="math_number" id="546"><field name="NUM">255</field></block></value></block></next></block></next></block></statement></block></xml> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int Delay_time; | |
int HSV; | |
int color_Width; | |
int Bv; | |
int BluePoint; | |
int Gv; | |
int GreenPoint; | |
int Rv; | |
int RedPoint; | |
int RedPoint2; | |
void get_Analog_value() { | |
Delay_time = (map(analogRead(A1),0,1025,10,1000)); | |
HSV = (map(analogRead(A0),0,1025,0,360)); | |
} | |
void LED_OFF() { | |
analogWrite(6, 255); | |
analogWrite(5, 255); | |
analogWrite(3, 255); | |
} | |
void setup() | |
{ | |
Delay_time = 10000; | |
HSV = 360; | |
color_Width = 120; | |
BluePoint = 240; | |
GreenPoint = 120; | |
RedPoint = 0; | |
RedPoint2 = 360; | |
Bv = 255; | |
Gv = 255; | |
Rv = 255; | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
while (true) { | |
get_Analog_value(); | |
if ((abs(BluePoint - HSV)) <= color_Width) { | |
Bv = (abs(BluePoint - HSV)) * (255 / color_Width); | |
} else { | |
Bv = 255; | |
} | |
if ((abs(GreenPoint - HSV)) <= color_Width) { | |
Gv = (abs(GreenPoint - HSV)) * (255 / color_Width); | |
} else { | |
Gv = 255; | |
} | |
if ((abs(RedPoint - HSV)) <= color_Width) { | |
Rv = (abs(RedPoint - HSV)) * (255 / color_Width); | |
} else { | |
if ((abs(RedPoint2 - HSV)) <= color_Width) { | |
Rv = (abs(RedPoint2 - HSV)) * (255 / color_Width); | |
} else { | |
Rv = 255; | |
} | |
} | |
analogWrite(6, Bv); | |
analogWrite(3, Gv); | |
analogWrite(5, Rv); | |
Serial.print(Bv); | |
delay(Delay_time); | |
LED_OFF(); | |
delay(Delay_time); | |
} | |
} |
Post a Comment