승강기 관련 소스입니다. 변수 불러오기가 안되네요 ㅠ

-@Naver의 이미지

#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> 블럭을 써주세요. 안 그러면 #include 헤더 날아가고, indentation 날아가고, syntax highlighting은 꿈도 못 꾸게 됩니다.

올리신 글을 직접 보세요. 누가 이렇게 올라온 코드를 읽고 싶겠어요?

2. customKey는 readKey 함수의 지역변수입니다. readKey 함수가 호출되었을 때 만들어지고 반환될 때 사라집니다.

함수의 경계를 넘어 값을 전달하고 싶다면 (1) 전역 변수처럼 함수 호출/반환과 상관없이 살아 있는 변수를 만들어서 쓰거나 (2) 함수 매개변수/반환값 등의 메커니즘으로 값을 전달해 줘야 합니다.

주어진 문제는 (2)의 방법으로 비교적 쉽게 해결이 가능해 보이는군요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.