Hi, I currently have this code. When the pushbutton is pressed the DC motor has to spin for a set time limit before coming to a stop. I would need to add the interrupt function into the code and I am not sure on how to do it. My motor is 380:1 Micro Metal Gearmotor HPCB 12V with Extended Motor Shaft and the driver carrier is MAX14870 Single Brushed DC Motor Driver Carrier. I am usuing Arduino UNO.
#include <DualMAX14870MotorShield.h>
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
#define LED_PIN 13
#define buttonPin 2
// variables will change:
int buttonState = HIGH; // variable for reading the pushbutton status
DualMAX14870MotorShield motors;
void setup()
{
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// Show the state of pushbutton on serial monitor
Serial.println(buttonState);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
motors.enableDrivers();
// run M1 motor with positive speed
digitalWrite(LED_PIN, HIGH);
//for (int speed = 0; speed <= 400; speed++)
{
motors.setM1Speed(200);
delay(10000);
motors.setM1Speed(0);
}
//stopIfFault();
//if (millis() >= 10000) exit(0);
//delay(2);
}else
// turn LED off:
digitalWrite(LED_PIN, LOW);
motors.disableDrivers();
delay(500);
}
