아두이노 nextion 질문드립니다 ㅜㅜ
-
게시물 수정 , 삭제는 로그인 필요
코딩 질문드립니다. 코드를 만들었는데 아래의 코드에서 두 가지 문제점이 발생합니다.
1. 스위치 on은 작동되지만 스위치 off가 작동되지 않습니다.
(스위치 ON을 하면 압력센서출력, LCD, 부저 LED 다 켜집니다. 스위치를 다시 눌렀을 때 모두 다 꺼지게 하고 싶습니다)
2. 부저가 delay(100)으로 울리게 하고 싶은데 계속 울리고 있습니다...
나머지 LCD,LED,압력센서는 잘되는데 부저와 스위치 문제 전혀 모르겠습니다 ㅜㅜ
아두이노 나노 사용중이고 NEXTION LCD 사용중입니다.
----------------------------------------------------------------------------------------------------------------------------------
#include <MsTimer2.h>
#include <FastLED.h>
#include <Nextion.h>
#define LED_PIN 5 // arduino nano digital 5 = led pin
#define NUM_LEDS 3 // LED 갯수 3개
#define FSR_PIN A2 // arduino nano analoge 2 = FSR
#define BUZZER_PIN 11 // arduino nano digital 11 = buzzer
#define SWITCH_ON A0 // arduino nano analoge 0 = switch
/* --------------------nano 설정-------------------- */
int FSR_Read; // FSR A0 pin
int FSR_Max = 0;
int timer2_cnt;
int button_cnt=0;
int buzzer_cnt=0;
int switch_value;
int switch_on_flag = 0;
int old_switch_on_flag = 0;
CRGB leds[NUM_LEDS]; // LED 갯수
/* --------------------LCD 설정-------------------- */
int FSR_Read_Perc = 0; // progressbar 표시
char buffer[100] = {0}; // for lcd text
SoftwareSerial HMISerial(1, 2); // arduino nano RX/TX interupt
NexButton button1 = NexButton(0, 1, "button1"); // page0 male
NexButton button2 = NexButton(0, 2, "button2"); // page0 female
NexButton button3 = NexButton(0, 3, "button3"); // page0 old
NexButton button4 = NexButton(3, 1, "button4"); // page3 back
NexButton button5 = NexButton(3, 2, "button5"); // page3 male
NexButton button6 = NexButton(3, 3, "button6"); // page3 female
NexButton button7 = NexButton(1, 1, "button7"); // page1 back
NexButton button8 = NexButton(2, 1, "button8"); // page2 back
NexButton button9 = NexButton(4, 1, "button9"); // page4 back
NexButton button10 = NexButton(5, 1, "button10"); // page5 back
NexText txt1 = NexText(0, 4, "txt1"); // page0 CPRWIZARD
NexProgressBar j1 = NexProgressBar(1, 2, "j1"); // page1 prograssbar
NexProgressBar j2 = NexProgressBar(2, 2, "j2"); // page2 prograssbar
NexProgressBar j3 = NexProgressBar(4, 2, "j3"); // page4 prograssbar
NexProgressBar j4 = NexProgressBar(5, 2, "j4"); // page5 prograssbar
NexText n1 = NexText(1, 3, "n1"); // page1 num
NexText n2 = NexText(2, 3, "n2"); // page2 num
NexText n3 = NexText(4, 3, "n3"); // page3 num
NexText n4 = NexText(5, 3, "n4"); // page4 num
// 화면에 터치를 하는 것들 touch event list에 등록
NexTouch *nex_event_list[] = {
&button1, &button2, &button3, &button4, &button5,
&button6, &button7, &button8, &button9, &button10, NULL
};
/* --------------------nano 설정-------------------- */
void timer2_int() // switch button & buzzer 설정
{
timer2_cnt++;
if(button_cnt < 15)
{
button_cnt++;
}
if(buzzer_cnt >0)
{
buzzer_cnt--;
}
}
void switch_option_check() // switch button 설정
{
switch_value = analogRead(SWITCH_ON);
if(switch_value < 50)
{
if(switch_on_flag==0)
{
switch_on_flag = 1;
buzzer_cnt=10;
buzzer_on();
button_cnt =0;
}
else
{
if(button_cnt > 10)
{
switch_on_flag = 0;
old_switch_on_flag = 0;
digitalWrite(BUZZER_PIN, LOW);
}
}
}
}
void buzzer_on() // buzzer on
{
digitalWrite(BUZZER_PIN, HIGH);
}
void buzzer_off() // buzzer off
{
digitalWrite(BUZZER_PIN, LOW);
}
void read_FSR(void)
{
int flag = 0;
if(flag == 0)
{
FSR_Max = 0;
for(int i=0 ; i< 500 ; ++i)
{
FSR_Read = analogRead(FSR_PIN);
if(FSR_Read >= FSR_Max)
{
FSR_Max = FSR_Read;
flag = 1;
}
}
}
if(flag == 1)
{
flag = 0;
}
}
/* --------------------LCD 설정-------------------- */
void button3_Callback(void *ptr) // 혹시 모르는 Call Back 설정
{
FSR_Read = analogRead(FSR_PIN);
if(FSR_Read) {return;}
char h_FSR_Read[10] = {0};
utoa(int(FSR_Read), h_FSR_Read, 10);
// txt5.setText(h_FSR_Read);
}
void page_1(void) // page1 설정
{
FSR_Read_Perc = (FSR_Max / 10);
j1.setValue(FSR_Read_Perc);
n1.setText(buffer);
memset(buffer, 0, sizeof(buffer));
itoa(FSR_Max, buffer, 10);
}
void page_2(void) // page2 설정
{
FSR_Read_Perc = (FSR_Max / 10);
j2.setValue(FSR_Read_Perc);
n2.setText(buffer);
memset(buffer, 0, sizeof(buffer));
itoa(FSR_Max, buffer, 10);
}
void page_3(void) // page3 설정
{
FSR_Read_Perc = (FSR_Max / 10);
j3.setValue(FSR_Read_Perc);
n3.setText(buffer);
memset(buffer, 0, sizeof(buffer));
itoa(FSR_Max, buffer, 10);
}
void page_4(void) // page4 설정
{
FSR_Read_Perc = (FSR_Max / 10);
j4.setValue(FSR_Read_Perc);
n4.setText(buffer);
memset(buffer, 0, sizeof(buffer));
itoa(FSR_Max, buffer, 10);
}
void setup()
{
/* --------------------nano 설정-------------------- */
Serial.begin(9600);
pinMode(SWITCH_ON, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
FastLED.addLeds<WS2812, LED_PIN, GRB> (leds, NUM_LEDS);
MsTimer2::set(10, timer2_int); // 50ms timer
MsTimer2::start();
/* --------------------LCD 설정-------------------- */
nexInit(); // nextion 초기화
pinMode(FSR_PIN, INPUT);
}
void loop()
{
switch_option_check();
if(switch_on_flag==1)
{
if(buzzer_cnt > 8)
{
buzzer_on();
}
else if(buzzer_cnt == 0)
{
buzzer_cnt = 10;
buzzer_on();
}
else
{
buzzer_off();
}
read_FSR(); // 외부함수 FSR 불러오기
page_1(); // LCD page1 불러오기
page_2(); // LCD page2 불러오기
page_3(); // LCD page3 불러오기
page_4(); // LCD page4 불러오기
nexLoop(nex_event_list); // nextion에서 발생하는 이벤트를 라이브러리로 전달
if(FSR_Max > 1 && FSR_Max <= 500)
{
leds[0] = CRGB (0, 0 ,0);
leds[1] = CRGB (0 ,0 ,0);
leds[2] = CRGB (0 ,0 ,0);
FastLED.show();
}
else if(FSR_Max > 500 && FSR_Max <=600)
{
leds[0] = CRGB (15, 10 ,0);
leds[1] = CRGB (15 ,10 ,0);
leds[2] = CRGB (15 ,10 ,0);
FastLED.show();
}
else if(FSR_Max > 600 && FSR_Max <= 850)
{
leds[0] = CRGB (0 ,10 ,0);
leds[1] = CRGB (0 ,10 ,0);
leds[2] = CRGB (0 ,10 ,0);
FastLED.show();
}
else if(FSR_Max > 850)
{
leds[0] = CRGB (10 ,0 ,0);
leds[1] = CRGB (10 ,0 ,0);
leds[2] = CRGB (10 ,0 ,0);
FastLED.show();
}
Serial.print("FSR_Read ");
Serial.println(FSR_Read);
Serial.print("FSR_Max ");
Serial.println(FSR_Max);
}
delay(100);
}
----------------------------------------------------------------------------------------------------------------------------------
코딩 질문드립니다. 코드를 만들었는데 아래의 코드에서 두 가지 문제점이 발생합니다.
1. 스위치 on은 작동되지만 스위치 off가 작동되지 않습니다.
(스위치 ON을 하면 압력센서출력, LCD, 부저 LED 다 켜집니다. 스위치를 다시 눌렀을 때 모두 다 꺼지게 하고 싶습니다)
2. 부저가 delay(100)으로 울리게 하고 싶은데 계속 울리고 있습니다...
나머지 LCD,LED,압력센서는 잘되는데 부저와 스위치 문제 전혀 모르겠습니다 ㅜㅜ
아두이노 나노 사용중이고 NEXTION LCD 사용중입니다.
----------------------------------------------------------------------------------------------------------------------------------
#include <MsTimer2.h>
#include <FastLED.h>
#include <Nextion.h>
#define LED_PIN 5 // arduino nano digital 5 = led pin
#define NUM_LEDS 3 // LED 갯수 3개
#define FSR_PIN A2 // arduino nano analoge 2 = FSR
#define BUZZER_PIN 11 // arduino nano digital 11 = buzzer
#define SWITCH_ON A0 // arduino nano analoge 0 = switch
/* --------------------nano 설정-------------------- */
int FSR_Read; // FSR A0 pin
int FSR_Max = 0;
int timer2_cnt;
int button_cnt=0;
int buzzer_cnt=0;
int switch_value;
int switch_on_flag = 0;
int old_switch_on_flag = 0;
CRGB leds[NUM_LEDS]; // LED 갯수
/* --------------------LCD 설정-------------------- */
int FSR_Read_Perc = 0; // progressbar 표시
char buffer[100] = {0}; // for lcd text
SoftwareSerial HMISerial(1, 2); // arduino nano RX/TX interupt
NexButton button1 = NexButton(0, 1, "button1"); // page0 male
NexButton button2 = NexButton(0, 2, "button2"); // page0 female
NexButton button3 = NexButton(0, 3, "button3"); // page0 old
NexButton button4 = NexButton(3, 1, "button4"); // page3 back
NexButton button5 = NexButton(3, 2, "button5"); // page3 male
NexButton button6 = NexButton(3, 3, "button6"); // page3 female
NexButton button7 = NexButton(1, 1, "button7"); // page1 back
NexButton button8 = NexButton(2, 1, "button8"); // page2 back
NexButton button9 = NexButton(4, 1, "button9"); // page4 back
NexButton button10 = NexButton(5, 1, "button10"); // page5 back
NexText txt1 = NexText(0, 4, "txt1"); // page0 CPRWIZARD
NexProgressBar j1 = NexProgressBar(1, 2, "j1"); // page1 prograssbar
NexProgressBar j2 = NexProgressBar(2, 2, "j2"); // page2 prograssbar
NexProgressBar j3 = NexProgressBar(4, 2, "j3"); // page4 prograssbar
NexProgressBar j4 = NexProgressBar(5, 2, "j4"); // page5 prograssbar
NexText n1 = NexText(1, 3, "n1"); // page1 num
NexText n2 = NexText(2, 3, "n2"); // page2 num
NexText n3 = NexText(4, 3, "n3"); // page3 num
NexText n4 = NexText(5, 3, "n4"); // page4 num
// 화면에 터치를 하는 것들 touch event list에 등록
NexTouch *nex_event_list[] = {
&button1, &button2, &button3, &button4, &button5,
&button6, &button7, &button8, &button9, &button10, NULL
};
/* --------------------nano 설정-------------------- */
void timer2_int() // switch button & buzzer 설정
{
timer2_cnt++;
if(button_cnt < 15)
{
button_cnt++;
}
if(buzzer_cnt >0)
{
buzzer_cnt--;
}
}
void switch_option_check() // switch button 설정
{
switch_value = analogRead(SWITCH_ON);
if(switch_value < 50)
{
if(switch_on_flag==0)
{
switch_on_flag = 1;
buzzer_cnt=10;
buzzer_on();
button_cnt =0;
}
else
{
if(button_cnt > 10)
{
switch_on_flag = 0;
old_switch_on_flag = 0;
digitalWrite(BUZZER_PIN, LOW);
}
}
}
}
void buzzer_on() // buzzer on
{
digitalWrite(BUZZER_PIN, HIGH);
}
void buzzer_off() // buzzer off
{
digitalWrite(BUZZER_PIN, LOW);
}
void read_FSR(void)
{
int flag = 0;
if(flag == 0)
{
FSR_Max = 0;
for(int i=0 ; i< 500 ; ++i)
{
FSR_Read = analogRead(FSR_PIN);
if(FSR_Read >= FSR_Max)
{
FSR_Max = FSR_Read;
flag = 1;
}
}
}
if(flag == 1)
{
flag = 0;
}
}
/* --------------------LCD 설정-------------------- */
void button3_Callback(void *ptr) // 혹시 모르는 Call Back 설정
{
FSR_Read = analogRead(FSR_PIN);
if(FSR_Read) {return;}
char h_FSR_Read[10] = {0};
utoa(int(FSR_Read), h_FSR_Read, 10);
// txt5.setText(h_FSR_Read);
}
void page_1(void) // page1 설정
{
FSR_Read_Perc = (FSR_Max / 10);
j1.setValue(FSR_Read_Perc);
n1.setText(buffer);
memset(buffer, 0, sizeof(buffer));
itoa(FSR_Max, buffer, 10);
}
void page_2(void) // page2 설정
{
FSR_Read_Perc = (FSR_Max / 10);
j2.setValue(FSR_Read_Perc);
n2.setText(buffer);
memset(buffer, 0, sizeof(buffer));
itoa(FSR_Max, buffer, 10);
}
void page_3(void) // page3 설정
{
FSR_Read_Perc = (FSR_Max / 10);
j3.setValue(FSR_Read_Perc);
n3.setText(buffer);
memset(buffer, 0, sizeof(buffer));
itoa(FSR_Max, buffer, 10);
}
void page_4(void) // page4 설정
{
FSR_Read_Perc = (FSR_Max / 10);
j4.setValue(FSR_Read_Perc);
n4.setText(buffer);
memset(buffer, 0, sizeof(buffer));
itoa(FSR_Max, buffer, 10);
}
void setup()
{
/* --------------------nano 설정-------------------- */
Serial.begin(9600);
pinMode(SWITCH_ON, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
FastLED.addLeds<WS2812, LED_PIN, GRB> (leds, NUM_LEDS);
MsTimer2::set(10, timer2_int); // 50ms timer
MsTimer2::start();
/* --------------------LCD 설정-------------------- */
nexInit(); // nextion 초기화
pinMode(FSR_PIN, INPUT);
}
void loop()
{
switch_option_check();
if(switch_on_flag==1)
{
if(buzzer_cnt > 8)
{
buzzer_on();
}
else if(buzzer_cnt == 0)
{
buzzer_cnt = 10;
buzzer_on();
}
else
{
buzzer_off();
}
read_FSR(); // 외부함수 FSR 불러오기
page_1(); // LCD page1 불러오기
page_2(); // LCD page2 불러오기
page_3(); // LCD page3 불러오기
page_4(); // LCD page4 불러오기
nexLoop(nex_event_list); // nextion에서 발생하는 이벤트를 라이브러리로 전달
if(FSR_Max > 1 && FSR_Max <= 500)
{
leds[0] = CRGB (0, 0 ,0);
leds[1] = CRGB (0 ,0 ,0);
leds[2] = CRGB (0 ,0 ,0);
FastLED.show();
}
else if(FSR_Max > 500 && FSR_Max <=600)
{
leds[0] = CRGB (15, 10 ,0);
leds[1] = CRGB (15 ,10 ,0);
leds[2] = CRGB (15 ,10 ,0);
FastLED.show();
}
else if(FSR_Max > 600 && FSR_Max <= 850)
{
leds[0] = CRGB (0 ,10 ,0);
leds[1] = CRGB (0 ,10 ,0);
leds[2] = CRGB (0 ,10 ,0);
FastLED.show();
}
else if(FSR_Max > 850)
{
leds[0] = CRGB (10 ,0 ,0);
leds[1] = CRGB (10 ,0 ,0);
leds[2] = CRGB (10 ,0 ,0);
FastLED.show();
}
Serial.print("FSR_Read ");
Serial.println(FSR_Read);
Serial.print("FSR_Max ");
Serial.println(FSR_Max);
}
delay(100);
}
----------------------------------------------------------------------------------------------------------------------------------
#아두이노 nextion #nextion 아두이노 통신 #nextion hmi lcd 아두이노