Paso 6: El programa!
El código es bastante simple. Intentar leer una vez antes de copiar pegar ;)
<p>#include<Servo.h><br></p><p> Servo servo; #include<IRremote.h></p><p>int IRpin = 8; // pin for the IR sensor</p><p><br>int LED = 13; // LED pin IRrecv irrecv(IRpin); decode_results results;</p><p>boolean LEDon = true; // initializing LEDon as true //L293 Connection const int motorA1 = 3; // Pin 2 of L293 const int motorA2 = 4; // Pin 7 of L293 const int motorB1 = 6; // Pin 10 of L293 const int motorB2 = 7; // Pin 14 of L293</p><p> const int BTState = 2;</p><p> //Useful Variables int i=0; int j=0; int state; int vSpeed=200; // Default speed, from 0 to 255</p><p>void setup() { // Set pins as outputs: pinMode(motorA1, OUTPUT); pinMode(motorA2, OUTPUT); pinMode(motorB1, OUTPUT); pinMode(motorB2, OUTPUT); // pinMode(lights, OUTPUT); pinMode(BTState, INPUT); // Initialize serial communication at 9600 bits per second: Serial.begin(9600); servo.attach(9); irrecv.enableIRIn(); // Start the receiver pinMode(LED, OUTPUT); pinMode(10, OUTPUT); } void loop() { //Stop car when connection lost or bluetooth disconnected if(digitalRead(BTState)=='S') { state='S'; }</p><p> //Save income data to variable 'state' if(Serial.available() > 0){ state = Serial.read(); } if (state == '0'){ servo.write(0); } else if(state == '1'){ servo.write(18); } else if(state == '2'){ servo.write(36); } else if(state == '3'){ servo.write(54); } else if(state == '4'){ servo.write(72); } else if(state == '5'){ servo.write(90); } else if(state == '6'){ servo.write(108); } else if(state == '7'){ servo.write(126); } else if(state == '8'){ servo.write(144); } else if(state == '9'){ servo.write(162); } else if(state == 'q'){ servo.write(180); } /***********************Forward****************************/ //If state is equal with letter 'F', car will go forward! if (state == 'F') { analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0); analogWrite(motorB1, vSpeed); analogWrite(motorB2, 0); } /**********************Forward Left************************/ //If state is equal with letter 'G', car will go forward left else if (state == 'G') { analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0); analogWrite(motorB1, 200); analogWrite(motorB2, 0); } /**********************Forward Right************************/ //If state is equal with letter 'I', car will go forward right else if (state == 'I') { analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0); analogWrite(motorB1, 0); analogWrite(motorB2, 200); } /***********************Backward****************************/ //If state is equal with letter 'B', car will go backward else if (state == 'B') { analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed); analogWrite(motorB1, 0); analogWrite(motorB2, vSpeed); } /**********************Backward Left************************/ //If state is equal with letter 'H', car will go backward left else if (state == 'H') { analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed); analogWrite(motorB1, 200); analogWrite(motorB2, 0); } /**********************Backward Right************************/ //If state is equal with letter 'J', car will go backward right else if (state == 'J') { analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed); analogWrite(motorB1, 0); analogWrite(motorB2, 200); } /***************************Left*****************************/ //If state is equal with letter 'L', wheels will turn left else if (state == 'L') { analogWrite(motorA1, 0); analogWrite(motorA2, 0); analogWrite(motorB1, 200); analogWrite(motorB2, 0); } /***************************Right*****************************/ //If state is equal with letter 'R', wheels will turn right else if (state == 'R') { analogWrite(motorA1, 0); analogWrite(motorA2, 200); analogWrite(motorB1, 0); analogWrite(motorB2, 0); } else if(state == 'W'){ digitalWrite(10,HIGH); delay(500); digitalWrite(10,LOW); } else if(state == 'w'){ digitalWrite(10,HIGH); delay(500); digitalWrite(10,LOW);</p><p>} /************************Stop*****************************/ //If state is equal with letter 'S', stop the car else if (state == 'S'){ analogWrite(motorA1, 0); analogWrite(motorA2, 0); analogWrite(motorB1, 0); analogWrite(motorB2, 0); } if (irrecv.decode(&results)) { irrecv.resume(); // Receive the next value } if (results.value == 0) // change zero to your IR remote button number { if (LEDon == true) // is LEDon equal to true? { LEDon = false; digitalWrite(LED, HIGH); delay(100); // keeps the transistion smooth } else { LEDon = true; digitalWrite(LED, LOW); delay(100); } } </p><p> }</p>