/*
Arduino | coding-help
Using Sparkfun Kit to create a project for my class,
but the delay function seems to not be called.
SoloKhi — November 9, 2025 at 9:41 PM
*/
//the right motor will be controlled by the motor A pins on the motor driver
const int AIN1 = 13; //control pin 1 on the motor driver for the right motor
const int AIN2 = 12; //control pin 2 on the motor driver for the right motor
const int PWMA = 11; //speed control pin on the motor driver for the right motor
int switchPin = 7; //switch to open the bridge
int switchPin2 = 6;
/********************************************************************************/
void setup()
{
pinMode(switchPin, INPUT_PULLUP); //set this as a pullup to sense whether the switch is flipped
pinMode(switchPin2, INPUT_PULLUP); //set this as a pullup to sense whether the switch2 is flipped
//set the motor control pins as outputs
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMA, OUTPUT);
}
/********************************************************************************/
void loop()
{
if (digitalRead(7) == LOW)
{
rightMotor(100);
delay(2000);
rightMotor(0);
}
if (digitalRead(6) == LOW)
{
rightMotor(-100);
delay(2000);
rightMotor(0);
}
}
/********************************************************************************/
void rightMotor(int motorSpeed) //function for driving the right motor
{
if (motorSpeed > 0) //if the motor should drive forward (positive speed)
{
digitalWrite(AIN1, HIGH); //set pin 1 to high
digitalWrite(AIN2, LOW); //set pin 2 to low
}
else if (motorSpeed < 0) //if the motor should drive backward (negative speed)
{
digitalWrite(AIN1, LOW); //set pin 1 to low
digitalWrite(AIN2, HIGH); //set pin 2 to high
}
else //if the motor should stop
{
digitalWrite(AIN1, LOW); //set pin 1 to low
digitalWrite(AIN2, LOW); //set pin 2 to low
}
analogWrite(PWMA, abs(motorSpeed)); //now that the motor direction is set, drive it at the entered speed
}
IN1
IN2
ENA
CW
CCW