Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h> //including the libraries of I2C
- #include <IRremote.h> // https://github.com/z3t0/Arduino-IRremote/archive/master.zip
- int RECV_PIN = 12;
- IRrecv irrecv(RECV_PIN);
- decode_results results;
- #define IR_Go 0x00ff629d
- #define IR_Back 0x00ffa857
- #define IR_Left 0x00ff22dd
- #define IR_Right 0x00ffc23d
- #define IR_Stop 0x00ff02fd
- #define IR_ESC 0x00ff52ad
- unsigned char Bluetooth_val; //defining variable val
- #define Lpwm_pin 5 //adjusting speed
- #define Rpwm_pin 10 //adjusting speed
- int pinLB = 2; // defining pin2 left rear
- int pinLF = 4; // defining pin4 left front
- int pinRB = 7; // defining pin7 right rear
- int pinRF = 8; // defining pin8 right front
- unsigned char Lpwm_val = 255;
- unsigned char Rpwm_val = 255;
- int Car_state = 0;
- void M_Control_IO_config(void)
- {
- pinMode(pinLB, OUTPUT); // pin 2
- pinMode(pinLF, OUTPUT); // pin 4
- pinMode(pinRB, OUTPUT); // pin 7
- pinMode(pinRF, OUTPUT); // pin 8
- pinMode(Lpwm_pin, OUTPUT); // pin 11 (PWM)
- pinMode(Rpwm_pin, OUTPUT); // pin 10 (PWM)
- }
- void Set_Speed(unsigned char Left, unsigned char Right)
- {
- analogWrite(Lpwm_pin, Left);
- analogWrite(Rpwm_pin, Right);
- }
- void advance() // going forward
- {
- digitalWrite(pinRB, LOW); // making motor move towards right rear
- digitalWrite(pinRF, HIGH);
- digitalWrite(pinLB, LOW); // making motor move towards left rear
- digitalWrite(pinLF, HIGH);
- Car_state = 1;
- }
- void turnR() //turning right(dual wheel)
- {
- digitalWrite(pinRB, LOW); //making motor move towards right rear
- digitalWrite(pinRF, HIGH);
- digitalWrite(pinLB, HIGH);
- digitalWrite(pinLF, LOW); //making motor move towards left front
- Car_state = 4;
- }
- void turnL() //turning left(dual wheel)
- {
- digitalWrite(pinRB, HIGH);
- digitalWrite(pinRF, LOW ); //making motor move towards right front
- digitalWrite(pinLB, LOW); //making motor move towards left rear
- digitalWrite(pinLF, HIGH);
- Car_state = 3;
- }
- void stopp() //stop
- {
- digitalWrite(pinRB, HIGH);
- digitalWrite(pinRF, HIGH);
- digitalWrite(pinLB, HIGH);
- digitalWrite(pinLF, HIGH);
- Car_state = 5;
- }
- void back() //back up
- {
- digitalWrite(pinRB, HIGH); //making motor move towards right rear
- digitalWrite(pinRF, LOW);
- digitalWrite(pinLB, HIGH); //making motor move towards left rear
- digitalWrite(pinLF, LOW);
- Car_state = 2;
- }
- void IR_Control(void)
- {
- unsigned long Key;
- while (Key != IR_ESC)
- {
- if (irrecv.decode(&results)) //judging if serial port receives data
- {
- Key = results.value;
- switch (Key)
- {
- case IR_Go: advance(); //UP
- break;
- case IR_Back: back(); //back
- break;
- case IR_Left: turnL(); //Left
- break;
- case IR_Right: turnR(); //Righ
- break;
- case IR_Stop: stopp(); //stop
- break;
- default:
- break;
- }
- irrecv.resume(); // Receive the next value
- }
- }
- stopp();
- }
- void setup()
- {
- irrecv.enableIRIn(); // Start the receiver
- M_Control_IO_config();
- Set_Speed(Lpwm_val, Rpwm_val);
- Serial.begin(9600); //initialized serial port , using Bluetooth as serial port, setting baud at 9600
- stopp();
- }
- void loop()
- {
- if (irrecv.decode(&results)) {
- if (results.value == IR_Stop )IR_Control();
- irrecv.resume(); // Receive the next value
- }
- if (Serial.available()) //to judge whether the serial port receives the data.
- {
- Bluetooth_val = Serial.read(); //reading (Bluetooth) data of serial port,giving the value of val;
- switch (Bluetooth_val)
- {
- case 'U': advance(); //UP
- break;
- case 'D': back(); //back
- break;
- case 'L': turnL(); //Left
- break;
- case 'R': turnR(); //Right
- break;
- case 'S': stopp(); //stop
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement