Arduino布尔数组; 1为真,其余为假

时间:2014-12-09 15:10:26

标签: arrays arduino boolean

我有一个关于arduino的问题,考虑使用8个LED和电位器进行设置。我想让1个LED点亮,这是与电位器返回的值相匹配的LED,其余的LED应该关闭。此外,当我改变电位计的位置时,LED应该相应地改变。到目前为止我有这个:

for(int i = 0; i)
{
    if (i = draaiKnopStand)
    {
        status[i] = HIGH;
    }
    else
    {
        status[i] = LOW;
    }

    digitalWrite(draaiKnopStand, status[i]);
}

1 个答案:

答案 0 :(得分:0)

我认为这是Arduino Uno Front

我的工作中没有Arduino。我是用模拟器做的。请试一试:

int _potentiometer = 9; // Potentiometer - Analog Pin
int _val = 0;
int _borderLineVal = 0;
int ledPins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers to which LEDs are attached
int pinCount = 6; // the number of pins (i.e. the length of the array)

void setup() {
    for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
        pinMode(ledPins[thisPin], OUTPUT);      
    }
}

void loop() {
    _val = analogRead(_potentiometer); //reading the Potentiometer value interval: 0 - 1023
    _borderLineVal = (int)(1023 / pinCount);
    Serial.println(_val);

    // turn all leds off
    for (int thisPin = 0; thisPin < pinCount; thisPin++) {
        digitalWrite(ledPins[thisPin], LOW);
    }

    // turn the select led on
    if(_val > 0){
        _pinHigh = (int)(1023 / _borderLineVal);
        digitalWrite(ledPins[_pinHigh], HIGH); // turn the pin on
    }
}
相关问题