(URGENT) marble sorter code problem

I am building a marble sorter that sorts the colored marbles in the photo attached. Here is how it works: you drop the marbles into a hopper, they are stoped by 2 gates attached to a motor that have a small space in between the gates for one marble to be detected by the color sensor at a time. Once the marble color has been detected, the second motor spins three cups on a plate and spins the correct cup to the corresponding color under the slide. Then, the motor that controls the gates opens and closes very quickly, allowing for another marble to be detected, and the previous marble slides into the correct cup. I’m not sure what is wrong with my code, but if someone could help I would be grateful.
the code:

brain = Brain()
motor_1 = Motor(Ports.PORT1)
motor_2 = Motor(Ports.PORT2)
optical_3 = Optical(Ports.PORT3)
optical_3.set_light(LedStateType.ON)
 
CUP_POSITIONS = {
    'orange': 0,
    'white': 120,
    'gray': 240
}
 
marble_detected = False
 
def open_and_close_gate():
    """Opens and closes the gate very quickly."""
    motor_1.spin_to_position(45, DEGREES, wait=True)
    wait(0.1, SECONDS)
    motor_1.spin_to_position(0, DEGREES, wait=True)
 
def rotate_to_cup(color):
    """Rotates motor_2 to align the correct cup."""
    if color in CUP_POSITIONS:
        motor_2.spin_to_position(CUP_POSITIONS[color], DEGREES, wait=True)
        wait(0.5, SECONDS)
    else:
        brain.screen.print("Color not recognized!")
 
def detect_marble_color():
    """Detects the color of the marble using the optical sensor."""
    wait(0.2, SECONDS)
    if optical_3.hue() <= 32:
        return 'orange'
    elif optical_3.color() == Color.WHITE:
        return 'white'
    elif optical_3.hue() <= 240:
        return 'gray'
    else:
        return None
 
while True:
    marble_color = detect_marble_color()
   
    if marble_color and not marble_detected:
        brain.screen.print("Detected: {marble_color}")
        open_and_close_gate()
        rotate_to_cup(marble_color)
        marble_detected = True
    elif not optical_3.is_near_object() and marble_detected:
        marble_detected = False
    else:
        wait(0.3, SECONDS) 

1 Like

update: i failed the assignment 45 views and not a single comment

I think you posted in a topic which is focused on technical support for issues with VEXcode V5 - it is not a place for others to solve your class assignments. The appropriate place for clarification is to see your teacher.

4 Likes

Hi, I’m one of the 45 views that you had. I looked and went “oh a homework project” and moved on. I did note that you had print statements, so I figured that you knew how to print data out on steps to see what it was seeing,

You just dumped the code out there. There wasn’t any indication of what you were seeing. Were you able to identify the colors of the ball? What other things did you notice when you ran your program. What did you try that worked / didn’t work?

As you worked on it across the last 12 days you could have posted any progress you had made.

But like the post above this notes, this isn’t really set up to do homework.

Help us help you may be of help in the future.

7 Likes