1. 부품 중에 주파수 조절/발생기 연결 방법 부탁드립니다.
1) Vin +
2) Vin -
3) PWM
4) GND
이렇게 각각 연결해야 하는 것 같은데, GND는 연결 가능한데 Vin +,- 와 PWM을 어디에 연결해야할지 모르겠습니다.
2. 기판을 작동시키고, 다른모드에서 다른화면으로 넘어갈때, 이전 화면에 있던 글씨들이 겹치지 않는 부분은 그대로 다음화면으로 넘어가는데.. 코드값 중에 어디가 잘못된건가요??(사진 첨부하겠습니다.)
3. 온도센서가 아무런 조작을 안했는데도 너무 범위가 넓게 왔다갔다하는데 9도~ 20도 혹시 코드값 문제 있는지 확인 좀 해주실 수 있으실까요?
코드값 참고 부탁드립니다.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const int sw1 = 10;
const int sw2 = 9;
const int sw3 = 8;
int s_state1 = 0;
int s_state2 = 0;
int s_state3 = 0;
int sw_flag_1 = 0;
int sw_flag_2 = 0;
int sw_flag_3 = 0;
int old_state1 = 1;
int old_state2 = 1;
int old_state3 = 1;
int sw1_pulse_flag = 0;
int sw1_sensor_flag = 0;
int sw2_pulse_flag = 0;
int sw2_sensor_flag = 0;
int sw3_select_flag = 0;
int start_flag = 1;
long reset_timer = 0;
int pulse_pin = 2;
float pulsewidth;
float peri;
int freq;
int timer_flag = 0;
int temp_pin = A1;
float temp_value = 0;
float temp = 0;
int cds_value = 0;
float cds_voltage = 0;
int POTEN = A0;
int trig_pin = 3;
int echo_pin = 4;
float duration = 0;
int distance = 0;
int i = 0;
int j = 0;
byte b00[8] {00,00,00,00,00,00,00,00};
byte b16[8] {16,16,16,16,16,16,16,16};
byte b24[8] {24,24,24,24,24,24,24,24};
byte b28[8] {28,28,28,28,28,28,28,28};
byte b30[8] {30,30,30,30,30,30,30,30};
byte b31[8] {31,31,31,31,31,31,31,31};
void setup()
{
pulse_mode_setup();
sw_mode_setup();
ultra_mode_setup();
lcd.createChar(0,b00);
lcd.createChar(1,b16);
lcd.createChar(2,b24);
lcd.createChar(3,b28);
lcd.createChar(4,b30);
lcd.createChar(5,b31);
lcd.init();
lcd.backlight();
}
void loop()
{
if(start_flag == 1)
{
reset_mode();
start_mode();
}
else
{
sw_mode_1();
sw_mode_2();
sw_mode_3();
act_1_mode();
pulse_mode();
sensor_mode();
}
}
void start_mode()
{
for(int i=0; i<5; i++)
{
lcd.setCursor(0,0);
lcd.print("Digital Pulse");
lcd.setCursor(0,1);
lcd.print("Number: A001");
delay(500);
lcd.clear();
delay(500);
}
lcd.setCursor(1,0);
lcd.print("Mode Button");
lcd.setCursor(1,1);
lcd.print("Push Mode SW1!");
}
void act_1_mode()
{
if(sw1_pulse_flag == 1 && sw2_pulse_flag == 0)
{
lcd.setCursor(0,0);
lcd.print("1.Pulse Mode");
lcd.setCursor(1,1);
lcd.print("Push Select!");
}
if(sw1_sensor_flag == 1 && sw2_sensor_flag == 0)
{
lcd.setCursor(0,0);
lcd.print("2.Sensor Mode");
lcd.setCursor(1,1);
lcd.print("Push Select!");
}
}
void pulse_mode_setup()
{
pinMode(pulse_pin,INPUT);
}
void pulse_mode()
{
if(sw2_pulse_flag == 1)
{
pulsewidth = pulseIn(pulse_pin,HIGH);
delay(100);
peri = float((pulsewidth/1000)*2);
freq = (1000/peri);
lcd.setCursor(0,0);
lcd.print("[Pulse Mode]");
lcd.setCursor(0,1);
lcd.print("Freq:");
lcd.print(freq);
lcd.print("Hz");
}
}
void sensor_mode()
{
if(sw2_sensor_flag == 1)
{
temp_mode();
cds_mode();
ultra_mode();
}
}
void temp_mode()
{
temp_value = analogRead(temp_pin);
temp = 0.488155 * (temp_value - 20);
delay(500);
lcd.setCursor(0,0);
lcd.print("T:");
lcd.print(temp,2);
lcd.print("C");
}
void cds_mode()
{
cds_value = analogRead(POTEN);
cds_voltage = cds_value * 5.0 / 1024.0;
lcd.setCursor(9,0);
lcd.print("CDS:");
lcd.setCursor(13,0);
if(cds_voltage >= 2.5)
{
lcd.print("NIG");
}
else
{
lcd.print("DAY");
}
}
void ultra_mode_setup()
{
lcd.createChar(0,b00);
lcd.createChar(1,b16);
lcd.createChar(2,b24);
lcd.createChar(3,b28);
lcd.createChar(4,b30);
lcd.createChar(5,b31);
lcd.init();
lcd.backlight();
pinMode(trig_pin,OUTPUT);
pinMode(echo_pin,INPUT);
Serial.begin(9600);
}
void ultra_mode()
{
digitalWrite(trig_pin,HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin,LOW);
duration = pulseIn(echo_pin,HIGH);
distance = duration / 29 / 2;
lcd.setCursor(0,1);
lcd.print("U:");
lcd.setCursor(2,1);
lcd.print(" ");
int i = distance/5;
int j = distance%5;
Serial.print(distance);
Serial.print(" ");
Serial.print(i);
Serial.print(" ");
Serial.print(j);
lcd.setCursor(2,1);
if(i>=14)
{
for(int k=0; k<14; k++)
{lcd.write(5);}
}
else if(i<14)
{
for(int k=0; k<i; k++)
{lcd.write(5);}
}
lcd.setCursor(i+2,1);
lcd.write(j);
}
void sw_mode_setup()
{
pinMode(sw1,INPUT);
pinMode(sw2,INPUT);
pinMode(sw3,INPUT);
}
void sw_reset()
{
while(! s_state1 && reset_timer ++<= 3000)
{
s_state1 = digitalRead(sw1);
delayMicroseconds(1000);
if(reset_timer == 3000)
{
start_flag = 1;
}
}
reset_timer = 0;
}
void sw_mode_1()
{
s_state1 = digitalRead(sw1);
if(old_state1 != s_state1)
{
old_state1 = s_state1;
if(s_state1 == HIGH)
{
if(sw2_pulse_flag == 0 && sw2_sensor_flag == 0)
{
sw_flag_1++;
if(sw_flag_1 > 1)
{
sw_flag_1 = 0;
}
if(sw_flag_1%2 == 1)
{
sw1_pulse_flag = 1;
sw1_sensor_flag = 0;
}
else
{
sw1_sensor_flag = 1;
sw1_pulse_flag = 0;
}
}
if(sw2_pulse_flag == 1)
{
sw1_pulse_flag = 1;
sw2_pulse_flag = 0;
}
if(sw2_sensor_flag == 1)
{
sw1_sensor_flag = 1;
sw2_sensor_flag = 0;
sw3_select_flag = 0;
}
}
}
sw_reset();
}
void sw_mode_2()
{
s_state2 = digitalRead(sw2);
if(old_state2 != s_state2)
{
old_state2 = s_state2;
if(s_state2 == HIGH)
{
if(sw1_pulse_flag == 1)
{
sw2_pulse_flag = 1;
timer_flag = 1;
}
if(sw1_sensor_flag == 1)
{
sw2_sensor_flag = 1;
}
}
}
}
void sw_mode_3()
{
s_state3 = digitalRead(sw3);
if(old_state3 != s_state3)
{
old_state3 = s_state3;
if(s_state3 == HIGH)
{
s_state1 = 0;
s_state2 = 0;
s_state3 = 0;
sw_flag_1 = 1;
sw_flag_2 = 0;
sw_flag_3 = 0;
old_state1 = 1;
old_state2 = 1;
old_state3 = 1;
sw1_pulse_flag = 1;
sw1_sensor_flag = 0;
sw2_pulse_flag = 0;
sw2_sensor_flag = 0;
}
}
}
void reset_mode()
{
start_flag = 0;
s_state1 = 0;
s_state2 = 0;
s_state3 = 0;
sw_flag_1 = 0;
sw_flag_2 = 0;
sw_flag_3 = 0;
old_state1 = 1;
old_state2 = 1;
old_state3 = 1;
sw1_pulse_flag = 0;
sw1_sensor_flag = 0;
sw2_pulse_flag = 0;
sw2_sensor_flag = 0;
sw3_select_flag = 0;
cds_value = 0;
cds_voltage = 0;
duration = 0;
distance = 0;
temp_value = 0;
temp = 0;
}