2022年2月25日 星期五

p6-9 Arduino板傳送LED狀態至電腦

 int led[]={8,9,10,11,12,13};

int i=0,j;

void setup()

{

 Serial.begin(9600);

 for(i=0;i<6;i++)

 {

  pinMode(led[i],OUTPUT);

 }

}


void loop() 

{

  Serial.print("LED=");

  for(j=0;j<6;j++)

  {

    if(j==i)Serial.print("1");

    else Serial.print("0");

  }

  Serial.println();

  digitalWrite(led[i],HIGH);

  delay(1000);

  digitalWrite(led[i],LOW);

  i++;

  if(i>5)

   i=0;

}

p6-8 小寫英文字母

 int val = 97;

int i;

void setup()

{

  Serial.begin(9600);


}


void loop()

{

  for (i = 0; i < 26; i++)

  {

    Serial.print("Arduino ");

    Serial.print(i);

    Serial.print(":");

    Serial.write(val +i );

    Serial.print("=");

    Serial.println(val+i);

    delay(1000);

  }

}

4-35 serialEvent()

 char ch;

byte no;

void setup()

{

  Serial.begin(9600);

}

void loop()

{

  delay(1000);

}

void serialEvent()

{

  ch = Serial.read();

  if (ch >= '1' && ch <= '9')

  {

    no = ch - '0';

    Serial.print(no);

    Serial.print("");

    for (int i = 0; i < no; i++)

      Serial.print("*");

    Serial.println();

  }

}

4-33 接收串列資料

 char ch;

byte no;

void setup()

{

  Serial.begin(9600);

}


void loop()

{

 if(Serial.available())

 {

  ch=Serial.read();

  if(ch>='1' && ch<='9')

  {

    no=ch-'0';

    Serial.print(no);

    Serial.print("");

    for(int i=0;i<no;i++)

      Serial.print("*");

    Serial.println();

  }

 }

}

4-31 傳送訊息給電腦

 int num = 65;

int i = 1;

void setup()

{

  Serial.begin(9600);

  while (!Serial);

}


void loop()

{


    Serial.write(num);

    Serial.print("\t");

    Serial.print(num);

    Serial.print("\t");

    Serial.print(num, BIN);

    Serial.print("\t");

    Serial.print(num, OCT);

    Serial.print("\t");

    Serial.println(num, HEX);

    

    Serial.print(i);

    Serial.print("*");

    Serial.print(i);

    Serial.print("=");

    Serial.print(i * i);

    Serial.print("\n\n");

    delay(1000);

    num++;

    i++;


}

2022年2月12日 星期六

Serial.print Serial.write

 int val=97;

void setup()

{

 Serial.begin(9600);

}


void loop()

{

 for(int i=0;i<26;i++)

 {

  Serial.print("Arduino ");

  Serial.print(i+1);

  Serial.print(":");

  Serial.write(val+i);

  Serial.print("=");

  Serial.println(val+i,HEX);

  delay(1000);

 }

}

2022年2月11日 星期五

rain led

 int led_pin[] = {3, 5, 6, 10};

int brightness[7][4] =

{

  {250, 0, 0, 0},

  {100, 250, 0, 0},

  {50, 100, 250, 0},

  {5, 50, 100, 250},

  {0, 5, 50, 100},

  {0, 0, 5, 50},

  {0, 0, 0, 5},

};


void setup()

{


}


void loop()

{

  for (int j = 0; j < 7; j++)

  {

    for (int i = 0; i < 4; i++)

    {

      analogWrite(led_pin[i], brightness[j][i]);

      delay(100);

    }

  }

    for (int j = 0; j < 7; j++)

  {

    for (int i = 0; i < 4; i++)

    {

      analogWrite(led_pin[i],brightness[j][3-i]);

      delay(100);

    }

  }

}

pwm 4個pulse

 int led_pin[] = {3, 5, 6, 9};

int i;

void setup()

{


}


void loop()

{

  for (int j = 0; j < 4; j++)

  {

    for ( i = 0; i < 255; i += 5)

    {

      analogWrite(led_pin[j], i); delay(30);

    }

    for (i = 255; i > 0; i -= 5)

    {

      analogWrite(led_pin[j], i); delay(30);

    }

    analogWrite(led_pin[j],i); delay(30);

    delay(100);

  }

    for (int j = 2; j >0; j--)

  {

    for ( i = 0; i < 255; i += 5)

    {

      analogWrite(led_pin[j], i); delay(30);

    }

    for (i = 255; i > 0; i -= 5)

    {

      analogWrite(led_pin[j], i); delay(30);

    }

    analogWrite(led_pin[j],i); delay(30);

    delay(100);

  }

}

debounce +sw

 boolean led_state;

boolean btn_state;

int last_btn_state;

unsigned long last_debounce_time;

void setup()

{

  Serial.begin(9600);

  pinMode(2, INPUT);

  pinMode(13, OUTPUT);

}


void loop()

{

  int reading = digitalRead(2);

  if (reading != last_btn_state)

  {

    last_debounce_time = millis();

  }


  if ((millis() - last_debounce_time) > 50)

  {

    if (btn_state != reading)

    {

      btn_state = reading;

      Serial.println(btn_state);

      if (btn_state == LOW)

      {

        led_state = !led_state;

        digitalWrite(13, led_state);

      }

    }

  }

  last_btn_state=reading;

}

2022年2月9日 星期三

pmw 4個led brightness左右

 int led_pin[] = {3, 5, 6, 10};

void setup() {

  // put your setup code here, to run once:


}


void loop()

{

  for (int i = 0; i < 4; i++)

  {

    for (int bt = 0; bt > 255; bt += 5)

    {

      analogWrite(led_pin[i], bt);

      delay(50);

    }

    for (int bt = 255; bt > 0; bt -= 5)

    {

      analogWrite(led_pin[i], bt);

      delay(50);

    }

    analogWrite(led_pin[i], 0);

  }

  for (int i = 2; i >0; i--)

  {

    for (int bt = 0; bt > 255; bt += 5)

    {

      analogWrite(led_pin[i], bt);

      delay(50);

    }

    for (int bt = 255; bt > 0; bt -= 5)

    {

      analogWrite(led_pin[i], bt);

      delay(50);

    }

    analogWrite(led_pin[i], 0);

  }

}

2022年2月7日 星期一

防彈跳 一個led + btn

 int reading;

boolean led_state ;//你宣告int led_state也可以pass

int btn_state;

int last_btn_state = LOW;

long last_debounce_time = 0;

void setup()

{

  Serial.begin(9600);

  pinMode(10, OUTPUT);

  pinMode(2, INPUT);

}


void loop()

{

  reading = digitalRead(2);

  if (reading != last_btn_state)

  {

    last_debounce_time = millis();

  }

  if ((millis() - last_debounce_time) > 50)

  {

    if (reading != btn_state)

    {

      btn_state = reading;//先交插state reading swap

      if (btn_state == LOW)

      {

        led_state=!led_state;//按下去再變化

        Serial.println(led_state);

        digitalWrite(10, led_state);


      }

    }

  }

  last_btn_state = reading;

}

2022年2月6日 星期日

if結構p6-17

int brightness = 0;

int fade_amount = 5;

int btn_state;

int last_btn_state = LOW;

unsigned long last_debounce_time = 0;

void setup()

{

  Serial.begin(9600);

  pinMode(2, INPUT);

  digitalWrite(10, LOW);

}


void loop()

{

  int reading = digitalRead(2);

  if (reading != last_btn_state)    last_debounce_time = millis();

  if ((millis() - last_debounce_time) > 50)

  {

          if (reading != btn_state)

          {

            btn_state = reading;

            if (btn_state == LOW)  chg_brightness();

          }

          else

          {

            if ((millis() - last_debounce_time) > 1000)

                if (btn_state == LOW)   chg_brightness();

          }

  }

  last_btn_state = reading;

}

void chg_brightness()

{

  brightness = brightness + fade_amount;

  if (brightness > 255)brightness = 255;

  if (brightness < 0)brightness = 0;

  if (brightness == 255 || brightness == 0)fade_amount = -fade_amount;

  analogWrite(10, brightness);

  Serial.println(brightness);

  delay(30);

}

P7-13

 int led_pin[]={2,3,4,5,6,7,8,9};

char led_table[]={0x81,0x42,0x24,0x18,0x24,0x42,0x81,0x00,0x81,0xc3,0xe7,0xff,0xe7,0xc3,0x81,0x00};

void setup() 

{

  Serial.begin(9600);

 for(int i=0;i<8;i++)

 {

  pinMode(led_pin[i],OUTPUT);

  digitalWrite(led_pin[i],LOW);

 }

}


void loop()

{

 for(int j=0;j<16;j++)

 {

  out(led_table[j]);

  Serial.println(j);

  delay(500);

 }

}

void out(byte data)

{

 for(int k=0;k<8;k++)

 {

  if(data%2==0)digitalWrite(led_pin[k],LOW);

  else digitalWrite(led_pin[k],HIGH);

  data=data/2;

 }

}

P7-12

 int led_pin[]={2,3,4,5,6,7,8,9};

char led_table[]={0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00};

void setup() 

{

  Serial.begin(9600);

 for(int i=0;i<8;i++)

 {

  pinMode(led_pin[i],OUTPUT);

  digitalWrite(led_pin[i],LOW);

 }

}


void loop()

{

 for(int j=0;j<16;j++)

 {

  out(led_table[j]);

  Serial.println(j);

  delay(500);

 }

}

void out(byte data)

{

 for(int k=0;k<8;k++)

 {

  if(data%2==0)digitalWrite(led_pin[k],LOW);

  else digitalWrite(led_pin[k],HIGH);

  data=data/2;

 }

}

7seg

 int seg7_pin[]={2,3,4,5,6,7,8};

char seg7_table[]={~0xc0,~0xf9,~0xa4,~0xb0,~0x99,~0x92,~0x82,~0xf8,~0x80,~0x90};

void setup() 

{

 for(int i=0;i<7;i++)

 {

  pinMode(seg7_pin[i],OUTPUT);

  digitalWrite(seg7_pin[i],LOW);

 }

}


void loop()

{

 for(int j=0;j<7;j++)

 {

  out(seg7_table[j]);

  delay(500);

 }

}

void out(byte data)

{

 for(int k=0;k<7;k++)

 {

  if(data%2==0)digitalWrite(seg7_pin[k],LOW);

  else digitalWrite(seg7_pin[k],HIGH);

  data=data/2;

 }

}


sw + brightness + fade_amount

 int brightness = 0;

int fade_amount = 5;

int reading;

unsigned long last_debounce_time = 0;

int btn_state;

int last_btn_state ;

void setup()

{

  Serial.begin(9600);

  pinMode(2, INPUT);

  digitalWrite(10,LOW);

}


void loop()

{

 reading=digitalRead(2);

 if(reading != last_btn_state)

 {

  last_debounce_time=millis();

 }

 if((millis()-last_debounce_time)>50)

 {

  if(reading !=btn_state)

  {

    btn_state=reading;

    if(btn_state==LOW) chg_brightness();

  }

  else

  {

    if((millis()-last_debounce_time)>1000)

    {

      if(btn_state==LOW) chg_brightness();

    }

  }

 }

 last_btn_state=reading;

}

void chg_brightness()

{

  brightness = brightness + fade_amount;

  if (brightness > 255)brightness = 255;

  if (brightness < 0)brightness = 0;

  if (brightness == 255 || brightness == 0) fade_amount = -fade_amount;

  analogWrite(10, brightness);

  Serial.println(brightness);

  delay(30);

}


2022年2月5日 星期六

fade

 int reading;

int btn_state;

int last_btn_state=LOW;

unsigned long last_debounce_time=0;

int brightness=0;

int fade_amount=5;

void setup() 

{

  Serial.begin(9600);

  pinMode(2,INPUT);

  pinMode(3,OUTPUT);

}


void loop() 

{

 reading=digitalRead(2);

 if(reading !=last_btn_state)

 {

  last_debounce_time=millis();

 }

 if((millis()-last_debounce_time)>50)

 {

  if(reading != btn_state)

  {

    btn_state=reading;

    if(btn_state==LOW)

    {

      chg_brightness();

    }

    else

    {

      if((millis()-last_debounce_time)>1000)

      {

        if(btn_state==LOW)chg_brightness();

      }

    }

  }

 }

 last_btn_state=reading;

}

void chg_brightness()

{

  brightness=brightness+fade_amount;

  if(brightness>255) brightness=255;

  if(brightness<0) brightness=0;

  if(brightness==0 || brightness==255) fade_amount=-fade_amount;

  analogWrite(3,brightness);

  Serial.println(brightness);

  delay(30);

}

24hr min sec

 int sec;

int min;

int hr;

long start_time;

long hr_setup = 3600000;

long min_setup = 60000;

long sec_setup = 1000;

void setup()

 {

  Serial.begin(9600);

}


void loop()

 {

  start_time = millis();

  hr = start_time / hr_setup;

  min = (start_time % hr_setup) / min_setup;

  sec = ((start_time % hr_setup) % min_setup) / sec_setup;

  Serial.print(hr);

  Serial.print(":");

  Serial.print(min);

  Serial.print(":");

  Serial.println(sec);

}

debounce sw

 boolean led_state = LOW;

boolean btn_state;

void setup()

{

  pinMode(2, INPUT);

  pinMode(13, OUTPUT);

}


void loop()

{

  btn_state = digitalRead(2);

  if (btn_state == LOW)

  {

    led_state=!led_state;

    digitalWrite(13,led_state);

    delay(20);

    while(1)

    {

      if(digitalRead(2)==HIGH)break;

    }

    delay(20);

  }

}

防彈跳 按一下亮,按一下off

 boolean btn_state;

boolean led_state = HIGH;

void setup()

{

  pinMode(13, OUTPUT);

  digitalWrite(13, led_state);

  pinMode(2, INPUT);

}


void loop()

{

  btn_state=digitalRead(2);

 if(btn_state==LOW)

 {

  led_state=!led_state;

  digitalWrite(13,led_state);

  delay(20);

  while(1)

  {

    btn_state=digitalRead(2);

    if(btn_state==HIGH)

    {

      break;

    }

  }

   delay(20); 

 }

}

algorithm

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