i am using an Arduino uno board with a set up that looks like this... nothing mo
ID: 2293740 • Letter: I
Question
i am using an Arduino uno board with a set up that looks like this... nothing more.
1. Write an Arduino program that controls a DC Motor using the H-Bridge board. Must include a function that sets the direction of the motor and speed a. i. Input Arguments: int Direction, int Speed b. Program should read from the serial port i. Receiving value '0' sets the motor speed to 0 ii. Receiving value '9' sets the motor speed to 255 ii. Values '1'-8' are equally spaced speeds between 0-255 iv. Receiving "F' sets the motor direction to FOR WARD v. Receiving 'R' sets the motor direction to REVERSE NOTE: For the yellow DC motor in your kit you may use the 5 volt source from the Arduino to power the H-Bridge Motor Driver. If you get a higher voltage DC motor later for your robot you should use a battery pack to power the H-Bridge. In any case, make sure your grounds for Arduino and Motor Driver are connected together.Explanation / Answer
arduino code:-
#define enA 9
#define in1 6
#define in2 7
char rx_byte = 0;
void setup()
{
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
// Set initial rotation direction
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
Serial.begin(9600); // for serial communication
}
void loop()
{
if (Serial.available() > 0)
{
rx_byte = Serial.read(); // get the character
// check if a number was received
if (rx_byte == '0')
{
speedfn(0);
}
if (rx_byte == '1')
{
speedfn(1);
}
if (rx_byte == '2')
{
speedfn(2);
}
if (rx_byte == '3')
{
speedfn(3);
}
if (rx_byte == '4')
{
speedfn(4);
}
if (rx_byte =='5')
{
speedfn(5);
}
if (rx_byte == '6')
{
speedfn(6);
}
if (rx_byte == '7')
{
speedfn(7);
}
if (rx_byte == '8')
{
speedfn(8);
}
if (rx_byte == '9')
{
speedfn(9);
}
if (rx_byte == 'f')
{
direcionfn(f);
}
if (rx_byte == 'r')
{
directionfn(r);
}
}
speedfn(int s)
{
int pwmOutput = map(s, 0, 9, 0 , 255); // Map the potentiometer value from 0 to 255
analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin
}
direcionfn(char d)
{
if(d == 'f')
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
delay(20);
}
if(d == 'r' )
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
delay(20);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.