수강한 개념
풀업 풀다운 저항 위치 앞뒤
조도센서
멀티스레드 이용
문제 구현 / 개선방안
1. 빛이 없을때 LED와 부저가 켜지고 스위치를 누르면 둘 다 종료 (멀티 스레드 이용)
#include <wiringPi.h>
#include <stdio.h>
#include <pthread.h>
#include <softTone.h>
#include <stdlib.h>
#define SW 5
#define CDS 0
#define LED 1
#define SPKR 6
#define TOTAL 32
int notes[] = {
391, 391, 440, 440, 391, 391, 329.63, 329.63,
391, 391, 329.63, 329.63, 293.66, 293.66, 0,
391, 391, 440, 440, 391, 391, 329.63, 329.63,
391, 329.63, 293.66, 329.63, 261.63, 261.63, 261.63, 0
};
int musicPlay() //부저 소리내기
{
int i;
softToneCreate(SPKR);
for(i = 0; i < TOTAL; ++i)
{
softToneWrite(SPKR, notes[i]);
delay(280);
}
return 0;
}
int *cdsControl()
{
int i;
pinMode(SW, INPUT);
pinMode(CDS, INPUT);
pinMode(LED, OUTPUT);
for(;;)
{
if(digitalRead(CDS) == LOW)//빛이 없으면
{
digitalWrite(LED, HIGH);//LED를 켜고
musicPlay();//부저를 울림
}
}
return 0;
}
int *switchControl()//스위치 제어
{
int i;
pinMode(SW, INPUT);
pinMode(LED, OUTPUT);
for(;;)
{
if(digitalRead(SW) == LOW)//버튼이 눌러지면
{
digitalWrite(LED, LOW);//LED를 끄고
softToneWrite(SPKR, 0);
softToneStop(SPKR);//부저 끄기
}
delay(10);
}
return 0;
}
int main(int argc, char **argv)
{
pthread_t tid1, tid2;
wiringPiSetup();
if(pthread_create(&tid1, NULL, cdsControl, NULL) != 0) { //센서 thread 생성
fprintf(stderr, "thread create error\n");
exit(1);
}
if(pthread_create(&tid2, NULL, switchControl, NULL) != 0) { //스위치 thread 생성
fprintf(stderr, "thread create error\n");
exit(1);
}
pthread_join(tid1, NULL); //1번 스레드 자원 회수
pthread_join(tid2, NULL); //2번 스레드 자원 회수
return 0;
}
2. 뮤텍스 이용
#include <wiringPi.h>
#include <stdio.h>
#include <pthread.h>
#include <softTone.h>
#include <stdlib.h>
#define SW 5
#define CDS 0
#define LED 1
#define SPKR 6
#define TOTAL 9
int notes[] = {
391, 391, 440, 440, 391, 391, 329, 329, 391
};
pthread_mutex_t lock; // 뮤텍스 선언
int musicPlay() // 부저 소리내기
{
int i;
softToneCreate(SPKR);
pthread_mutex_lock(&lock); // 뮤텍스 잠금
for(i = 0; i < TOTAL; ++i)
{
softToneWrite(SPKR, notes[i]);
delay(280);
}
pthread_mutex_unlock(&lock); // 뮤텍스 해제
return 0;
}
void *cdsControl(void *arg)
{
pinMode(CDS, INPUT);
pinMode(LED, OUTPUT);
for(;;)
{
if(digitalRead(CDS) == LOW) // 빛이 없으면
{
digitalWrite(LED, HIGH); // LED 켜기
musicPlay(); // 부저 울리기
}
}
return NULL;
}
void *switchControl(void *arg) // 스위치 제어
{
pinMode(SW, INPUT);
pinMode(LED, OUTPUT);
for(;;)
{
if(digitalRead(SW) == LOW) // 버튼이 눌러지면
{
pthread_mutex_lock(&lock); // 부저 제어 시 뮤텍스 잠금
digitalWrite(LED, LOW); // LED 끄기
softToneWrite(SPKR, 0); // 부저 끄기 (파형을 0으로 설정)
pthread_mutex_unlock(&lock); // 뮤텍스 해제
}
delay(10);
}
return NULL;
}
int main(int argc, char **argv)
{
pthread_t tid1, tid2;
wiringPiSetup();
pthread_mutex_init(&lock, NULL); // 뮤텍스 초기화
if(pthread_create(&tid1, NULL, cdsControl, NULL) != 0) {
fprintf(stderr, "thread create error\n");
exit(1);
}
if(pthread_create(&tid2, NULL, switchControl, NULL) != 0) {
fprintf(stderr, "thread create error\n");
exit(1);
}
pthread_join(tid1, NULL); // 1번 스레드 자원 회수
pthread_join(tid2, NULL); // 2번 스레드 자원 회수
pthread_mutex_destroy(&lock); // 뮤텍스 해제
return 0;
}
기타 사항
VEDA 바로가기 : www.vedacademy.co.kr
VEDA(한화비전 아카데미) 영상으로 확인하기 : https://url.kr/zy9afd
본 후기는 VEDA(한화비전 아카데미) 1기 학습 기록으로 작성되었습니다.
'교육관련 > 한화비전 VEDA 수강일지' 카테고리의 다른 글
[VEDA 1기 수강일지] 55일차 - 임베디드 리눅스 이론 (1) : 임베디드 리눅스 구성요소 (0) | 2024.10.14 |
---|---|
[VEDA 1기 수강일지] 50일차 - openCV (2) : (0) | 2024.10.02 |
[VEDA 1기 수강일지] 42일차 - linux 미니프로젝트 : tcp 통신을 이용한 채팅프로그램 (2) (0) | 2024.09.13 |
[VEDA 1기 수강일지] 40일차 - linux 미니프로젝트 : tcp 통신을 이용한 채팅프로그램 (1) (0) | 2024.09.11 |
[VEDA 1기 수강일지] 21일차 - Linux 기초 (1) : 파일 조회 등 (0) | 2024.08.12 |
댓글