BASIC4MCU | 질문게시판 | 답변 : 스텝모터 제어하는 소스파일 수정방법 질문드립니다.
페이지 정보
작성자 master 작성일2023-09-15 16:47 조회108회 댓글1건본문
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
//
#define bt_start 2
#define bt_countup 3
#define bt_countdown 4
#define bt_speedup 5
#define bt_speeddown 6
#define DIR 8
#define STEP 9
//
bool flag_countup=0,flag_countdown=0;
int speed=3000,pulse=0,;
//
void setup(){
pinMode(bt_start,INPUT_PULLUP);
pinMode(bt_countup,INPUT_PULLUP); pinMode(bt_countdown,INPUT_PULLUP);
pinMode(bt_speedup,INPUT_PULLUP); pinMode(bt_speeddown,INPUT_PULLUP);
pinMode(DIR,OUTPUT); pinMode(STEP,OUTPUT);
lcd.init(); lcd.backlight();
lcd.setCursor(0,0); lcd.print(" FILAMENT WINDER");
lcd.setCursor(0,1); lcd.print(" CAPSTONE DESIGN");
lcd.setCursor(0,2); lcd.print(" INJE UNIV");
lcd.setCursor(0,3); lcd.print(" HELLO WORLD");
delay(3000);
lcd.clear();
}
//
void lcd_disp(){
int Speed=map(speed,15000,500,0,100);
lcd.setCursor(0,0); lcd.print("StepMotor Pulse ");
lcd.setCursor(0,1); lcd.print("200pulse:1spin ~ "); lcd.print(pulse/200);
lcd.setCursor(0,2); lcd.print("StepMotor Speed");
lcd.setCursor(0,3); lcd.print("0% to 100% ~ "); lcd.print(Speed);
}
//
void run(){
for(int i=0;i<pulse;i++){ //1.8도: 1pulse=360도: 200pulse
digitalWrite(STEP,1); delayMicroseconds(speed);
digitalWrite(STEP,0); delayMicroseconds(speed);
}
}
//
void loop(){
lcd_disp();
//------------------------------------
if(digitalRead(bt_start)==0)run();
//------------------------------------
if(digitalRead(bt_countup)==0){
if(flag_countup==0){ flag_countup=1; pulse=5400; lcd_disp(); }
}
else{
if(flag_countup)flag_countup=0;
}
//------------------------------------
if(digitalRead(bt_countdown)==0){
if(flag_countdown==0){ flag_countdown=1; pulse-=200; if(pulse<0)pulse=0; lcd_disp(); }
}
else{
if(flag_countdown)flag_countdown=0;
}
//------------------------------------
if(digitalRead(bt_speedup)==0){
speed-=100; if(speed<500)speed=500;
lcd_disp();
}
//------------------------------------
if(digitalRead(bt_speeddown)==0){
speed+=100; if(speed>15000)speed=15000;
lcd_disp();
}
}
pulse+-200; 대신 pulse=5400;로 수정하면 됩니다.
if(digitalRead(bt_countup)==0){
if(flag_countup==0){ flag_countup=1; pulse=5400; lcd_disp(); }
}
댓글 1
조회수 108Bs드리프터님의 댓글
Bs드리프터 작성일댓글이 늦었네요.. 그동안 pulse값을 이상하게 수정해서인지 원하는 동작이 안 나왔었네요.. 감사합니다