If- Then and a few other questions

Hello! I have a few questions I need help with, please see below.

  1. I have students currently working on the Gen 2 bots and having them make decisions using color cubes and If then statements. I made a challenge on the floor with tape. The first part of the challenge the students sort the cubes (if you recall the old Strawberry sorter challenge) The If-then’s working to sense color cubes with optical sensor in a forever loop. For example, it’ll see blue and sort it right, see green and sort left, see red and sort right. Once the line of cubes is sorted, it won’t follow the remainder of the code to complete the path/maze given like move forward10 inches, turn right, move forward 8 inches etc. How do you code it so it’s out of the forever loop and actually continues to finish the challenge/code given. A student tried a repeat loop and it worked, but is there no way to code out of a forever loop?

  2. What does the block " optical to color/gesture mode" mean or do? How is it used?

  3. The blocks “broadcast message” what is it’s main purpose? I have used it as getting the robot to do 2 things simultaneously. Is this correct, or what else do I do with it?

1 Like

Hi @Bonnie_Sikorski ! Those are some excellent questions :slightly_smiling_face:

  1. You can use the “break” block to exit a forever loop and run any code that comes after the loop

  2. Setting the Optical Sensor to “Gesture mode” enables the sensor to detect Gestures(a movement going up, down, left, or right) being made in front of the sensor. Setting the Optical Sensor to “Color mode” enables the sensor to detect the color of an object in front of the sensor. Please note, whenever you set the Optical sensor to one mode, it disables the other(i.e. you cannot detect colors if in Gesture Mode, and you cannot detect gestures in Color mode)

  3. The purpose of the Broadcast Message block is to trigger a stack of blocks/code to run whenever a user-chosen event occurs. (I.e. you may want to wait to run a stack of blocks until you detect the color red. You can attach that stack to a “When I receive message1" block, and then use the “Broadcast message1” block inside of an if statement checking to see if red is detected.). You are correct that the original block stack will continue running whilst the stack(s) that received the message begin to run.

Hope this adequately answers your questions!

1 Like

Hi @Bonnie_Sikorski! Great questions! And thank you @Cole_Guesman for that helpful response :slight_smile: I also wanted to note there are some example projects and tutorial videos built into VEXcode IQ that might also help:

I hope this helps!

1 Like

Thank you! It does help a little. I tried the Break- and looked at the samples as well. I added the breaks in with either the bumper or distance sensor. It worked sometimes. I have the bots build in the clawbot form and the optical light is turned on. I have found that turning my class lights off made it work more consistently. Isn’t that odd? Didn’t realize the sensors were that sensitive. Some still had issues getting away from the loop and completing code. I am not sure. In the sample code, with all the “print” code. Can you explain why that is used just to clarify my thoughts? Thank you for all the assistance!

Hi @Bonnie_Sikorski! I was just wondering can you post a picture of the code you were trying? This can help us troubleshoot as well :smiley:

Using the [Print] blocks allow for visual feedback as the code progresses. From the “Breaking Loops” example above, the [Print] blocks are used to give a visual indication of the variable value during each iteration of the loop. This makes the product of the project flow more visible.

Using [Print] blocks is a great way to troubleshoot. You can print many things, including sensor values and text. You can also use the Sensor Dashboard to understand what the sensor is “seeing.” This is a great way to troubleshoot sensors but also to obtain the values that you will use in the code.
brain screen - optical dashboard

For a little more information on this, we cover the Optical and Distance sensors in the last chapter of our IQ Training Course that I would recommend checking out :slight_smile:

Let me know if this helps!