I am using an Arduino Uno board with only an H-bridge and motor to create this.
ID: 3902422 • Letter: I
Question
I am using an Arduino Uno board with only an H-bridge and motor to create this. No buttons, pins or anything else like that
Write an Arduino program that controls a DC Motor using the H-Brdige board
-Must include a function that sets the direction of the motor and speed - input arguments: int Direction, int Speed
-Program should read the following FROM THE SERIAL PORT
Recieving a value '0' sets the motor speed to 0
Recieving a value '9' sets the motor speed to 255
Values '1-8' are equally spaced between 0 - 255
Recieving 'F' sets the motor direction to FORWARD
Recieving 'R' sets the motor direction to REVERSE
Explanation / Answer
int MotorPin = 4; // MotorPin set to pin2
int Motorforward = 5; // MotorDirectionPin set to pin3
int Motorbackward = 6; // MotorDirectionPin set to pin3
void setup()
{
Serial.begin(9600); // start serial port at 9600 bps:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(MotorPin, OUTPUT); // sets the pin as output
pinMode(Motorforward, OUTPUT); // sets the pin as output
pinMode(Motorbackward, OUTPUT);
establishContact();
}
void loop()
{
digitalWrite(MotorPin, HIGH);
if (Serial.available() > 0) {
char serialread = Serial.read();
switch(serialread){
case 'w':
Serial.print(serialread);
analogWrite(Motorforward, 130);
analogWrite(Motorbackward, 0);
break;
case 's':
Serial.print(serialread);
analogWrite(Motorforward, 0);
analogWrite(Motorbackward, 130);
break;
case 'a':
Serial.print(serialread);
analogWrite(Motorforward, 0);
analogWrite(Motorbackward, 0);
break;
case 'e':
Serial.print(serialread);
analogWrite(Motorforward, 255);
analogWrite(Motorbackward, 0);
break;
case 'd':
Serial.print(serialread);
analogWrite(Motorforward, 0);
analogWrite(Motorbackward, 255);
break;
}
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.