2021年8月22日 星期日

RGB 加按鈕 0 1 2,再執行上面num=2就歸零

 int rled_pin = 3;

int gled_pin = 4;

int bled_pin = 5;

int btn1 = 2;

int num_val;

int num = -1;

void setup()

{

  Serial.begin(9600);

  pinMode(btn1, INPUT);

}


void loop()

{

  num_val = digitalRead(btn1);

  if (num_val == LOW)

  {

    delay(1000);

    while (digitalRead(btn1) == LOW);

    if (num ==2)

    {

      num = 0;//此處是一個等號 

      Serial.print("在num=3的後面num="); Serial.println(num);

    }

    else

    {

      num++;//他會執行0 1 2,再執行上面num=2就歸零

      Serial.print("'在else 後的num="); Serial.println(num);

    }

  }

  if (num == 0)

  {

    analogWrite(rled_pin, 255);

    analogWrite(gled_pin, 0);

    analogWrite(bled_pin, 0);

  }

  else if (num == 1)

  {

    analogWrite(rled_pin, 0);

    analogWrite(gled_pin, 255);

    analogWrite(bled_pin, 0);

  }

  else if (num == 2)

  {

    analogWrite(rled_pin, 0);

    analogWrite(gled_pin, 0);

    analogWrite(bled_pin, 255);

  }

}

沒有留言:

張貼留言

algorithm

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