以下のプログラミングをしたのですが(見づらくて申し訳ないです)、問題点がいくつかありました。
1、if文にボタンを押したら(status = HIGHの状態)割り込み?のようにすぐ飛んでほしい。
2、サーボモーターは止まっているはずが、常に微動している。
3、LEDテープの色が(255,255,255)で白になるはずが、オレンジになってしまう。
Arduino UNO R3 の互換品を使っています。
いい解決策があれば、できれば具体的に修正点を教えていただきたいです。
C
1 2 3 4 5 6#include <Adafruit_NeoPixel.h> 7 8//ライブラリインクルード 9#include <Adafruit_NeoPixel.h> 10 11#define LED_COUNT ( 40 ) 12#define DIGITAL_PIN ( 6 ) 13 14 15#define input_pin 2 16#define input_pin2 3 17#define input_pin3 10 18#define input_pin4 11 19#define input_pin5 12 20 21 22 23//www.elegoo.com 24//2016.12.08 25#include <Servo.h> 26 27Servo myservo; // create servo object to control a servo 28// twelve servo objects can be created on most boards 29 30int pos = 0; // variable to store the servo position 31 32 33Adafruit_NeoPixel led = Adafruit_NeoPixel( LED_COUNT,DIGITAL_PIN , NEO_GRB + NEO_KHZ800); 34void setup() { 35 //初期化処理 36 led.begin(); 37 led.show(); 38 39 pinMode(input_pin,INPUT) ; 40 pinMode(input_pin2,INPUT) ; 41 pinMode(input_pin3,INPUT) ; 42 pinMode(input_pin4,INPUT) ; 43 pinMode(input_pin5,INPUT) ; 44 45 pinMode(13, OUTPUT); 46 47 Serial.begin(9600); 48 myservo.attach(7); // attaches the servo on pin 9 to the servo object 49 50} 51 52void loop() { 53 digitalWrite(13, HIGH); // スイッチをOFFする 54 55 56 57 for(int i = 0 ; i < LED_COUNT ; i++ ) 58{ 59led.setPixelColor(19+i,led.Color(255,255,255)); 60led.setPixelColor(20+i,led.Color(0,0,0)); 61 62led.setPixelColor(19-i,led.Color(255,255,255)); 63led.setPixelColor(18-i,led.Color(0,0,0)); 64 65 66led.show(); 67delay(20); 68} 69 70 int status ; 71 int status2 ; 72 73 74 status = digitalRead(input_pin) ; //スイッチの状態を読む 75 status2 = digitalRead(input_pin2) ; //スイッチの状態を読む 76 77 78 79 80 if(status == HIGH && status2 == LOW){ 81 for(int i = 0 ; i < LED_COUNT ; i++ ) 82{ 83led.setPixelColor(19+i,led.Color(0,255,0)); 84led.setPixelColor(20+i,led.Color(0,0,0)); 85 86led.setPixelColor(19-i,led.Color(0,255,0)); 87led.setPixelColor(18-i,led.Color(0,0,0)); 88 89 90led.show(); 91delay(20); 92} 93 digitalWrite(13, LOW); // スイッチをONする 94 delay(3000); //3秒待つ 95 96 97 98 99 } else if(status == HIGH && status2 == HIGH){ 100 for(int i = 0 ; i < LED_COUNT ; i++ ) 101{ 102led.setPixelColor(19+i,led.Color(10,10,10)); 103led.setPixelColor(20+i,led.Color(0,0,0)); 104 105led.setPixelColor(19-i,led.Color(10,10,10)); 106led.setPixelColor(18-i,led.Color(0,0,0)); 107 108 109led.show(); 110delay(20); 111} 112 113 114 for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees 115 // in steps of 1 degree 116 myservo.write(pos); // tell servo to go to position in variable 'pos' 117 delay(15); // waits 15ms for the servo to reach the position 118 } 119 for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees 120 myservo.write(pos); // tell servo to go to position in variable 'pos' 121 delay(15); // waits 15ms for the servo to reach the position 122 } 123 124 125 126 while(1){ 127 int status3 ; 128 int status4 ; 129 int status5 ; 130 status3 = digitalRead(input_pin3) ; //スイッチの状態を読む 131 status4 = digitalRead(input_pin4) ; //スイッチの状態を読む 132 status5 = digitalRead(input_pin5) ; //スイッチの状態を読む 133 134int t = 0; 135int l = 0; 136int m = 0; 137 138 if(status3 == HIGH){ 139 digitalWrite(13, LOW); // スイッチをONする 140 141 while(t<250){ 142 for(int i = 0 ; i < LED_COUNT ; i++ ) 143{ 144led.setPixelColor(19+i,led.Color(0,0,255)); 145led.setPixelColor(20+i,led.Color(0,0,0)); 146 147led.setPixelColor(19-i,led.Color(0,0,255)); 148led.setPixelColor(18-i,led.Color(0,0,0)); 149 150 151led.show(); 152delay(20); 153t++; 154} 155 } 156 157 158 159 160 goto label; 161 }else if(status4 == HIGH){ 162 163digitalWrite(13, LOW); // スイッチをONする 164 165 while(l<500){ 166 167 for(int i = 0 ; i < LED_COUNT ; i++ ) 168{ 169led.setPixelColor(19+i,led.Color(255,0,0)); 170led.setPixelColor(20+i,led.Color(0,0,0)); 171 172led.setPixelColor(19-i,led.Color(255,0,0)); 173led.setPixelColor(18-i,led.Color(0,0,0)); 174 175 176led.show(); 177delay(20); 178l++; 179} 180 } 181 182 183 184 goto label; 185 186 }else if(status5 == HIGH){ 187 188digitalWrite(13, LOW); // スイッチをONする 189 190int m = 0; 191while(m<1000){ 192 uint16_t i, j; 193 194 for(j=0; j <256; j++) { 195 for(i=0; i < led.numPixels(); i++) { 196 led.setPixelColor(i, rotateColor((((i) * 256 / led.numPixels()) + j) & 255)); 197 } 198 led.show(); 199 delay(10); 200m++; 201} 202} 203 204 205 206 goto label; 207 } 208 209 210 } 211 } 212label:; 213} 214 215//RGBの色の移り変わり関数 216uint32_t rotateColor(byte WheelPos) { 217 if(WheelPos < 85) { 218 return led.Color(WheelPos * 3, 255 - WheelPos * 3, 0); 219 } else if(WheelPos < 170) { 220 WheelPos -= 85; 221 return led.Color(255 - WheelPos * 3, 0, WheelPos * 3); 222 } else { 223 WheelPos -= 170; 224 return led.Color(0, WheelPos * 3, 255 - WheelPos * 3); 225 } 226}
回答3件
あなたの回答
tips
プレビュー