4WD Car Kit Arduino Santiapps Series

Arduino Honduras Santiapps Marcio Valenzuela

Arduino (IoT) Video Serie: Kit de Carro Parte 4 (IR)

Este es el codigo final:

#include "AFMotor.h"
#include "IRremote.h"

AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor3(3, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor4(4, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

int RECV_PIN = 10;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
  irrecv.enableIRIn(); // Start the receiver

  motor1.setSpeed(200);     // set the speed to 200/255
  motor2.setSpeed(200);     // set the speed to 200/255
  motor3.setSpeed(200);     // set the speed to 200/255
  motor4.setSpeed(200);     // set the speed to 200/255
}

void loop() {

  //Recibir & Analizar codigo recibido
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);

      if (results.value == 0x2C2E80FF){
        Serial.println("forward");
        forward();
        delay(2000);
        brake();
      } else if (results.value == 0x5A1A483D){
        Serial.println("backward");
        backward();
        delay(2000);
        brake();
      } else if (results.value == 0x9578646A){
        Serial.println("left");
        left();
        delay(1000);
        brake();  
      } else if (1==1) {
        Serial.println("right");
        right();
        delay(1000);
        brake();  
      }

      irrecv.resume(); // Receive the next value
  } 
}

void forward(){
  Serial.println("running forward");
  motor3.run(FORWARD);   
  motor4.run(FORWARD);     
}
void backward(){
  Serial.println("running backward");
  motor3.run(BACKWARD);    
  motor4.run(BACKWARD);    
}
void left(){
  motor3.run(FORWARD);   
}
void right(){
  motor4.run(FORWARD);    
}
void brake(){
  motor1.run(RELEASE);    
  motor2.run(RELEASE);    
  motor3.run(RELEASE);    
  motor4.run(RELEASE);      
}

Leave a Reply