아두이노 코딩-197: Sine 함수를 이용한 아두이노 analogWrite

in kr •  6 years ago 

noname01.png

Sine 함수 계산을 코딩하여 시리얼 모니터에 출력해보자. Sine 함수의 성격 상 –1에서 +1 사이의 값을 출력해준다.
헤더 영역에 실수형(float) 어레이로 101개를 설정한다.
float s[101];

시리얼 모니터와 시리얼 플로터를 사용할 수 있도록 setup()에서 통신 속도를 설정해 둔다.
Serial.begin(9600);

Sine 함수 값을 이용하여 6번 핀에 설치된 LED를 analgWrite 명령으로 서서히 꺼졌다가 밝아지게끔 Dimmer 로 만들어 보자.

int pin = 6;//헤더 영역에 써 두자.
pinMode(pin, OUTPUT);

loop()에서 Sine 파형을 1주기만 발생 시키자. loop를 돌게 되면 Sine 파가 계속 발생되는 셈이다. 생성된 Sine 파형에 1.0 을 더하면 0.0∼2.0 사이 값을 주게 되며 다시 127.5를 곱하면 0.0∼255.0 사이의 값이 지속적으로 생성되므로 이 값을 LED가 설치된 핀에 analogWrite 명령으로 입력해주면 Dimmer 가 완성된다.

for loop를 코딩함에 정수형 인덱스 i 는 0∼100까지로 설정하자.
s[i]=127.5(1.0sin(2.0PIi*t));

시리얼 모니터에서 계산 결과를 모니터할 수 있도록 출력하자.
Serial.println(s[i], 2);//소수이하 2자리까지 출력
analogWrite( pin, s[i]);//
구체적인 코딩은 첨부된 파일을 참조하기 바란다.

일단 코드의 컴파일 업로딩이 완성되었으면 일차적으로 시리얼 모니터에 출력하여 그 값이 0∼255 인지 확인하자.
그 다음에는 시리얼 모니터를 끄고 시리얼 플로터를 켜서 Sine 파형을 관찰해 보자.

noname02.png

다음 동영상을 통해 Dimmering 현상을 관찰해 보자.

//Sine_analogwrite

float s[101];
float t=0.01;
int pin = 6;
void setup() {
Serial.begin(9600);
pinMode(pin, OUTPUT);
}
void loop() {
for (int i=0; i<101; i++) {
s[i]=127.5(1+sin(2PIit));
Serial.println(s[i], 2);
analogWrite(pin, s[i]);
delay(100);
}

}

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

짱짱맨 호출에 응답하여 보팅하였습니다.

아두이노 워크샵에 갔던 기억이 새록새록 나네요 하핫

Congratulations @codingart! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You published a post every day of the week

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Support SteemitBoard's project! Vote for its witness and get one more award!