Arduino 使用3个LED的串行通信更改亮度

Arduino 使用3个LED的串行通信更改亮度,arduino,Arduino,到目前为止,我已经编写了这样的代码:如果1然后输入pressed或send clicked,然后再次输入1并输入pressed或send clicked,则LED 1将点亮;如果以类似的方式输入“1”“0”,则LED 1将熄灭;对于LED 2和LED 3,依此类推,即:“2”“1”将点亮LED 2,“3”“0”将熄灭LED 3 int incomingVal; int ledPin = 16; int ledPin2 = 15; int ledPin3 = 14;

到目前为止,我已经编写了这样的代码:如果1然后输入pressed或send clicked,然后再次输入1并输入pressed或send clicked,则LED 1将点亮;如果以类似的方式输入“1”“0”,则LED 1将熄灭;对于LED 2和LED 3,依此类推,即:“2”“1”将点亮LED 2,“3”“0”将熄灭LED 3

 int incomingVal;
    int ledPin = 16; 
    int ledPin2 = 15; 
    int ledPin3 = 14; 

    void setup()
    {

      Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
      Serial.println("starting");
      pinMode(ledPin,OUTPUT);
      pinMode(ledPin2,OUTPUT);
      pinMode(ledPin3,OUTPUT);
    }




    void loop()
    {
      if (Serial.available() > 0 ) //then chars are in the serial buffer
      {
        incomingVal = Serial.parseInt();
        Serial.print("You entered: ");
        Serial.println(incomingVal);

        if (incomingVal == 10)//turns off led 1 
        {
          digitalWrite(ledPin, LOW);  

        }

         if (incomingVal == 11)//turns on led 1 
        {
          digitalWrite(ledPin, HIGH);  

        }

         if (incomingVal == 20)//turns off led 2
        {
          digitalWrite(ledPin2, LOW);  

        }

         if (incomingVal == 21)//turns on led 2
        {
          digitalWrite(ledPin2, HIGH);  

        }

         if (incomingVal == 30)//turns off led 3
        {
          digitalWrite(ledPin3, LOW);  

        }

         if (incomingVal == 31)//turns on led 3
        {
          digitalWrite(ledPin3, HIGH);  

        }

      }

    }

如何更改代码,以便输入第三个值将亮度从0更改为250?例如,键入“2,1125”将使LED 2以50%的亮度亮起。

要使LED以一半的亮度亮起,您需要使用。确保正在使用的插脚旁边有~个插脚(Uno上的插脚3、5、6、9、10、11)

int led_pins[3] = {9,10,11};

int incomingVal;
int brightness;
int parsed_value;
int x = 0;
void setup()
{
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
  Serial.println("starting");
  pinMode(led_pins[0],OUTPUT);  // You could also use a loop here
  pinMode(led_pins[1],OUTPUT);
  pinMode(led_pins[2],OUTPUT);
}




void loop()
{
  if (Serial.available() > 0 ) //then chars are in the serial buffer
  {
    incomingVal = Serial.parseInt();
    Serial.print("You entered: ");
    Serial.println(incomingVal);

    //figure out which LED we selected
    parsed_value = incomingVal;
    while (parsed_value > 9){
      /*
      because of integer division, this line will remove the last
       number from the integer. ie 11 / 10 = 1 (the result is rounded down)
       The loop will continue until only 1 digit remains
       */
      x++;
      parsed_value = parsed_value / 10; 
    }
    // x represents the number of times parsed_value was divided by 10
    // this line removes the first digit of the incoming value, leaving
    // the brightness
    brightness = incomingVal - pow(10,x)*parsed_value;
    /*parsed_value will be 1 greater than the array key value because
     the array is 0 based.*/
    //set the LED to the right brightness
    analogWrite(led_pins[parsed_value-1],brightness);
  }

}
我会稍微更改您的输入方法,因为0亮度基本上与关闭LED相同,您应该只需要2个数字。第一个数字对应LED,第二个数字对应亮度

int led_pins[3] = {9,10,11};

int incomingVal;
int brightness;
int parsed_value;
int x = 0;
void setup()
{
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
  Serial.println("starting");
  pinMode(led_pins[0],OUTPUT);  // You could also use a loop here
  pinMode(led_pins[1],OUTPUT);
  pinMode(led_pins[2],OUTPUT);
}




void loop()
{
  if (Serial.available() > 0 ) //then chars are in the serial buffer
  {
    incomingVal = Serial.parseInt();
    Serial.print("You entered: ");
    Serial.println(incomingVal);

    //figure out which LED we selected
    parsed_value = incomingVal;
    while (parsed_value > 9){
      /*
      because of integer division, this line will remove the last
       number from the integer. ie 11 / 10 = 1 (the result is rounded down)
       The loop will continue until only 1 digit remains
       */
      x++;
      parsed_value = parsed_value / 10; 
    }
    // x represents the number of times parsed_value was divided by 10
    // this line removes the first digit of the incoming value, leaving
    // the brightness
    brightness = incomingVal - pow(10,x)*parsed_value;
    /*parsed_value will be 1 greater than the array key value because
     the array is 0 based.*/
    //set the LED to the right brightness
    analogWrite(led_pins[parsed_value-1],brightness);
  }

}

要使LED以半亮度亮起,您需要使用。确保正在使用的插脚旁边有~个插脚(Uno上的插脚3、5、6、9、10、11)

我会稍微更改您的输入方法,因为0亮度基本上与关闭LED相同,您应该只需要2个数字。第一个数字对应LED,第二个数字对应亮度

int led_pins[3] = {9,10,11};

int incomingVal;
int brightness;
int parsed_value;
int x = 0;
void setup()
{
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
  Serial.println("starting");
  pinMode(led_pins[0],OUTPUT);  // You could also use a loop here
  pinMode(led_pins[1],OUTPUT);
  pinMode(led_pins[2],OUTPUT);
}




void loop()
{
  if (Serial.available() > 0 ) //then chars are in the serial buffer
  {
    incomingVal = Serial.parseInt();
    Serial.print("You entered: ");
    Serial.println(incomingVal);

    //figure out which LED we selected
    parsed_value = incomingVal;
    while (parsed_value > 9){
      /*
      because of integer division, this line will remove the last
       number from the integer. ie 11 / 10 = 1 (the result is rounded down)
       The loop will continue until only 1 digit remains
       */
      x++;
      parsed_value = parsed_value / 10; 
    }
    // x represents the number of times parsed_value was divided by 10
    // this line removes the first digit of the incoming value, leaving
    // the brightness
    brightness = incomingVal - pow(10,x)*parsed_value;
    /*parsed_value will be 1 greater than the array key value because
     the array is 0 based.*/
    //set the LED to the right brightness
    analogWrite(led_pins[parsed_value-1],brightness);
  }

}