Battle Robot

Battle Robot

How it works

Two obniz integration.
The robot responds to the controller.
forward/back/rotation and one more action with the servo motor.

Materials

  1. obniz – 2
  2. servomotor – 1
  3. dc motor – 2
  4. battery or usb adaptor & cable – 2

Code

var led = Parts('LED');
var button1 = Parts('Button');
var button2 = Parts('Button');
var button3 = Parts('Button');
var button4 = Parts('Button');
var button5 = Parts('Button');

var obniz_robot = new Obniz("84902871");
var motor1 = Parts('DCMotor');
var motor2 = Parts('DCMotor');
var servo = Parts('ServoMotor');
  
button1.wired(obniz, 1, 2);
button2.wired(obniz, 3, 4);
button3.wired(obniz, 5, 6);
button4.wired(obniz, 9, 10);
button5.wired(obniz, 11, 12);

obniz_robot.onconnect = function(){
  obniz_robot.reset();
  obniz_robot.debugprint = true;
  motor1.wired(obniz_robot, 4, 6);
  motor2.wired(obniz_robot, 7, 8);
  motor1.voltage(2.5);

  servo.wired(obniz_robot, 1, 2, 3);
  console.log("onconnect obniz_robot");
};

obniz.loop = function() {
  if(!button1.isPressed()){
    console.log("go" );
    motor1.forward();
    motor2.forward();
  }else if(!button2.isPressed()){
    console.log("reverse" );
    motor1.reverse();
    motor2.reverse();
  }else{
    console.log("stop" );
    motor1.stop();
    motor2.stop();
  }
  if(!button3.isPressed()){
    servo.angle(180.0);
  }else{
    servo.angle(0);
  }
};

Leave a Comment

%d bloggers like this: