0% found this document useful (0 votes)
44 views2 pages

Motor Control

The document defines pins for controlling the direction of a motor via relays, and includes code to initialize the direction pins, power the selected pin, and change the direction by reading a button pin in a debounced manner.

Uploaded by

nkhatamuchondi03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views2 pages

Motor Control

The document defines pins for controlling the direction of a motor via relays, and includes code to initialize the direction pins, power the selected pin, and change the direction by reading a button pin in a debounced manner.

Uploaded by

nkhatamuchondi03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#define PIN_1 11 //Pin driving motor cw via relay 1

#define PIN_2 10 //Pin driving motor ccw via relay 2

#define BUTTON_PIN 13
#define Motor_Pin_Number 2

unsigned long debounceDuration = 50; // millis


unsigned long lastTimeButtonStateChanged = 0;
byte lastButtonState = HIGH;
byte LEDState = LOW;
int DirectionIndex = 0;

//Variable Definition
byte MotorDirectionPin[Motor_Pin_Number] = {PIN_1,
PIN_2};

//Function Declaration
void initDirectionPins()
{
for (int i = 0; i < Motor_Pin_Number; i++) {
pinMode(MotorDirectionPin[i], OUTPUT);
}
}

void powerOnSelectedDirection(int index)


{
for (int i = 0; i < Motor_Pin_Number; i++) {
if (i == index) {
digitalWrite(MotorDirectionPin[i], HIGH);
}
else {
digitalWrite(MotorDirectionPin[i], LOW);
}
}

//

void setup(){// setup

void initDirectionPins();

pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalWrite(MotorDirectionPin[DirectionIndex], HIGH);
}
//Loop
void loop(){
unsigned long timeNow = millis();
if (timeNow - lastTimeButtonStateChanged > debounceDuration) {
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = timeNow;
lastButtonState = buttonState;
if (buttonState == HIGH) { // button has been released
DirectionIndex++;
if (DirectionIndex >= Motor_Pin_Number) {
DirectionIndex = 0;
}
powerOnSelectedDirection(DirectionIndex);
}
}
}

You might also like