국내 최초 국가기술자격증 전문 사이트! - 올배움kisa


저작권지킴이

저작권지킴이 캠페인안내

고객센터

고객센터

메뉴 바로가기

쇼셜 바로가기

  • 유튜브-국가기술자격증TV
  • 올배움kisa 페이스북
  • 올배움kisa 인스타그램
  • kisa 블로그
  • 올배움kisa - 카카오톡 채널
  • 인터넷 원서접수 - 큐넷
  • 네이버카페-자격증 닷컴
  1. >
  2. 커뮤니티
  3. >
  4. 베스트 질문 답변

베스트 질문 답변

 
작성일 : 21-10-22 06:48
 
[전자산업기사(박성호)] 유형3번 질문

교수님 안녕하세요?

이전에 질문했던 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);

 }

 


올배움 21-10-22 09:21
 
안녕하세요.
답변 드리겠습니다.

1. init 과 begin 의 차이는 라이브러리 버전에 따른 차이입니다.
기본적으로는 init을 쓰지만 구버전에서는 begin으로 입력을 해야합니다.

2. 문의주신내용은 영상을 확인해봤는데 초음파센서가 보이지 않습니다.
3번 문제의 경우 초음파센서 15cm 이내에 물체가 확인되면 1번으로 강제로 돌아오는 기능이 있습니다. 회원님께서 올려주신 코드로 테스트 해본 결과 문제가 없는데 혹시 초음파센서앞에 물체가 있는지 확인후 다시 테스트를 해보시는게 좋을 것 같습니다.
 

Total 4,084

번호 강좌명 제목
2754 화약취급기능사(이정훈) 2019년 4회 필답 6번문제 질문입니다. (1)
2753 건설재료시험기사(김현우) 기출 2010 1회 8번 (기본적인것들 질문합니다ㅠㅠ) (1)
2752 가스산업기사(최갑규) 12년 4회 7번 (1)
2751 건설재료시험기사(김현우) 콘크리트 슬럼프시험 (1)
2750 건축기사 20년책 636페이지 6장 철골공사 핵심필수문제 3번 질문입니다 (1)
2749 전자산업기사(박성호) 교수님 2과제에 대해 2가지 질문이 있습니다. (1)
2748 전자산업기사(박성호) 유형1번 질문 (1)
2747 건설재료시험기사(김현우) 물결합재비 보정 (1)
2746 콘크리트산업기사(김현우) 시험장관련 (1)
2745 식품기사(박대준) 강의 10강에 나오는 크로마토그래피의 원리와 고정상에 대한 질문입니다. (1)
   121  122  123  124  125  126  127  128  129  130  131  132  133  134  135