Created
January 22, 2016 06:32
-
-
Save hoangddt/a8c02561c9aeee1bb085 to your computer and use it in GitHub Desktop.
Dieu Khien Banh Xe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct banhXe | |
{ | |
int pin1; | |
int pin2; | |
int pin1Value; | |
int pin2Value; | |
char state; | |
}; | |
int dieuKhien = 2; | |
char state = 'T'; | |
banhXe left = {8, 9, 1,0, 'T'}, | |
right = {10, 11, 1, 0, 'T'}; | |
void registerBanhXe(banhXe *a) | |
{ | |
pinMode(a->pin1, OUTPUT); | |
pinMode(a->pin2, OUTPUT); | |
} | |
void controlBanhXe(banhXe *a) | |
{ | |
digitalWrite(a->pin1, a->pin1Value); | |
digitalWrite(a->pin2, a->pin2Value); | |
} | |
void changeStateBanhXe(banhXe *a) | |
{ | |
a->pin1Value = 1 - a->pin1Value; | |
a->pin2Value = 1 - a->pin2Value; | |
a->state = a->state == 'T' ? 'L' : 'T'; | |
} | |
void setup() { | |
pinMode(dieuKhien, INPUT); | |
registerBanhXe(&left); | |
registerBanhXe(&right); | |
Serial.begin(9600); | |
} | |
void toggle(int *a, int *b) | |
{ | |
*a = 1 - (*a); | |
*b = 1 - (*b); | |
} | |
void loop() { | |
int input = digitalRead(dieuKhien); | |
if (input == LOW && state == 'T') | |
{ | |
changeStateBanhXe(&left); | |
changeStateBanhXe(&right); | |
state = 'L'; | |
} | |
else if (input == HIGH && state == 'L') | |
{ | |
changeStateBanhXe(&left); | |
changeStateBanhXe(&right); | |
state = 'T'; | |
} | |
/* | |
Serial.println("State - Input - Pin1 - Pin2"); | |
Serial.println(state); | |
Serial.println(input); | |
Serial.println(pin1Value); | |
Serial.println(pin2Value); | |
*/ | |
controlBanhXe(&left); | |
controlBanhXe(&right); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment