승강기 관련 소스입니다. 변수 불러오기가 안되네요 ㅠ
#include
#include
#include //use the Keypad libraries
#include
#define FLOOR1 3
#define FLOOR2 7
#define FLOOR3 15
const int stepsPerRev;
Stepper myStepper(stepsPerRev,A3,A2,A1,A0);
int duration;
int triggerPin = 12;
int echoPin = 13;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] =
{
{
'1','9','4','9' }
,
{
'2','9','5','6' }
,
{
'3','9','9','7' }
,
{
'9','9','9','9' }
};
byte rowPins[ROWS] = { 4, 5, 6, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 8, 9, 10, 11}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2);
int correct = 0;
void setup() {
// put your setup code here, to run once:
myStepper.setSpeed(10);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.init();
lcd.backlight();
}
void loop() {
digitalWrite(triggerPin, LOW);// put your main code here, to run repeatedly:
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin,LOW);
unsigned long duration = pulseIn(echoPin, HIGH);
int distance = duration/29/2; // 초음파 센서 거리 측정
int SAT1 = (distance - FLOOR1);
int SAT2 = (distance - FLOOR2);
int SAT3 = (distance - FLOOR3);
if (distance >=3 && distance <7){
lcd.setCursor(4,0);
lcd.println("1 f l o o r");
}
else if (distance >=7 && distance <15){
lcd.setCursor(4,0);
lcd.println("2 f l o o r");
}
else {
lcd.setCursor(4,0);
lcd.println("3 f l o o r");
}
// 층수에 맞는 위치 지정 후 모터 제어
readKey() ;
switch (customKey)
{
case '3':
while(SAT3 = 0){
if(SAT3 <0){
myStepper.step(stepsPerRev);
}
else{
myStepper.step(-stepsPerRev);
}
}
break;
case '2':
while(SAT2 = 0){
if(SAT2 <0){
myStepper.step(stepsPerRev);
}
else{
myStepper.step(-stepsPerRev);
}
}
break;
case '1':
while(SAT1 = 0){
if(SAT1 <0){
myStepper.step(stepsPerRev);
}
else{
myStepper.step(-stepsPerRev);
}
}
break;
}
delay(100);
}
void readKey()
{
int correct = 0;
char (customKey)= customKeypad.getKey(); //get the key value
}
위의 소스를 저희가 입력을 하였는데
3층 높이의 승강기에 대한 코딩을 하는 중 입니다.
일반 원리는 초음파 센서로 승강기의 상시 높이를 측정하여
저희가 입력한 1층2층3층의 높이에 도달할 때 까지 모터가 작동하여--> 실시간 승강기의 높이(초음파센서로 측정하는)와 저희가 직접 입력한 층수의 높이의 차가 '0'이 될때 까지 모터가 작동
하려합니다.
이때 승강기의 각 층 입력을 4x4키패드를 활용하여 하는데요
이때 저희가 맨 마지막의 void readKey에서 지정한 버튼 값인 customKey라는 것 을
위의 void loop에 변수로 불러오고 싶은데 불러와 지지가 않습니다. ㅠ 어떻게 해야할까요
이 버튼의 값을 받아야 층수가 입력이 되어서 모터가 작동을 하게 하려 합니다.
처음이라 이렇게 올리는게 맞나 싶네요
1. <code> 블럭을 써주세요. 안 그러면
1. <code> 블럭을 써주세요. 안 그러면 #include 헤더 날아가고, indentation 날아가고, syntax highlighting은 꿈도 못 꾸게 됩니다.
올리신 글을 직접 보세요. 누가 이렇게 올라온 코드를 읽고 싶겠어요?
2. customKey는 readKey 함수의 지역변수입니다. readKey 함수가 호출되었을 때 만들어지고 반환될 때 사라집니다.
함수의 경계를 넘어 값을 전달하고 싶다면 (1) 전역 변수처럼 함수 호출/반환과 상관없이 살아 있는 변수를 만들어서 쓰거나 (2) 함수 매개변수/반환값 등의 메커니즘으로 값을 전달해 줘야 합니다.
주어진 문제는 (2)의 방법으로 비교적 쉽게 해결이 가능해 보이는군요.
댓글 달기