Day4 04-DCmotor, button

2024. 2. 4. 15:51arduino

  • 버튼을 이용한 모터제어
const int MA_IA = 9;
const int MA_IB = 10;

const int btnStartPin = 2;
const int btnStopPin = 3;

volatile bool isStartPressed = false;
volatile bool isStopPressed = false;

void ISRButtonStart() {
  isStartPressed = true;
  Serial.println("Start pressed!");
}

void ISRButtonStop() {
  isStopPressed = true;
  Serial.println("Stop pressed!");
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(MA_IA, OUTPUT);
  pinMode(MA_IB, OUTPUT);

  pinMode(btnStartPin, INPUT_PULLUP);
  pinMode(btnStopPin, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(btnStartPin), ISRButtonStart, FALLING);  //2
  attachInterrupt(digitalPinToInterrupt(btnStopPin), ISRButtonStop, FALLING);   //3
}

void loop() {
  // put your main code here, to run repeatedly:
  if (isStartPressed && !isStopPressed) {
    isStartPressed = false;
    analogWrite(MA_IA, 250);
    analogWrite(MA_IB, 0);
  }
  if (isStopPressed && !isStartPressed) {
    isStopPressed = false;
    analogWrite(MA_IA, 0);
    analogWrite(MA_IB, 0);
  }
}

'arduino' 카테고리의 다른 글

Day4 06-joystick  (0) 2024.02.04
Day4 05-DCmotor, button 2  (0) 2024.02.04
Day4 03-DCmotor  (0) 2024.02.04
Day4 02-encoder  (0) 2024.02.04
Day4 01-debounce  (0) 2024.02.04