---
# System prepended metadata

title: Robot

---

# Robot
```javascript

const motorA = ev3_motorA();
const motorB = ev3_motorB();
const colour = ev3_colorSensor();

display(ev3_connected(motorA) ? "A connected" : "A not connected");
display(ev3_connected(motorB) ? "B connected" : "B not connected");
display(ev3_connected(colour) ? "colour connected" : "colour not connected");

// let green_count = 0;
// let red_count = 0;

// let flag = true; 

// //helper functions
// function brake() {
//     ev3_motorSetStopAction(motorA, "brake");
//     ev3_motorSetStopAction(motorB, "brake");
// }

// function coast() {
//     ev3_motorSetStopAction(motorA, "coast");
//     ev3_motorSetStopAction(motorB, "coast");
// }

// function run_slow() {
//     const motor_dur = 50;
//     const motor_spd = -300;

//     ev3_runForTime(motorA, motor_dur, motor_spd);
//     ev3_runForTime(motorB, motor_dur, motor_spd);
//     ev3_pause(10);
// }
// function run_fast() {
//     const motor_dur = 50;
//     const motor_spd = -2000;

//     ev3_runForTime(motorA, motor_dur, motor_spd);
//     ev3_runForTime(motorB, motor_dur, motor_spd);
//     ev3_pause(10);
// }

// while (flag) {  
//     function check_colour() {
//          if (ev3_colorSensorGetColor(colour) === 3) {
//              green_count = green_count + 1;
//              display("green: " + green_count);
//          } else if (ev3_colorSensorGetColor(colour) === 5) {
//              red_count = red_count + 1;
//              display("red: " + red_count);
//          } else {}
//     }
//     // programme
//      if (red_count === 1) {
//          run_slow();
//          check_colour();
//      } else if (red_count === 2) { 
//          flag = false; 
//      } else if (green_count === 1) {
//          run_slow();
//          check_colour();
//      } else {
//          run_fast();
//          check_colour();        
//      }
// }

// brake();











//Q2
const motorA = ev3_motorA();
const motorB = ev3_motorB();
const colour = ev3_colorSensor();

display(ev3_connected(motorA) ? "A connected" : "A not connected");
display(ev3_connected(motorB) ? "B connected" : "B not connected");
display(ev3_connected(colour) ? "colour connected" : "colour not connected");

function turn(dir) {
        //motor_dur corresponds to the value for which robot will turn 
        //the required value => 90
    const motor_dur = 575;
    const motor_forward = -1000;
    const motor_reverse = 1000;
        //speed of motor have the same absolute values so robot will rotate the same
        //amount but in different directions
    if (dir === "left") {
        ev3_runForTime(motorA, motor_dur, motor_reverse);
        ev3_runForTime(motorB, motor_dur, motor_forward);
        ev3_pause(1000);
    } else if (dir === "right") {
        ev3_runForTime(motorA, motor_dur, motor_forward);
        ev3_runForTime(motorB, motor_dur, motor_reverse);
        ev3_pause(1000);
    } else {}
}
function run_forward_for(dist) {
    dist = dist / 10;
        //divided by ten so input can match the cm required to run
        //133 and 1000 corresponds to the value for which our robot travels 10cm
    const motor_dur = 133;
    const motor_spd = -1000;
    
    for (let i = 0; i < dist; i = i + 1) {
        ev3_runForTime(motorA, motor_dur, motor_spd);
        ev3_runForTime(motorB, motor_dur, motor_spd);
        ev3_pause(200);   
    }
}
function check_colour() {
    if (ev3_colorSensorGetColor(colour) === 2) {
        //Blue    
    } else if (ev3_colorSensorGetColor(colour) === 3) {
        //Green    
    } else if (ev3_colorSensorGetColor(colour) === 4) {
        //Yellow    
    } else if (ev3_colorSensorGetColor(colour) === 5) {
        //Red    
    } else {}
}
function turn(dir) {
    //motor_dur corresponds to the value for which robot will turn 
    //the required value => 90
    const motor_dur = 575;
    const motor_forward = -1000;
    const motor_reverse = 1000;
    
    //speed of motor have the same absolute values so robot will rotate the same
    //amount but in different directions
    if (dir === "left") {
        ev3_runForTime(motorA, motor_dur, motor_reverse);
        ev3_runForTime(motorB, motor_dur, motor_forward);
        ev3_pause(1000);
    } else if (dir === "right") {
        ev3_runForTime(motorA, motor_dur, motor_forward);
        ev3_runForTime(motorB, motor_dur, motor_reverse);
        ev3_pause(1000);
    } else {}
}

function repair() {
        run_forward_for(30);
        check_colour();
        turn("right");
        run_forward_for(60);
        turn("right");
        run_forward_for(50);
    }

}




