시리얼 모니터를 혼자 공부하는중에 초음파를 이용해 거리측정한 값을 시리얼 모니터와 7-segment에 나타내는것입니다 그중 7-segment와 초음파 모두 정상 작동하지만 시리얼 모니터에만 오류가 생기네요 ㅠㅠ
오류:시리얼 모니터에 거리(cm)가 안나오고 ㅁ 칸만 나옵니다
아두이노 파일:
#include<arduino.h>
#define ledA 3
#define ledB 7
#define ledC 11
#define ledD 9
#define ledE 8
#define ledF 4
#define ledG 12
//#define DOT 10
#define seg1 1
#define seg2 5
#define seg3 6
#define seg4 13
/*void charfind(int n)
{
switch(n)
{
case 1 :
//A
digitalWrite(ledD,LOW);
digitalWrite(ledE,HIGH);
digitalWrite(ledF,HIGH);
digitalWrite(ledG,HIGH);
digitalWrite(ledA,HIGH);
digitalWrite(ledB,HIGH);
digitalWrite(ledC,HIGH);
digitalWrite(DOT, LOW);
break;
case 2:
//B
digitalWrite(ledD,HIGH);
digitalWrite(ledE,HIGH);
digitalWrite(ledF,HIGH);
digitalWrite(ledG,HIGH);
digitalWrite(ledA,HIGH);
digitalWrite(ledB,HIGH);
digitalWrite(ledC,HIGH);
digitalWrite(DOT, LOW);
break;
case 3:
//C
digitalWrite(ledD,HIGH);
digitalWrite(ledE,HIGH);
digitalWrite(ledF,HIGH);
digitalWrite(ledG,LOW);
digitalWrite(ledA,HIGH);
digitalWrite(ledB,LOW);
digitalWrite(ledC,LOW);
digitalWrite(DOT,LOW);
break;
case 4:
//D
digitalWrite(ledD,HIGH);
digitalWrite(ledE,HIGH);
digitalWrite(ledF,HIGH);
digitalWrite(ledG,LOW);
digitalWrite(ledA,HIGH);
digitalWrite(ledB,HIGH);
digitalWrite(ledC,HIGH);
digitalWrite(DOT,LOW);
break;
case 5:
//E
digitalWrite(ledD,HIGH);
digitalWrite(ledE,HIGH);
digitalWrite(ledF,HIGH);
digitalWrite(ledG,HIGH);
digitalWrite(ledA,HIGH);
digitalWrite(ledB,LOW);
digitalWrite(ledC,LOW);
digitalWrite(DOT,LOW);
break;
case 6:
digitalWrite(ledD,LOW);
digitalWrite(ledE,HIGH);
digitalWrite(ledF,HIGH);
digitalWrite(ledG,HIGH);
digitalWrite(ledA,HIGH);
digitalWrite(ledB,LOW);
digitalWrite(ledC,LOW);
digitalWrite(DOT,LOW);
break;
}
}
*/
void fnd(int n)
{
switch(n)
{
case 0:
digitalWrite(ledA,HIGH);
digitalWrite(ledB,HIGH);
digitalWrite(ledC,HIGH);
digitalWrite(ledD,HIGH);
digitalWrite(ledE,HIGH);
digitalWrite(ledF,HIGH);
digitalWrite(ledG,LOW);
//digitalWrite(DOT,LOW);
break;
case 1:
digitalWrite(ledA,LOW);
digitalWrite(ledB,HIGH);
digitalWrite(ledC,HIGH);
digitalWrite(ledD,LOW);
digitalWrite(ledE,LOW);
digitalWrite(ledF,LOW);
digitalWrite(ledG,LOW);
// digitalWrite(DOT,LOW);
break;
case 2:
digitalWrite(ledA,HIGH);
digitalWrite(ledB,HIGH);
digitalWrite(ledC,LOW);
digitalWrite(ledD,HIGH);
digitalWrite(ledE,HIGH);
digitalWrite(ledF,LOW);
digitalWrite(ledG,HIGH);
//digitalWrite(DOT,LOW);
break;
case 3:
digitalWrite(ledA,HIGH);
digitalWrite(ledB,HIGH);
digitalWrite(ledC,HIGH);
digitalWrite(ledD,HIGH);
digitalWrite(ledE,LOW);
digitalWrite(ledF,LOW);
digitalWrite(ledG,HIGH);
//digitalWrite(DOT,LOW);
break;
case 4:
digitalWrite(ledA,LOW);
digitalWrite(ledB,HIGH);
digitalWrite(ledC,HIGH);
digitalWrite(ledD,LOW);
digitalWrite(ledE,LOW);
digitalWrite(ledF,HIGH);
digitalWrite(ledG,HIGH);
//digitalWrite(DOT,LOW);
break;
case 5:
digitalWrite(ledA,HIGH);
digitalWrite(ledB,LOW);
digitalWrite(ledC,HIGH);
digitalWrite(ledD,HIGH);
digitalWrite(ledE,LOW);
digitalWrite(ledF,HIGH);
digitalWrite(ledG,HIGH);
// digitalWrite(DOT,LOW);
break;
case 6:
digitalWrite(ledA,HIGH);
digitalWrite(ledB,LOW);
digitalWrite(ledC,HIGH);
digitalWrite(ledD,HIGH);
digitalWrite(ledE,HIGH);
digitalWrite(ledF,HIGH);
digitalWrite(ledG,HIGH);
// digitalWrite(DOT,LOW);
break;
case 7:
digitalWrite(ledA,HIGH);
digitalWrite(ledB,HIGH);
digitalWrite(ledC,HIGH);
digitalWrite(ledD,LOW);
digitalWrite(ledE,LOW);
digitalWrite(ledF,LOW);
digitalWrite(ledG,LOW);
//digitalWrite(DOT,LOW);
break;
case 8:
digitalWrite(ledA,HIGH);
digitalWrite(ledB,HIGH);
digitalWrite(ledC,HIGH);
digitalWrite(ledD,HIGH);
digitalWrite(ledE,HIGH);
digitalWrite(ledF,HIGH);
digitalWrite(ledG,HIGH);
//digitalWrite(DOT,LOW);
break;
case 9:
digitalWrite(ledA,HIGH);
digitalWrite(ledB,HIGH);
digitalWrite(ledC,HIGH);
digitalWrite(ledD,HIGH);
digitalWrite(ledE,LOW);
digitalWrite(ledF,HIGH);
digitalWrite(ledG,HIGH);
//digitalWrite(DOT,LOW);
break;
case 10:
digitalWrite(ledA,HIGH);
digitalWrite(ledB,HIGH);
digitalWrite(ledC,HIGH);
digitalWrite(ledD,HIGH);
digitalWrite(ledE,HIGH);
digitalWrite(ledF,HIGH);
digitalWrite(ledG,LOW);
//digitalWrite(DOT,LOW);
break;
}
}
void clr(){
digitalWrite(ledA,LOW);
digitalWrite(ledB,LOW);
digitalWrite(ledC,LOW);
digitalWrite(ledD,LOW);
digitalWrite(ledE,LOW);
digitalWrite(ledF,LOW);
digitalWrite(ledG,LOW);
//digitalWrite(DOT,LOW);
delay(500);
}
void set(){
digitalWrite(ledA,HIGH);
digitalWrite(ledB,HIGH);
digitalWrite(ledC,HIGH);
digitalWrite(ledD,HIGH);
digitalWrite(ledE,HIGH);
digitalWrite(ledF,HIGH);
digitalWrite(ledG,HIGH);
//digitalWrite(DOT,HIGH);
delay(500);
}
int a[4]={0,0,0,0};
void setup() {
pinMode(ledA,OUTPUT);
pinMode(ledB,OUTPUT);
pinMode(ledC,OUTPUT);
pinMode(ledD,OUTPUT);
pinMode(ledE,OUTPUT);
pinMode(ledF,OUTPUT);
pinMode(ledG,OUTPUT);
// pinMode(DOT,OUTPUT);
pinMode(seg1,OUTPUT);
pinMode(seg2,OUTPUT);
pinMode(seg3,OUTPUT);
pinMode(seg4,OUTPUT);
pinMode(2,INPUT);
pinMode(10,OUTPUT);
Serial.begin(9600);
}
void loop() {
long duration, cm;
digitalWrite(10,LOW);
delayMicroseconds(2);
digitalWrite(10,HIGH);
delayMicroseconds(10);
digitalWrite(10,LOW);
duration = pulseIn(2,HIGH);
cm=(duration/2)/29.1;
a[3]=cm;
a[2]=cm/10;
a[1]=cm/100;
a[0]=cm/1000;
Serial.print(cm);
Serial.println("cm");
digitalWrite(seg1,LOW);
fnd(a[0]);
delay(5);
digitalWrite(seg1,HIGH);
digitalWrite(seg2,LOW);
fnd(a[1]);
delay(5);
digitalWrite(seg2,HIGH);
digitalWrite(seg3,LOW);
fnd(a[2]);
delay(5);
digitalWrite(seg3,HIGH);
digitalWrite(seg4,LOW);
fnd(a[3]);
delay(5);
digitalWrite(seg4,HIGH);
}
|