2020年1月30日 星期四

pwm

const int led = 3;
void setup()
{
  // put your setup code here, to run once:
  pinMode(led,OUTPUT);
}

void loop()
{
  // put your main code here, to run repeatedly:
 analogWrite(led,255);delay(1000);
 analogWrite(led,128);delay(1000);
 analogWrite(led,64);delay(1000);
 analogWrite(led,32);delay(1000);
 analogWrite(led,16);delay(1000);
 analogWrite(led,8);delay(1000);
 analogWrite(led,4);delay(1000);
}

例2:
const int led = 3;
void setup()
{
  // put your setup code here, to run once:
  pinMode(led,OUTPUT);
}

void loop()
{
  // put your main code here, to run repeatedly:
  for(int i=0;i<256;i++)
  {
    analogWrite(led,i);delay(15);
  }
  for(int i=254;i>0;i--)
  {
    analogWrite(led,i);delay(15);
  }
}

例3
const int led1 = 9;
const int led2 = 10;
int x = 0;
void setup()
{
  // put your setup code here, to run once:
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
}

void loop()
{
  // put your main code here, to run repeatedly:
  if (x == 0)
  {
    for (int i = 0; i < 256; i++)
    {
      analogWrite(led1, i); delay(15);
    }
    for (int i = 254; i >= 0; i--)//led真的關掉,才不會有微亮
    {
      analogWrite(led1, i); delay(15);
    }
    x=1;
  }
  else if(x==1)
  {
    for (int i = 0; i < 256; i++)
    {
      analogWrite(led2, i); delay(15);
    }
    for (int i = 254; i >= 0; i--)
    {
      analogWrite(led2, i); delay(15);
    }
    x=0;
  }
}

例4: pili燈+呼吸燈
const int led[] ={3,5,6,9,10,11};
int x=0;
int delaytime=5;
void setup()
{
  // put your setup code here, to run once:
  for(int i=0;i<6;i++)
  {
   pinMode(led[i], OUTPUT);
  }
}
void loop()
{
  // put your main code here, to run repeatedly:
    for (int i = 0; i < 256; i++)
    {
      analogWrite(led[x], i); delay(delaytime);
    }
    for (int i = 254; i >= 0; i--)//led真的關掉,才不會有微亮
    {
      analogWrite(led[x], i); delay(delaytime);
    }
    x++;
    if (x== 6)x=0;
}

沒有留言:

張貼留言

algorithm

 #include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv)  { for(int j=2;j<=100;j++)//j...