2013-05-03 99 views
0

嘿,如果有什麼東西,我會振作起來。一個突擊隊,我可以閱讀以改善此代碼? 至少它現在工作,但需要做一些微調:)Arduino電機速度LED(Led條形圖代碼)

因爲你看到的代碼是非常混亂和麻煩閱讀。希望有人能幫忙。

預先謝謝。

int potPin = 0; 
int motorPin = 11; 
int potValue = 0; 
int on = 0; 


int L2 = 2; 
int L3 = 3; 
int L4 = 4; 
int L5 = 5; 
int L6 = 6; 
int L7 = 7; 
int L8 = 8; 
int L9 = 9; 
int L10 = 10; 


int M2 = 40; 
int M3 = 75; 
int M4 = 100; 
int M5 = 125; 
int M6 = 150; 
int M7 = 175; 
int M8 = 200; 
int M9 = 225; 
int M10 = 250; 

void setup() 
{ 
pinMode(motorPin,OUTPUT); 
pinMode(potPin, INPUT); 


pinMode (L2, OUTPUT); 
pinMode (L3, OUTPUT); 
pinMode (L4, OUTPUT); 
pinMode (L5, OUTPUT); 
pinMode (L6, OUTPUT); 
pinMode (L7, OUTPUT); 
pinMode (L8, OUTPUT); 
pinMode (L9, OUTPUT); 
pinMode (L10, OUTPUT); 

Serial.begin(9600); 
} 

void loop() 
{ 
    Monitor(); 
    Motorspeed(); 
    LedBar(); 
}; 

void Monitor() 
{ 
    int val = Serial.read() - '0'; 

    if (val == 1) 
{ 
    Serial.println("Motor is ON"); 
    on = 1; 
    digitalWrite(motorPin, HIGH); 
} 

    else if (val == 0) 
{ 
    Serial.println("Motor is OFF"); 
    on = 0; 
    digitalWrite(motorPin, LOW); 
} 

    Serial.flush(); 
} 



    void Motorspeed() 
    { 
    potValue = analogRead(potPin) /4; 

    if(on == 1) 
    { 
    analogWrite(motorPin, potValue); 
    } 
    } 

    void LedBar() 
    { 
    potValue = analogRead(potPin) /4; 
    if(on == 1){ 
    if (on == 1, potValue > M2) { 
    digitalWrite(L2, HIGH); 
    } 
    else { 
    digitalWrite(L2, LOW); 
    } 
    if (on == 1, potValue > M3) { 
    digitalWrite(L3, HIGH); 
    } 
    else { 
    digitalWrite(L3, LOW); 
    } 
    if (on == 1, potValue > M4) { 
    digitalWrite(L4, HIGH); 
    } 
    else { 
    digitalWrite(L4, LOW); 
    } 

    if (on == 1, potValue > M5) { 
    digitalWrite(L5, HIGH); 
    } 
    else { 
    digitalWrite(L5, LOW); 
    } 

    if (on == 1, potValue > M6) { 
    digitalWrite(L6, HIGH); 
    } 
    else { 
    digitalWrite(L6, LOW); 
    } 

    if (on == 1, potValue > M7) { 
    digitalWrite(L7, HIGH); 
    } 
    else { 
    digitalWrite(L7, LOW); 
    } 

    if (on == 1, potValue > M8) { 
    digitalWrite(L8, HIGH); 
    } 
    else { 
    digitalWrite(L8, LOW); 
    } 

    if (on == 1, potValue > M9) { 
    digitalWrite(L9, HIGH); 
    } 
    else { 
    digitalWrite(L9, LOW); 
    } 

    if (on == 1, potValue > M10) { 
    digitalWrite(L10, HIGH); 
    } 

    else { 
    digitalWrite(L10, LOW); 
    }} 
    else 
    { 
    digitalWrite(2, LOW); 
    digitalWrite(3, LOW); 
    digitalWrite(4, LOW); 
    digitalWrite(5, LOW); 
    digitalWrite(6, LOW); 
    digitalWrite(7, LOW); 
    digitalWrite(8, LOW); 
    digitalWrite(9, LOW); 
    digitalWrite(10, LOW); 
    } 
    } 

回答

2

簡短的回答:學習如何使用循環(for和while)和數組。

較長的一個:

定義中號這樣的:

int M[] = {40,75,...,250}; 

(取代...與值的其餘部分)

並更改LedBar功能:

void LedBar() 
{ 
    int potValue = analogRead(potPin) /4; 
    if(on == 1) 
     for (int i=0;i<=8;i++) 
      if (potValue > M[i]) 
       digitalWrite(i+2, HIGH); 
    else 
     for (int i=0;i<=8;i++) 
      digitalWrite(M[i+2], LOW); 
} 

此外,在安裝程序,而不是pinMode (L.., OUTPUT);行,你可以寫:

for (int i=2;i<=10;i++) 
    pinMode (i, OUTPUT); 
0

凌亂的部分是LedBar功能......我建議把它寫這樣的:

void LedBar() 
{ 
    if(on == 1) 
    { 
    if (potValue < M2) { 
     //digitalWrite all leds 0; 
    } 
    else if (potValue > M2 && potValue < M3) { 
     //digitalWrite first led 1; 
     //digitalWrite all the others 0; 
    } 
    else if (potValue > M3 && potValue < M4) { 
     //digitalWrite first and second led 1; 
     //digitalWrite all others 0; 
    } 
    //and so on... 
    } 


    else 
    { 
    digitalWrite(2, LOW); 
    digitalWrite(3, LOW); 
    digitalWrite(4, LOW); 
    digitalWrite(5, LOW); 
    digitalWrite(6, LOW); 
    digitalWrite(7, LOW); 
    digitalWrite(8, LOW); 
    digitalWrite(9, LOW); 
    digitalWrite(10, LOW); 
    } 
    }