I am trying to figure out how to use a rangefinder with v5 using a cortex senser. The code template is being used and the code prints on the brain and everything but i never picks up the object.
I have a script set up to make a drivetrain go and decelerate when it gets in a certain range of a wall and comes to a stop. It uses 2 buttons, a rangefinder, and a 2 motor drivetrain:
Drivetrain: 1&10
Controller: Controller1
Button(On): H
Button(Off): D
RangefinderA: A&B
#pragma region VEXcode Generated Robot Configuration
// Make sure all required headers are included.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include "vex.h"
using namespace vex;
// Brain should be defined by default
brain Brain;
// START V5 MACROS
#define waitUntil(condition) \
do { \
wait(5, msec); \
} while (!(condition))
#define repeat(iterations) \
for (int iterator = 0; iterator < iterations; iterator++)
// END V5 MACROS
// Robot configuration code.
motor LeftDriveSmart = motor(PORT1, ratio18_1, true);
motor RightDriveSmart = motor(PORT10, ratio18_1, false);
drivetrain Drivetrain = drivetrain(LeftDriveSmart, RightDriveSmart, 319.19, 295, 40, mm, 1);
controller Controller1 = controller(primary);
bumper On = bumper(Brain.ThreeWirePort.H);
bumper Off = bumper(Brain.ThreeWirePort.D);
sonar RangeFinderA = sonar(Brain.ThreeWirePort.A);
// define variable for remote controller enable/disable
bool RemoteControlCodeEnabled = true;
#pragma endregion VEXcode Generated Robot Configuration
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: {author} */
/* Created: {date} */
/* Description: V5 project */
/* */
/*----------------------------------------------------------------------------*/
// Include the V5 Library
#include "vex.h"
using namespace vex;
// Function to move text across the screen
void moveText() {
int xPos = 1;
int yPos = 1;
int screenWidth = 60;
while(true) {
// Move text to the right
wait(150,msec);
Brain.Screen.clearLine(1);
Brain.Screen.setCursor(xPos, yPos++);
Brain.Screen.print("Thinking...");
if (yPos > screenWidth) {yPos = 1;}
// Wait for a short period of time
//this_thread::sleep_for(50);
// Clear previous text location
Brain.Screen.setCursor(xPos-1, yPos);
Brain.Screen.print("Thinking...");
}
}
int MotorSpeed = 0;
void motorAcceleration() {
int maxSpeed = 400;
int speedIncrement = 20;
int delayBetweenIncrements = 25;
while (MotorSpeed <= maxSpeed) {
Drivetrain.setDriveVelocity(MotorSpeed, percent);
Brain.Screen.clearLine(6);
Brain.Screen.print("Motor Speed %d percent", MotorSpeed);
Drivetrain.drive(forward);
wait(delayBetweenIncrements, msec);
MotorSpeed += speedIncrement;
}
}
int main() {
// Start moving text on a separate thread
thread t(moveText);
// Set the initial motor speed to 50%
double motorSpeed = 50;
while (On.pressing() == 0) {}
while (true) {
// Check if the Off bumper is pressed to end the program
while (true) {
if (Off.pressing()) {
vexSystemExitRequest();
}
// Keep driving forward unless the range is less than 500mm
if (RangeFinderA.distance(mm) < 525) {
// Slow down gradually until the bot stops before hitting an object
while (motorSpeed > 0) {
Drivetrain.drive(forward);
Drivetrain.setDriveVelocity(motorSpeed, percent);
motorSpeed -= 10;
wait(200, msec);
//Brain.Screen.setCursor(3,1);
}
Drivetrain.stop();
} else {
// Keep driving forward at a constant speed
motorAcceleration();
}
wait(10, msec);
}
}
}
So i put the code in to V5 but now it says we have an error on Button(On): H (line 83) saying it has an illegal target for annotation. Me and my advisor can not figure out why it is saying that we thought it was just mad that we had not set up the controller but that did not fix it.