Thursday, December 11, 2014

It Works!!!

The robot officially moves with the remote control. I ended up seeking help from a computer science friend and learned a lot from the. After I finally got the hang of things, I ended up making a few extra moves for the robot to do. Pushing the OK button makes it dance, pushing the INFO button makes the robot go in a circle, HELP makes the robot do a figure 8, and pressing MENU makes the robot shake back and forth.

The motors ended up being wired backwards and I didn't have a screwdriver handy to change the order, so I ended up putting some things backwards in the program in order for the robot to do what was intended. Also, the wheels are a little wonky so one had to run longer than the other in order to make a perfect figure 8 or perfect circle, which is why the values in some of the sections are a little weird.

The first time around in the earlier post regarding this program, the values gathered for the buttons were in hexadecimal instead of decimal, and that didn't work because it read letters instead of integers. These values were converted in order for the program to compile and execute as needed.


//This is the best code of the 21st century.
#include <IRremote.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

#define IR_CODE_FORWARD 16798291
#define IR_CODE_BACKWARD 16831187
#define IR_CODE_TURN_RIGHT 16839411
#define IR_CODE_TURN_LEFT 16806515
#define IR_CODE_DANCE 3937909453
#define IR_CODE_INFO 16817309
#define IR_CODE_HELP 16825019
#define IR_CODE_MENU 16796235
#define IR_CODE_CONTINUE -1

boolean isActing=false;
long timer;
const long TIME_OUT=100;
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *rightMotor = AFMS.getMotor(3);
Adafruit_DCMotor *leftMotor = AFMS.getMotor(4);

void setup(){
  Serial.begin(9600);
  AFMS.begin();
  rightMotor->setSpeed(180);
  leftMotor->setSpeed(180);
  irrecv.enableIRIn();
}
void fig8(){
  for(int i = 0 ; i < 2 ; ++i){
    rightMotor->setSpeed(50);
    leftMotor->run(BACKWARD);
    rightMotor->run(BACKWARD);
    delay(2000);
    rightMotor->setSpeed(180);
    delay(1000);
    leftMotor->setSpeed(60);
    delay(3500);
    rightMotor->run(RELEASE);
    leftMotor->run(RELEASE);
    leftMotor->setSpeed(180);
  }
}
void shake(){
  for(int i = 0 ; i < 10 ; ++i){
    if(i%2==0){
      rightMotor->run(BACKWARD);
      leftMotor->run(BACKWARD);
      delay(250);
    }else{
      rightMotor->run(FORWARD);
      leftMotor->run(FORWARD);
      delay(250);
    }
  }
  rightMotor->run(RELEASE);
  leftMotor->run(RELEASE);
}
void circle(){
  rightMotor->setSpeed(50);
  leftMotor->run(BACKWARD);
  rightMotor->run(BACKWARD);
  delay(3000);
  leftMotor->run(RELEASE);
  rightMotor->run(RELEASE);
  rightMotor->setSpeed(180);
}
void dance(){
  for(int i = 0 ; i < 10 ; ++i){
    if(i%2 == 0){
      leftMotor->run(FORWARD);
      rightMotor->run(BACKWARD);
      delay(250);
    }else{
      leftMotor->run(BACKWARD);
      rightMotor->run(FORWARD);
      delay(250);
    }
  }
  leftMotor->run(RELEASE);
  rightMotor->run(RELEASE);
}
void isWorking(){
  timer=millis();
  isActing=true;
}
void loop(){
  if(irrecv.decode(&results)){
    switch(results.value){
      case IR_CODE_BACKWARD:
        leftMotor->run(FORWARD);
        rightMotor->run(FORWARD);
        isWorking();
        break;
      case IR_CODE_FORWARD:
        leftMotor->run(BACKWARD);
        rightMotor->run(BACKWARD);
        isWorking();
        break;
      case IR_CODE_TURN_RIGHT:
        leftMotor->run(BACKWARD);
        rightMotor->run(FORWARD);
        isWorking();
        break;
      case IR_CODE_TURN_LEFT:
        leftMotor->run(FORWARD);
        rightMotor->run(BACKWARD);
        isWorking();
        break;
      case IR_CODE_INFO:
        circle();
        break;
      case IR_CODE_HELP:
        fig8();
        break;
      case IR_CODE_MENU:
        shake();
        break;
      case IR_CODE_DANCE:
        dance();
        break;
      case IR_CODE_CONTINUE:
        timer=millis();
        break;
    }
    irrecv.resume();
  }
  if(isActing && (millis()-timer>=TIME_OUT)){
    leftMotor->run(RELEASE);
    rightMotor->run(RELEASE);
    isActing=false;
  }
 
}

No comments:

Post a Comment