โค้ด aduino ควบคุมมอเตอร์ หาผู้รู้ตอบคำถาม

กระทู้คำถาม
รบกวนคนรู้เรื่องโค้ด อธิบายโค้ดนี้ให้หน่อยแต่ละบรรทัดคือยังไงคำสั่งอะไรบ้าง


#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7);
#include <Keypad.h>

boolean motor_start = false;
const byte pin_a = 2;   //for encoder pulse A
const byte pin_fwd = 12; //for H-bridge: run motor forward
const byte pin_pwm = 11; //for H-bridge: motor speed
int encoder = 0;
double pv_speed = 0;
double set_speed = 0;
double pwm_pulse = 0;     //this value is 0~255
int timer1_counter; //for timer

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {3, 4, 5, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 8, A0, A1}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char key;
String command;


void setup() {
  lcd.begin (16,2);
  lcd.setBacklightPin(3,POSITIVE);
  lcd.setBacklight(HIGH);
  pinMode(pin_a,INPUT_PULLUP);
  pinMode(pin_fwd,OUTPUT);
  pinMode(pin_pwm,OUTPUT);
  attachInterrupt(digitalPinToInterrupt(pin_a), detect_a, RISING);
  // start serial port at 9600 bps:
  Serial.begin(9600);
  //--------------------------timer setup
  noInterrupts();           // disable all interrupts
  TCCR1A = 0;
  TCCR1B = 0;
  timer1_counter = 59286;   // preload timer 65536-16MHz/256/2Hz (34286 for 0.5sec) (59286 for 0.1sec)

  
  TCNT1 = timer1_counter;   // preload timer
  TCCR1B |= (1 << CS12);    // 256 prescaler
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  interrupts();             // enable all interrupts
  //--------------------------timer setup

  analogWrite(pin_pwm,0);   //stop motor
  digitalWrite(pin_fwd,0);  //stop motor
}

void loop() {
  
  key = keypad.getKey();
    if (key){
    //  tone(A0,2000,20);
    Serial.println(key);
  }
  if(key=='1' || key=='2' || key=='3' || key=='4' || key=='5' || key=='6' || key=='7' || key=='8' || key=='9' || key=='0'){
  command += key;
  if(command.length()>4){
    command="";
    lcd.clear();
  }
    }
    
lcd.setCursor(0, 0);
lcd.print("RPM:");
lcd.print(pv_speed,0);
lcd.print("    ");

lcd.setCursor(0, 1);
lcd.print("SET:");
lcd.print(command);
lcd.print("    ");

if(key=='A'){
   set_speed=command.toInt();
   pwm_pulse=map(command.toInt(),0,3400,0,255);
    digitalWrite(pin_fwd,1);      //run motor run forward
    motor_start = true;
      lcd.clear();
    }
    
     if(key=='B'){
    digitalWrite(pin_fwd,0);
    motor_start = false;
      lcd.clear();
    }
    
    if(key=='D'){
      command="";
      lcd.clear();
    }
}

void detect_a() {
  encoder+=1; //increasing encoder at new pulse
}
ISR(TIMER1_OVF_vect)        // interrupt service routine - tick every 0.1sec
{
  TCNT1 = timer1_counter;   // set timer
  pv_speed = encoder/2;
  encoder=0;
  if (Serial.available() <= 0) {
    Serial.print(pv_speed);         //Print speed (rpm) value to Visual Studio
    Serial.print("/");
    Serial.println(pwm_pulse);
    }
        if(pv_speed<ร้องไห้set_speed-40)){
      pwm_pulse+=1;
    }else if(pv_speed>ร้องไห้set_speed+40)){
      pwm_pulse-=1;
    }
    
    if (pwm_pulse <=255 & pwm_pulse >0){
    analogWrite(pin_pwm,pwm_pulse);  //set motor speed  
  }
  else{
    if (pwm_pulse>255){
      analogWrite(pin_pwm,255);
    }
    else{
      analogWrite(pin_pwm,0);
    }
  }
}
แก้ไขข้อความเมื่อ
แสดงความคิดเห็น
อ่านกระทู้อื่นที่พูดคุยเกี่ยวกับ 
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่