2016-04-18 9 views
-6

私はarduinoコードに問題があり、私はそれが機能するように助けを必要とする、それはいくつかのLEDライトを制御するために使用される予定です。明るくする、暗くする、点滅させる、そしてそれらを動かす。ヘルプが必要です!私のArduinoコードを作成する

問題を読むことができるように、スクリプト全体にコメントを追加しました。

/*This script is supposed to make a arduino board control the lights on a lego creation for a school project. 
    Button1 should make lights brighter 
    Button2 should make ligts shine less 
    Button3 should make the light blink 
    Button4 is supposed to make the light run after each other. 
*/ 

//Setting up all the lights and buttons 
const int lights[] = {9, 8, 7}; 
int light = 0; 

const int button1 = 24; 
int buttonstate1 = 0; 
const int button2 = 25; 
int buttonstate2 = 0; 
const int button3 = 26; 
int buttonstate3 = 0; 
const int button4 = 27; 
int buttonstate4 = 0; 
int lightStrength = 50; 
int blinkToggle = 0; 

//Setting up the input's and output's 
void setup() { 
    pinMode(button1, INPUT); 
    pinMode(button2, INPUT); 
    pinMode(button3, INPUT); 
    pinMode(button4, INPUT); 
    pinMode(lights[0], OUTPUT); 
    pinMode(lights[1], OUTPUT); 
    pinMode(lights[2], OUTPUT); 

    //Here i have a problem with make the list take all in one line of code. 
    analogWrite(lights[0], lightStrength); 
    analogWrite(lights[1], lightStrength); 
    analogWrite(lights[2], lightStrength); 

} 

void loop() { 
//checking if the buttons are being pressed 
buttonstate1 = digitalRead(button1); 
buttonstate2 = digitalRead(button2); 
buttonstate3 = digitalRead(button3); 
buttonstate1 = digitalRead(button4); 

//Make the lights brighter 
    while (lightStrength <= 235){ 
    if (buttonstate1 == HIGH){ 
     delay(10); 
     lightStrength = lightStrength + 1; 
     delay(10); 
     analogWrite(lights[0], lightStrength); 
     analogWrite(lights[1], lightStrength); 
     analogWrite(lights[2], lightStrength); 
    } 
    } 

//make the lights darker 
    while (lightStrength >= 45){ 
    if (buttonstate2 == HIGH){ 
     delay(5); 
     lightStrength = lightStrength - 1; 
     delay(5); 
     analogWrite(lights[0], lightStrength); 
     analogWrite(lights[1], lightStrength); 
     analogWrite(lights[2], lightStrength); 
    } 
    } 

//Blink the lights 
    while(blinkToggle == 1){ 
    if (buttonstate3 == HIGH){ 
     delay(100); 
     blinkToggle = 0; 
    } 
    } 
    while(blinkToggle == 0){ 
    if (buttonstate3 == HIGH){ 
     delay(100); 
     blinkToggle = 1; 
    } 
    } 
    while(blinkToggle == 1){ 
    delay(150); 
    analogWrite(lights[0], 0); 
    analogWrite(lights[1], 0); 
    analogWrite(lights[2], 0); 
    delay(150); 
    analogWrite(lights[0], lightStrength); 
    analogWrite(lights[1], lightStrength); 
    analogWrite(lights[2], lightStrength); 
    } 

//make the lights run (Not completed because the other code didn't work) 
    if (buttonstate4 == HIGH){ 
    delay(100); 

    } 

} 
+0

ようこそ[よくある質問をしますか?](http://stackoverflow.com/help/how -to-ask) –

+0

LEDは電流制御されており、電圧制御されていません。ハードウェアは正しいですか?何が問題なのですか? – Unimportant

答えて

1

まず「必要な助け」を求めるのは一般的なことです。あなたは間違いなくより良い質問をする必要があります。例えば、あなたが何を意味していたのか、何が問題なのか、場合によっては何が期待されているのか理解できませんでした。

とにかくこれはコメントではなく答えです。あなたのコードにはいくつかの一般的な間違いがありますし、あなたのコードをワイルドにするのに十分です。

は、例えば、あなたが

として、すべてのテストを書いた
while (lightStrength <= 235){ 
    if (buttonstate1 == HIGH){ 

さて、あなたのボタンは、その後lightStrengthがインクリメントされることはありません低い場合、ループはそのエンディングに到達することはありませんので、プログラムがそこで立ち往生されます。コードを変更することをお勧めします

if (buttonstate1 == HIGH){ 
    while (lightStrength <= 235){ 

これでコードは停止しません。

次に、blinkToggleのテストは間違っています。しばらくのうちにテストするべきではなく、if(たとえばif(blinkToggle == 1){)を使用してテストしてください。

ボタン4は実装されていないため、その動作は何か分かりませんので、実装方法を教えてください。

私は//Here i have a problem with make the list take all in one line of code.コメントを理解できませんでした。それを繰り返さずに同じ値を書きたい場合は、forループを使用してください:

byte i; 
for (i = 0; i < 3; i++) 
    analogWrite(lights[i], lightStrength); 

あなたのプログラムのためのいくつかのアドバイス。

まず、デバウンシング。バウンスが何であるかわからない場合は、on wikipediaを検索するか、googleで検索してください。とにかく、Bounce2ライブラリは、ボタンを操作する必要があるときはいつでも非常に便利です。その例を検索するか、単にいくつかの例を見てみるだけです。

そして、このプログラムであなたのアプローチを変更することをお勧めします。これにはFSM(Finite State Machine)アプローチが適しています。複雑なライブラリを使用しなくても、単純なC言語のプログラムを作成することができます。

関連する問題