BASIC4MCU | 질문게시판 | 답변 : 아두이노 코딩 문제 도움주시면 감사하겠습니다.
페이지 정보
작성자 master 작성일2024-04-06 05:45 조회2,271회 댓글1건본문
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <HCMotor.h>
#include <Wire.h>
/* 모터드라이버 연결핀 */
#define DIR_PIN A3 //스텝모터드라이버 DIR 연결핀
#define CLK_PIN A2 //스텝모터드라이버 CLK 연결핀
/* 리미트스위치 연결핀 */
#define LLIMIT_PIN A0
#define RLIMIT_PIN A1
Servo airpump_12;
Servo vaccumpump_13;
Servo solenoide_10;
Servo servoMotor;
unsigned long servoMoveTime=0,previousMillis=0;
const long interval=8000; // 2초
/* HCMotor 라이브러리 인스턴스 생성 */
HCMotor HCMotor;
int Speed=10;
//const int LINEAR_ACTUATOR_PIN=A0;
//const int LLIMIT_SENSOR_PIN=A0;
//const int RLIMIT_SENSOR_PIN=A1;
//const int AIR_PUMP_PIN=12;
const int SERVO_PIN=11;
const int VACUUM_PUMP_PIN=13;
const int AIR_PUMP_PIN=12;
//const int START_BUTTON_PIN=A2;
LiquidCrystal_I2C lcd(0x27,20,4); //LiquidCrystal lcd(12,11,10,16,15,14);
const int ROWS=4,COLS=4;
char keys[ROWS][COLS]={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS]={9,8,7,6};
byte colPins[COLS]={5,4,3,2};
Keypad keypad=Keypad(makeKeymap(keys),rowPins,colPins,ROWS,COLS);
//
//bool airPumpRunning=false; // 에어펌프 작동 여부를 나타내는 변수
bool linearActuatorMoving=false,vacuumPumpRunning=false,servoMoving=false,servoAtZero=true;
int key=0; // 사용자 입력 시간(초)
//unsigned long startTime,elapsedTime,endTime;
unsigned long airPumpStartTime=0,servoEndTime=0,servoStopTime=0;
String input_value;
//
void setup(){
lcd.begin(16,2);
//pinMode(LINEAR_ACTUATOR_PIN,OUTPUT); pinMode(AIR_PUMP_PIN,OUTPUT); pinMode(START_BUTTON_PIN,INPUT_PULLUP);
//pinMode(LLIMIT_PIN,INPUT); pinMode(RLIMIT_PIN,INPUT);
pinMode(VACUUM_PUMP_PIN,OUTPUT);
servoMotor.attach(SERVO_PIN);
airpump_12.attach(12);
vaccumpump_13.attach(13);
solenoide_10.attach(10);
HCMotor.Init(); /* 라이브러리 초기화 */
lcd.backlight();
HCMotor.attach(0,STEPPER,CLK_PIN,DIR_PIN); /* 모터0을 스텝모터로 설정하고 연결된 핀을 지정 */
HCMotor.Steps(0,CONTINUOUS); /* 모터를 연속동작모드로 설정 */
servoMotor.write(0);
solenoide_10.write(0);
input_value.reserve(120);
}
//
void startProcess(){
//linearActuatorMoving=true;
HCMotor.Direction(0,FORWARD); Speed=10; HCMotor.DutyCycle(0,Speed/3);
//startTime=millis();
lcd.clear(); lcd.print("Process started");
// Start moving linear actuator digitalWrite(LINEAR_ACTUATOR_PIN,HIGH);
}
//
void stopLinearActuator(){
//linearActuatorMoving=false; digitalWrite(LINEAR_ACTUATOR_PIN,LOW);
Speed=0; HCMotor.DutyCycle(0,Speed);
}
//
//void startServoMotor(){ servoMotor.write(90); elapsedTime=millis()-startTime; }
//void stopServoMotor() { servoMotor.write( 0); }
//
void reverseLinearActuator(){
//digitalWrite(LINEAR_ACTUATOR_PIN,LOW);
HCMotor.Direction(0,REVERSE); Speed=10; HCMotor.DutyCycle(0,Speed/3);
// Code for reversing linear actuator
}
//
void stopVacuumPump(){
vaccumpump_13.write(0);
reverseLinearActuator();
//vacuumPumpRunning=false; digitalWrite(VACUUM_PUMP_PIN,LOW);
}
//
void startVacuumPump(){
//unsigned long currentTime=millis(); vacuumPumpRunning=true; digitalWrite(VACUUM_PUMP_PIN,HIGH);
solenoide_10.write(180); delay(500);
vaccumpump_13.write(180); delay(2000); stopVacuumPump();
//currentTime=millis();
}
//
void stopAirPump(){
//airPumpRunning=false; digitalWrite(AIR_PUMP_PIN,LOW); // 에어펌프 작동 플래그 해제
airpump_12.write(0); startVacuumPump();
}
//
void step2(){
char key=keypad.getKey();
if(key){
lcd.print(key);
if(key=='*'){ input_value=""; lcd.clear(); } // clear input value
else if(key=='#'){
unsigned long timer=input_value.toInt();
timer=timer*1000; // time in seconds
//unsigned long currentMillis=millis(); unsigned long previousMillis=currentMillis;
//while(currentMillis-previousMillis<interval){ currentMillis=millis(); } // Update current time
servoMotor.write(90); delay(timer); // Wait for 1 second(adjust if needed)
servoMotor.write( 0); stopAirPump();
input_value=""; // clear input value
}
else{ input_value+=key; } // append new character to input value string
}
}
//
void startAirPump(){
//airPumpRunning=true; // 에어펌프 작동 플래그 설정
airpump_12.write(180); step2();
//servoMoveTime=millis();
//if(millis()-servoMoveTime==5000){}
//digitalWrite(AIR_PUMP_PIN,HIGH);
}
//
void loop(){
unsigned long currentTime=millis();
char key=keypad.getKey();
if(key !=NO_KEY){
lcd.setCursor(0,0); lcd.println(key);
//lcd.setCursor(4,0); lcd.print("sec");
if(key=='A'){ startProcess(); delay(3000); lcd.clear(); }
/*
if(key!='A'){
int duration=(key-'0')*1000; // 입력된 숫자를 millisecond 단위로 변환
servoMotor.write(90); delay(duration); servoMotor.write(0); // 서보모터를 열고 닫음
}
*/
}
if(/*linearActuatorMoving&&*/ digitalRead(RLIMIT_PIN)==LOW){
stopLinearActuator(); startAirPump(); // 에어펌프 시작
//lcd.print("AirPumping"); delay(3000); lcd.clear(); // 이거 있으면 키패드가 안눌림
}
/*
if(currentTime-airPumpStartTime>=5000){
//servoMotor.write(90); // 서보 모터를 90도로 움직임
servoEndTime=currentTime; // 서보 모터가 움직인 시간 기록
}
if(servoMoving && currentTime-servoEndTime>=key*1000 && servoAtZero){
servoMotor.write(0); // 서보 모터를 0도로 움직임
servoAtZero=false; // 서보 모터가 0도에 도달함
servoStopTime=currentTime; // 서보 모터가 멈춘 시간 기록
stopAirPump();
startVacuumPump();
}
if(vacuumPumpRunning && millis()-currentTime>=15000){ stopVacuumPump(); reverseLinearActuator(); }
*/
if(/*linearActuatorMoving && */digitalRead(LLIMIT_PIN)==LOW){ stopLinearActuator();}
}
함수든 변수든 사용하기 전에 선언이 먼저 되어 있어야 합니다.
이 순서가 바뀔 때에는 미리 프로토타입 선언을 먼저 해주어야 됩니다.
코드 윗쪽에 함수 프로토타입 선언을 해둔다면 순서를 신경쓰지 않아도 되겠죠
(이 답변에서는 함수 호출 순서를 맞추었습니다.)
코드 실행에 관한 문제는 직접 돌려가면서 코드를 디버깅 해야하는데
물건을 가진 사람만 가능하므로 본인이 직접 하셔야 합니다.
댓글 1
조회수 2,271제발요님의 댓글
제발요 작성일답변 감사합니다. 수정해주신 코드로 다시 시도해보겠습니다.