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

Code Bánh Xe

The document defines pins for input and output and initializes serial communication. It contains an endless loop that reads serial input and controls motor speed and direction by writing PWM signals to the pin outputs based on the input character. Speed and auxiliary speed variables are increased over time for some input options to provide acceleration.

Uploaded by

imhuyyyy
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)
22 views2 pages

Code Bánh Xe

The document defines pins for input and output and initializes serial communication. It contains an endless loop that reads serial input and controls motor speed and direction by writing PWM signals to the pin outputs based on the input character. Speed and auxiliary speed variables are increased over time for some input options to provide acceleration.

Uploaded by

imhuyyyy
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
You are on page 1/ 2

#define in1 5

#define in2 6
#define in3 9
#define in4 10
#define txd 0
#define rxd 1
char th;
int speed = 70;
int a = 140;
void setup()
{
Serial.begin(9600);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop()
{
if (Serial.available()>0){
th = Serial.read();
}

if (th == 'F'){
analogWrite(in1,0);
analogWrite(in2,speed);
analogWrite(in3,speed);
analogWrite(in4,0);
while (speed<255){
speed++;
delay(10);
}
}
if (th == 'B'){
analogWrite(in1,speed);
analogWrite(in2,0);
analogWrite(in3,0);
analogWrite(in4,speed);
while (speed<255){
speed++;
delay(10);
}
}
if (th == 'L'){
analogWrite(in1,0);
analogWrite(in2,a);
analogWrite(in3,0);
analogWrite(in4,a);
while (a<255){
a++;
delay(10);
}
}
if (th == 'R'){
analogWrite(in1,a);
analogWrite(in2,0);
analogWrite(in3,a);
analogWrite(in4,0);
while (a<255){
a++;
delay(10);
}
}
if (th == 'G'){
analogWrite(in1,0);
analogWrite(in2,speed);
analogWrite(in3,a);
analogWrite(in4,0);
while (a<255){
a+=2;
speed+=1;
}
}
if (th == 'I'){
analogWrite(in1,a);
analogWrite(in2,0);
analogWrite(in3,0);
analogWrite(in4,speed);
while (a<255){
a+=2;
speed+=1;
}
}
if (th == 'H'){
analogWrite(in1,64);
analogWrite(in2,0);
analogWrite(in3,0);
analogWrite(in4,127);
}
if (th == 'G'){
analogWrite(in1,127);
analogWrite(in2,0);
analogWrite(in3,0);
analogWrite(in4,64);
}
if (th=='S'){
analogWrite(in1,0);
analogWrite(in2,0);
analogWrite(in3,0);
analogWrite(in4,0);
}

You might also like