Cortex PLTW Natural Coding Help

Hi, I am currently doing a project with a VEX Cortex test bed using bump switch, potentiometer, light sensor, etc. There is coding needed for this project using PLTW Natural language, not blocks, but free written code. Right now we are doing a section on “if-else” coding. These are things that I need help coding with, (Thank you to anyone who can help me with this code)

  1. Write a program that will do the following:
    • Use the if-else command to program the green LED to turn on if the bumper switch is pressed,
    and off if it’s released.
    • Loop Forever.

  2. Write a program that will do the following:
    • Program a flashlight to come on only when the light sensor is covered.
    • Loop forever.

  3. Write a program that will do the following:
    Four conditions:
    a) When the bump switch is pressed, the left motor turns CW half speed
    b) When the limit switch is pressed, the right motor turns CW half speed
    c) When the light sensor is covered, the flashlight comes on
    d) Everything is off
    Loop forever.

SensorType[in1] = sensorTouch; //configure bumper switch
SensorType[dgtl1] = sensorDigitalOut; //configure LED
while(true)
{
    if(sensorValue[in1] == 1) //if bumper switch is pressed
    {
        SensorValue[dgtl1] = 1; //turn on LED
    }
    else
    {
        SensorValue[dgtl1] = 0; //turn off LED
    }
}
SensorType[in1] = sensorLightActive; //configure light sensor
SensorType[dgtl1] = sensorDigitalOut; // configure flashlight
while(true)
{
    if(SensorValue[in1] < 50) /if light sensor is covered
    {
        SensorValue[dgtl1] = 1; //turn on flashlight
    }
    else
    {
        SensorValue[dgtl1] = 0; //turn off flashlight
    }
}
SensorType[in1] = sensorTouch; //configure bump switch
SensorType[in2] = sensorTouch; //configure limit switch
SensorType[in3] = sensorTouch; //configure light sensor
SensorType[dgtl1] = sensorDigitalOut; //configure left motor
SensorType[dgtl2] = sensorDigitalOut; //configure right motor
SensorType[dgtl3] = sensorDigitalOut; //configure flashlight
while(true)
{
    if(SensorValue[in1] == 1) //if bump switch is pressed
    {
         motor[dgtl1] = 50; //turn left motor at half speed
    }
    else if(SensorValue[in2] == 1) //if limit switch is pressed
    {
        motor[dgtl2] = 50; //turn right motor at half speed
    }
    else if(SensorValue[in3] < 50) //if light sensor is covered
    {
        SensorValue[dgtl3] = 1; //turn on flashlight
    }
    else
    {
        motor[dgtl1] = 0; //turn off flashlight
        motor[dgtl2] = 0;
        SensorValue[dgtl3] = 0;
    }
}

I recommend looking through these slides to get a better understanding of how to code for future projects: AR_3_2_A_GatewayVEXTestbed.pptx - Google Slides
Hope this helps.

1 Like

Hope you get an A with @jo7432EE doing your homework for you :slight_smile:

3 Likes

Thank you so much! I had a quick question about my friend’s part of the code. This code should work, however we get a yellow X error with the words; Warning:Mismatched typedefs. Converting typedef ‘char’ to typedef ‘tSensors’, value ‘potentionmeter’

What is wrong with it?
while(1 == 1) //loop forever
{

	if(bumpSwitch == 1) //button starts right motor
	{
		startMotor(rightMotor, 67);
	}

	if(limitSwitch == 1) //limit switch starts left motor
	{
		startMotor(leftMotor, -67);
	}

	else //if neither is pressed motor stops
	{
		stopMotor(rightMotor);
		stopMotor(leftMotor);
	}
}

It appears that the limit or bump switch is declared as a char type but is being used as a tSensor type. To fix this your friend should declare the limit switch and bump switch as a tSensor type instead of a char type.

2 Likes