교수님 안녕하세요?
이전에 질문했던 temp 변동값은 해결했습니다.
추가 질문 있습니다.
1. 코드값 중에 lcd.init() 있지 않습니까? 근데 일부 시험장에서 시험치고 온 수험생들 말을 들어보니
따로 연습할때는 lcd.init()가 잘 작동되지만, 시험장에서는 안된다면서 lcd.begin(16,2) 이걸로 바꿔서
코드값 쓰라는데,, 막상 코드값 바꿔서 넣으면 메가보드가 작동이 되지 않습니다.
시험장에서도 lcd.init()로 그냥 써도 문제 없겠지요?
2. 올배움 채팅으로 동영상 하나 보내드렸는데,
유형 3번에서 sensor mode 진입하려고하면 다시 1.clock mode 화면으로 강제 전환됩니다.
진입했다가 바로 강제 전환되기도 하구요.
이전에는 잘 작동 됐던거 같은데 갑자기 안되길래 mega보드 문제인가 싶었는데
유형1,2번은 sensor mode에 정상 진입합니다.
유형3 코드값 첨부할테니 확인 부탁드립니다.
#include <EEPROM.h>
#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_clock_flag = 0;
int sw1_sensor_flag = 0;
int sw2_clock_flag = 0;
int sw2_sensor_flag = 0;
int sw3_select_flag = 0;
int eeprom_flag = 0;
int start_flag = 1;
long reset_timer = 0;
const byte pulse_pin = 2;
int timer_status = 0;
int old_timer_status = 0;
int old_ss = 0;
int ms = 0;
int mms = 0;
int ss = 50;
int mm = 59;
int hh = 11;
String timer_1ms_str = "0";
String timer_10ms_str = "0";
String timer_1sec_str = "0";
String timer_10sec_str = "5";
String timer_1min_str = "9";
String timer_10min_str = "5";
String timer_1hour_str = "1";
String timer_10hour_str = "1";
int timer_mode_flag = 0;
int cds_value = 0;
int old_cds_value = 0;
int avg_cds = 0;
int old_avg_cds = 0;
int cds_count = 0;
String cds_value_str1000;
String cds_value_str100;
String cds_value_str10;
String cds_value_str1;
int cds_pin = A0;
int trig_pin = 3;
int echo_pin = 4;
int duration = 0;
int distance = 0;
int temp_pin = A1;
int temp_value = 0;
float temp = 0;
float old_temp = 0;
float avg_temp = 0;
float eeprom_avg_temp = 0;
int temp_count = 0;
String temp_str;
void setup()
{
timer_mode_setup();
clock_mode_setup();
sw_mode_setup();
cds_mode_setup();
ultra_mode_setup();
temp_mode_setup();
lcd.init();
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();
clock_mode();
sensor_mode();
timer_mode();
}
}
void start_mode()
{
eeprom_flag = EEPROM.read(0);
for(int i=0; i<5; i++)
{
lcd.setCursor(0,0);
lcd.print("Digital Sensor ");
lcd.setCursor(0,1);
lcd.print("Number: A001");
delay(500);
lcd.clear();
delay(500);
}
lcd.setCursor(0,0);
lcd.print("Mode Button ");
lcd.setCursor(0,1);
lcd.print(" Push Mode SW1! ");
}
void act_1_mode()
{
if(sw1_clock_flag == 1)
{
lcd.setCursor(0,0);
lcd.print("1.Clock Mode ");
lcd.setCursor(0,1);
lcd.print(" Push Select! ");
}
if(sw1_sensor_flag == 1)
{
lcd.setCursor(0,0);
lcd.print("2.Sensor Mode ");
lcd.setCursor(0,1);
lcd.print(" Push Select! ");
}
}
void clock_mode_setup()
{
pinMode(pulse_pin,INPUT);
}
void clock_mode()
{
if(sw2_clock_flag == 1)
{
lcd.setCursor(0,0);
lcd.print("[ DigitalClock ]");
}
}
void sensor_mode()
{
if(sw2_sensor_flag == 1)
{
cds_mode();
ultra_mode();
temp_mode();
timer_mode();
}
}
void cds_mode_setup()
{
Serial.begin(9600);
}
void cds_mode()
{
cds_value = analogRead(cds_pin);
Serial.println(cds_value);
avg_cds = (avg_cds+cds_value)/2;
cds_count ++;
if(cds_count == 30)
{
cds_count = 0;
if(old_cds_value != avg_cds)
{
old_cds_value = avg_cds;
cds_value_str1000 = String(avg_cds/1000);
cds_value_str100 = String((avg_cds%1000)/100);
cds_value_str10 = String((avg_cds%100)/10);
cds_value_str1 = String(avg_cds%10);
lcd.setCursor(0,1);
lcd.print("CDS:" + cds_value_str1000 + cds_value_str100 + cds_value_str10 + cds_value_str1);
}
}
}
void ultra_mode_setup()
{
pinMode(trig_pin,OUTPUT);
pinMode(echo_pin,INPUT);
}
void ultra_mode()
{
if(old_ss != ss)
{
old_ss = ss;
digitalWrite(trig_pin,HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin,LOW);
duration = pulseIn(echo_pin,HIGH);
distance = duration / 29 / 2;
if(distance <= 15)
{
reset_mode();
sw1_clock_flag = 1;
sw_flag_1 = 1;
}
}
}
void temp_mode_setup()
{}
void temp_mode()
{
temp_value = analogRead(temp_pin);
temp = 0.488155 * (analogRead(temp_pin) - 20);
avg_temp = (avg_temp + temp)/2;
temp_count ++;
if(temp_count == 30)
{
temp_count = 0;
if(old_temp != avg_temp)
{
old_temp = avg_temp;
temp_str = String(avg_temp);
lcd.setCursor(8,1);
lcd.print(" T:" + temp_str);
}
}
}
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_clock_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_clock_flag = 1;
sw1_sensor_flag = 0;
}
else
{
sw1_sensor_flag = 1;
sw1_clock_flag = 0;
}
}
if(sw2_clock_flag == 1)
{
sw1_clock_flag = 1;
sw2_clock_flag = 0;
}
if(sw2_sensor_flag == 1)
{
sw1_sensor_flag = 1;
sw2_sensor_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_clock_flag == 1)
{
timer_mode_flag = 1;
sw2_clock_flag = 1;
sw1_clock_flag = 0;
}
else
{
sw2_clock_flag = 0;
}
if(sw1_sensor_flag == 1)
{
timer_mode_flag = 1;
sw2_sensor_flag = 1;
sw1_sensor_flag = 0;
lcd.clear();
}
else
{
sw2_sensor_flag = 0;
}
}
}
}
void sw_mode_3()
{
s_state3 = digitalRead(sw3);
if(old_state3 != s_state3)
{
old_state3 = s_state3;
if(s_state3 == HIGH)
{
if(sw2_sensor_flag == 1)
{
EEPROM.write(0,hh);
EEPROM.write(1,mm);
EEPROM.write(2,ss);
EEPROM.write(3,ms);
EEPROM.write(4,cds_value);
EEPROM.put(5,avg_temp);
}
else if(sw1_clock_flag == 0 && sw1_sensor_flag == 0 && sw2_clock_flag == 0 && sw2_sensor_flag == 0)
{
eeprom_mode();
}
}
}
}
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_clock_flag = 0;
sw1_sensor_flag = 0;
sw2_clock_flag = 0;
sw2_sensor_flag = 0;
sw3_select_flag = 0;
timer_status = 0;
old_timer_status = 0;
ms = 0;
ss = 50;
mm = 59;
hh = 11;
timer_mode_flag = 0;
cds_value = 0;
duration = 0;
distance = 0;
temp_value = 0;
temp = 0;
}
void timer_mode_setup()
{}
void ms_Plus()
{
ms ++;
if(ms == 100)
{
ms = 0;
ss++;
}
if(ss == 60)
{
ss = 0;
mm++;
}
if(mm==60)
{
mm = 0;
hh ++;
}
if(hh==24)
{
hh =0;
}
}
void timer_mode()
{
if(timer_mode_flag == 1)
{
attachInterrupt(digitalPinToInterrupt(pulse_pin), ms_Plus, RISING);
}
else
{
hh = 11;
mm = 59;
ss = 50;
ms = 0;
}
timer_1hour_str = String(hh%10);
timer_10hour_str = String(hh/10);
timer_1min_str = String(mm%10);
timer_10min_str = String(mm/10);
timer_1sec_str = String(ss%10);
timer_10sec_str = String(ss/10);
timer_1ms_str = String(ms%10);
timer_10ms_str = String(ms/10);
if(sw2_sensor_flag == 1)
{
lcd.setCursor(0,0);
if(hh<12)
{
lcd.print("[AM]" + timer_10hour_str + timer_1hour_str + ":" +
timer_10min_str + timer_1min_str + ":" +
timer_10sec_str + timer_1sec_str + ":" +
timer_10ms_str + timer_1ms_str);
}
else if(hh>=12)
{
lcd.print("[PM]" + timer_10hour_str + timer_1hour_str + ":" +
timer_10min_str + timer_1min_str + ":" +
timer_10sec_str + timer_1sec_str + ":" +
timer_10ms_str + timer_1ms_str);
}
}
if(sw2_clock_flag == 1)
{
lcd.setCursor(0,1);
if(hh<12)
{
lcd.print("[AM]" + timer_10hour_str + timer_1hour_str + ":" +
timer_10min_str + timer_1min_str + ":" +
timer_10sec_str + timer_1sec_str + ":" +
timer_10ms_str + timer_1ms_str);
}
else if(hh>=12)
{
lcd.print("[PM]" + timer_10hour_str + timer_1hour_str + ":" +
timer_10min_str + timer_1min_str + ":" +
timer_10sec_str + timer_1sec_str + ":" +
timer_10ms_str + timer_1ms_str);
}
}
}
void eeprom_mode()
{
hh = EEPROM.read(0);
mm = EEPROM.read(1);
ss = EEPROM.read(2);
ms = EEPROM.read(3);
cds_value = EEPROM.read(4);
EEPROM.get(5,eeprom_avg_temp);
timer_1ms_str = String(ms%10);
timer_10ms_str = String(ms/10);
timer_1sec_str = String(ss%10);
timer_10sec_str = String(ss/10);
timer_1min_str = String(mm%10);
timer_10min_str = String(mm/10);
timer_1hour_str = String(hh%10);
timer_10hour_str = String(hh/10);
lcd.setCursor(0,0);
if(hh<12)
{
lcd.print("[AM]" + timer_10hour_str + timer_1hour_str + ":" +
timer_10min_str + timer_1min_str + ":" +
timer_10sec_str + timer_1sec_str + ":" +
timer_10ms_str + timer_1ms_str);
}
else if(hh>=12)
{
lcd.print("[PM]" + timer_10hour_str + timer_1hour_str + ":" +
timer_10min_str + timer_1min_str + ":" +
timer_10sec_str + timer_1sec_str + ":" +
timer_10ms_str + timer_1ms_str);
}
cds_value_str1000 = String(cds_value/1000);
cds_value_str100 = String((cds_value%1000)/100);
cds_value_str10 = String((cds_value%100)/10);
cds_value_str1 = String(cds_value%10);
lcd.setCursor(0,1);
lcd.print("CDS:" + cds_value_str1000 + cds_value_str100 + cds_value_str10 + cds_value_str1);
temp_str = String(eeprom_avg_temp);
lcd.setCursor(8,1);
lcd.print(" T:" + temp_str);
}