HELP - If statement with multiple Line Trackers

I have three separate mechanisms with motors attached to them and a line tracker asscoiated with it. I want all 3 motors to turn on once a bumper is pressed. Then when a line tracker senses darkness I want that motor to spin faster for a few seconds before returned to it original speed. Backgroud - I have students who made a classic"shooting gallery" game from a carnival. They have 3 line trackers each associated to a different motor. Once the nerf bullet hits the sensor, they want the mechanism(motor) to spin faster for a few seconds then return to normal speed.

I’m struggling to get this to work. This is what I have so far. But it only starts the motors and nothing when my finger goes over the line tracker.

#include “vex.h”

using namespace vex;

int main() {

// Initializing Robot Configuration. DO NOT REMOVE!

vexcodeInit();

int threshold = 55;

{

while(true){

if(bump1.pressing()){

motor1.setVelocity(20,rpm);

motor2.setVelocity(20,rpm);

motor3.setVelocity(20,rpm);

motor1.spin(forward);

motor2.spin(forward);

motor3.spin(forward);

waitUntil(line1.value(pct)<threshold);

motor1.stop();

wait(.5, seconds);

motor1.setVelocity(150,rpm);

motor1.spin(reverse);

wait(4,seconds);

motor1.stop();

waitUntil(line2.value(pct)<threshold);

motor2.stop();

wait(.5, seconds);

motor2.setVelocity(150,rpm);

motor2.spin(reverse);

wait(4,seconds);

motor2.stop();

waitUntil(line3.value(pct)<threshold);

motor3.stop();

wait(.5, seconds);

motor3.setVelocity(150,rpm);

motor3.spin(forward);

wait(4,seconds);

motor3.stop();

  1. Once again, format your code using ```.
  2. You don’t need every other line blank.
#include “vex.h”

using namespace vex;

int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();

int threshold = 55;
{ // What is this here for?

while (true) {
  if (bump1.pressing()) {
    motor1.setVelocity(20,rpm);
    motor2.setVelocity(20,rpm);
    motor3.setVelocity(20,rpm);
    motor1.spin(forward);
    motor2.spin(forward);
    motor3.spin(forward);

    waitUntil(line1.value(pct) < threshold);
    motor1.stop();

    wait(.5, seconds);
    motor1.setVelocity(150, rpm);
    motor1.spin(reverse);

    wait(4,seconds);
    motor1.stop();

    waitUntil(line2.value(pct) < threshold);
    motor2.stop();

    wait(.5, seconds);
    motor2.setVelocity(150,rpm);
    motor2.spin(reverse);

    wait(4, seconds);
    motor2.stop();

    waitUntil(line3.value(pct) < threshold);
    motor3.stop();

    wait(.5, seconds);
    motor3.setVelocity(150, rpm);
    motor3.spin(forward);

    wait(4, seconds);
    motor3.stop();
  1. Why is there a random curly brace before your while loop?
  2. Why are there no curly braces at the end?
  3. Indent your code.
  4. If you want to have all three sensors sensing and waiting at the same time, you’ll need to use threading. Look it up; there are plenty of resourses.
2 Likes

Hi Dan. I’ve formatted your code a little better and fixed your brackets and added comments:

#include “vex.h”
using namespace vex;

int main() 
{
		// Initializing Robot Configuration. DO NOT REMOVE!
	vexcodeInit();

	int threshold = 55;

	while(true)
	{
		if(bump1.pressing())
		{
				//Set motor velocities.
			motor1.setVelocity(20,rpm);
			motor2.setVelocity(20,rpm);
			motor3.setVelocity(20,rpm);
			
				//Set direction for motors
			motor1.spin(forward);
			motor2.spin(forward);
			motor3.spin(forward);

				///////FIRST ROW////////
			waitUntil(line1.value(pct)<threshold);
			motor1.stop();
			wait(.5, seconds);
			motor1.setVelocity(150,rpm);
			motor1.spin(reverse);
			wait(4,seconds);
			motor1.stop();

				///////SECOND ROW////////
			waitUntil(line2.value(pct)<threshold);
			motor2.stop();
			wait(.5, seconds);
			motor2.setVelocity(150,rpm);
			motor2.spin(reverse);
			wait(4,seconds);
			motor2.stop();
			
				///////THIRD ROW////////
			waitUntil(line3.value(pct)<threshold);
			motor3.stop();
			wait(.5, seconds);
			motor3.setVelocity(150,rpm);
			motor3.spin(forward);
			wait(4,seconds);
			motor3.stop();
			
		}//end if
		
	}//while
	
}//Main()

I don’t know if you are parent, teacher, or teacher/coach but, I know that teachers and/or coaches are sometimes reluctant to ask for help. And chances are there is a programmer or someone who knows programming among your parents. It doesn’t hurt to ask and I’m betting most would be willing and even happy or eager to help.

For future reference, be sure to try and format the code and wrap it in your post using “code” tags - the easiest is HTML code tags but be sure that anything you put inside the code-tagged area is indented at least once. Well, formatted code should follow the example I set above if possible but for the code tags above, you will need to indent the entire thing once (just highlight the code you want to post in your IDE and hit tab once then copy and paste it between the code tags in your post).

In the code above, what would happen if no one ever hit the line1 sensor yet lines 2 or 3 are constantly hit (rhetorical Q)? Like the previous poster said, if you are going to wait three separate motors then look at multithreading as an option. Be sure to look at the Vexcode examples and not a straight-up C++ tutorial which is way more complex (Vexcode has tools/items that help make multithreading easier).

2 Likes

I really appreciate your post. I am a club advisor and we just got the new vex V5 kits this year. I have middle school kids who are brand new to learning this. I personally have little experience, but getting there. I finally had RobotC figured out with the old cortex, and now trying to adjust to this new setup. I made the mistake of copying their program and pasting it in here before correcting some of the bad syntax errors.

In the code. The first bumper is meant to start their game. Causing all 3 motors to spin at a low RPM. Then as each line tracker is hit, the motor would spin at a higher rpm for a few seconds before returning to the low rpm.

1 Like

So basically your options are

  1. Use multithreading, with one thread for each motor & light sensor.
  2. Store time values in variables.

Since I already described multithreading in your other topic (I think), I’ll describe time in variables here. The basic idea is, whenever a light sensor is triggered, store the time in its variable. Whenever current time - stored time < your desired number of seconds, motor velocity = 100. Whenever current time - stored time > your desired time, motor velocity = 50.

int hitLevel = 55;
int speedTime = 2;
double time1 = -speedTime; 
// So that it's not triggered the first two seconds of your code
double time2 = -speedTime;
double time3 = -speedTime;

int lowSpeed = 50;
int highSpeed = 100;

waitUntil(bumper.pressing());
// Set motor velocities to lowSpeed

while (true):
  if (tracker1 < hitLevel):
    time1 = Brain.Timer.value();
  if (tracker2 < hitLevel):
    time2 = Brain.Timer.value();
  ...
  if (Brain.Timer.value() - time1 < 2):
    motor1.setVelocity(highSpeed, percent);
  else:
    motor1.setVelocity(lowSpeed, percent);
  // Repeat for other three motors and times
2 Likes