i need help with this code please

Move your robot forward until you are approximately 10" away from the obstacle in front of you and read the color ("red" is right, "blue" is left) to indicate which way to turn. Proceed until your touch sensor hits the obstacle (2x4) backup for a second, read the color ("red" is right, "blue" is left) to determine the direction to turn. Proceed straight until you are 10" from the next obstacle, read the color ("red" is right, "blue" is left) to determine the direction to turn. Proceed until you read the color green then complete at least a full circle all the while playing a song on the Lego. To complete is 100%. To win the speed test earns an extra 10%.

5 Comments

This sounds like a homework assignment or instructions for a programming contest. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
This is not code, at least not in any coding/programming language I know of. It certainly isn't Matlab code. What code have you written that you need help with?
clear;clc;
% Connect to the EV3 brick
myev3 = legoev3;
% Connect the motors (adjust ports as needed)
motorA = motor(myev3, 'A'); % Left motor
motorB = motor(myev3, 'D'); % Right motor
% Connect the sensors (adjust ports as needed)
usSensor = sonicSensor(myev3, 3); % Ultrasonic sensor
colorSensor = colorSensor(myev3, 4); % Color sensor
touchSensor = touchSensor(myev3, 2); % Touch sensor
% Main routine
while true
% Move forward until 10 inches from an obstacle
while distance_sensor.get_distance(motorA,motorB) > 10
moveForward(motorA, motorB)
motorA.Speed = 30;
motorB.Speed = 30;
start(motorA);
start(motorB);
% Determine direction to turn based on color
color = readcolorsensor(myev3)
if color == "red"
turn_right(motorA)
elif color == "blue":
turn_left(motorB)
% Move forward until touch sensor is pressed
while not touch_sensor.is_pressed();
move_forward()
stop()
% Back up for a second
move_backward()
% Determine direction to turn based on color
color = color_sensor.get_color()
if color == "red":
turn_right()
elif color == "blue":
turn_left()
% Move forward until green color is detected
while color_sensor.get_color(touchsensor) == "green"
move_forward(motorA,motorB)
stop(motorA,motorB)
% Perform a full circle and play a song
turn_right() % Adjust as needed to make a full circle
play_song()
break % Exit loop after completing the task
that's what i have so far for the code, can someone help how to adjust please
Sorry, it's difficult for me to debug unless I have your Lego hardware.
MATLAB does not use elif -- it uses elseif
The nesting of your code is questionable. Your while for the color sensor being green is nested withing the elif case. It is more likely that you want
if color == "red":
turn_right()
elseif color == "blue":
turn_left()
end
while color_sensor.get_color(touchsensor) == "green"
so that the while loop is done regardlesss if you turned right or left (or neither.)

Sign in to comment.

 Accepted Answer

You can use the AI Chat Playground to get you started.
I went there and entered
use matlab code to "Move your robot forward until you are approximately 10" away from the obstacle in front of you and read the color ("red" is right, "blue" is left) to indicate which way to turn. Proceed until your touch sensor hits the obstacle (2x4) backup for a second, read the color ("red" is right, "blue" is left) to determine the direction to turn. Proceed straight until you are 10" from the next obstacle, read the color ("red" is right, "blue" is left) to determine the direction to turn. Proceed until you read the color green then complete at least a full circle all the while playing a song on the Lego."
and it gave me some commented code. It probably will not work right out of the box but it will give you a start.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!