Toggle Button Isuess

while my team’s robot is still being built I decided to make some code that prints the values of the Joystick sensors to the ROBOTC debug stream (since there is no other way to see them while running the code). for the most part it works. However, I decided to have the accelerometers displayed separately since they are rarely used. So the button you press to show the values of the first joystick (the left LCD button) you can press again to show the accelerometers. That’s where the issue is. press the button once and it works fine, press it a second time and instead of only showing the accelerometers, it tries to show both the values of joystick 1 and the accelerometers at the same time.

also, this isn’t all my code, just the part that isn’t working.


	while(true)
	{
                int LCDMode = 4;
		bool LCDLeft = false;
		bool LCDCenter = false;
		bool LCDRight = false;


		if (nLCDButtons == 1 && !LCDLeft && LCDMode != 1 && LCDMode != 2)
		{
			LCDMode = 1;
			LCDLeft = true;
			wait1Msec(500);
		}
		else if (!nLCDButtons == 1 || LCDMode == 1)
			LCDLeft = false;
	
	
			if (nLCDButtons == 1 && !LCDLeft && LCDMode == 1 || LCDMode == 2)
		{
			LCDMode = 2;
			LCDLeft = true;
			wait1Msec(500);
		}
		else if (!nLCDButtons == 1 || LCDMode != 1)
			LCDLeft = false;


		switch(LCDMode)
		{
		case 1:// controller 1
			displayLCDString(0,0,"Joystick One ");
			writeDebugStreamLine("|     Analog    | Buttons 5 & 6   |   Buttons 7   |   Buttons 8   |"); //DebugStream Line 1
			writeDebugStream(" channel 1: %d", vexRT[Ch1]); //Start of DebugStream Line 2

			if(vexRT[Ch1] > 9 && vexRT[Ch1] < 100) // If statements are to keep everything aligned correctly
			{
				writeDebugStream("      Button 5U: %d", vexRT[Btn5U]);
			}

			else	if(vexRT[Ch1] > 99)
			{
				writeDebugStream("     Button 5U: %d", vexRT[Btn5U]);
			}

			else if(vexRT[Ch1] < 0 && vexRT[Ch1] > -10)
			{
				writeDebugStream("      Button 5U: %d", vexRT[Btn5U]);
			}

			else if(vexRT[Ch1] < -9 && vexRT[Ch1] > -100)
			{
				writeDebugStream("     Button 5U: %d", vexRT[Btn5U]);
			}

			else if(vexRT[Ch1] < -99)
			{
				writeDebugStream("    Button 5U: %d", vexRT[Btn5U]);
			}

			else
			{
				writeDebugStream("       Button 5U: %d", vexRT[Btn5U]);
			}

			writeDebugStream("      Button 7U: %d", vexRT[Btn7U]);
			writeDebugStreamLine("   Button 8U: %d", vexRT[Btn8U]); //End of DebugStream Line 2

			writeDebugStream(" channel 2: %d", vexRT[Ch2]); //Start of DebugStream Line 3

			if(vexRT[Ch2] > 9 && vexRT[Ch2] < 100)
			{
				writeDebugStream("      Button 5D: %d", vexRT[Btn5D]);
			}

			else	if(vexRT[Ch2] > 99)
			{
				writeDebugStream("     Button 5D: %d", vexRT[Btn5D]);
			}

			else if(vexRT[Ch2] < 0 && vexRT[Ch2] > -10)
			{
				writeDebugStream("      Button 5D: %d", vexRT[Btn5D]);

			}
			else if(vexRT[Ch2] < -9 && vexRT[Ch2] > -100)
			{
				writeDebugStream("     Button 5D: %d", vexRT[Btn5D]);
			}

			else if(vexRT[Ch2] < -99)
			{
				writeDebugStream("    Button 5D: %d", vexRT[Btn5D]);
			}

			else
			{
				writeDebugStream("       Button 5D: %d", vexRT[Btn5D]);
			}

			writeDebugStream("      Button 7D: %d", vexRT[Btn7D]);
			writeDebugStreamLine("   Button 8D: %d", vexRT[Btn8D]); //End of DebugStream Line 3

			writeDebugStream(" channel 3: %d", vexRT[Ch3]); //Start of DebugStream Line 4

			if(vexRT[Ch3] > 9 && vexRT[Ch3] < 100)
			{
				writeDebugStream("      Button 6U: %d", vexRT[Btn6U]);
			}

			else	if(vexRT[Ch3] > 99)
			{
				writeDebugStream("     Button 6U: %d", vexRT[Btn6U]);
			}

			else if(vexRT[Ch3] < 0 && vexRT[Ch3] > -10)
			{
				writeDebugStream("      Button 6U: %d", vexRT[Btn6U]);

			}
			else if(vexRT[Ch3] < -9 && vexRT[Ch3] > -100)
			{
				writeDebugStream("     Button 6U: %d", vexRT[Btn6U]);
			}

			else if(vexRT[Ch3] < -99)
			{
				writeDebugStream("    Button 6U: %d", vexRT[Btn6U]);
			}

			else
			{
				writeDebugStream("       Button 6U: %d", vexRT[Btn6U]);
			}

			writeDebugStream("      Button 7L: %d", vexRT[Btn7L]);
			writeDebugStreamLine("   Button 8L: %d", vexRT[Btn8L]); //End of DebugStream Line 4

			writeDebugStream(" channel 4: %d", vexRT[Ch4]); //Start of DebugStream Line 5

			if(vexRT[Ch4] > 9 && vexRT[Ch4] < 100)
			{
				writeDebugStream("      Button 6D: %d", vexRT[Btn6D]);
			}

			else	if(vexRT[Ch4] > 99)
			{
				writeDebugStream("     Button 6D: %d", vexRT[Btn6D]);
			}

			else if(vexRT[Ch4] < 0 && vexRT[Ch4] > -10)
			{
				writeDebugStream("      Button 6D: %d", vexRT[Btn6D]);

			}
			else if(vexRT[Ch4] < -9 && vexRT[Ch4] > -100)
			{
				writeDebugStream("     Button 6D: %d", vexRT[Btn6D]);
			}

			else if(vexRT[Ch4] < -99)
			{
				writeDebugStream("    Button 6D: %d", vexRT[Btn6D]);
			}

			else
			{
				writeDebugStream("       Button 6D: %d", vexRT[Btn6D]);
			}

			writeDebugStream("      Button 7R: %d", vexRT[Btn7R]);
			writeDebugStreamLine("   Button 8R: %d", vexRT[Btn8R]); //End of DebugStream Line 5
			wait1Msec(350);
			break;
			
		case 2: //Accelerometers
			writeDebugStreamLine("|  Accelerometers  |");
			writeDebugStreamLine(" AccelY: %d", vexRT[AccelY]);
			writeDebugStreamLine(" AccelX: %d", vexRT[AccelX]);
		wait1Msec(350);

It’s hard to tell from the code how you want the lcd button to work, push once for analog & buttons, push again for accelerometers, push again to go back to buttons to push twice quickly for the accelerometers. Anyway, here is one possible way.

        if (nLCDButtons == 1 && !LCDLeft && LCDMode != 1 )
        {
            LCDMode = 1;
            LCDLeft = true;
            wait1Msec(500);
        }
        else
        if (!nLCDButtons == 1 && LCDLeft && LCDMode == 1)
            LCDLeft = false;

        if (nLCDButtons == 1 && !LCDLeft && LCDMode != 2)
        {
            LCDMode = 2;
            LCDLeft = true;
            wait1Msec(500);
        }
        else
        if (!nLCDButtons == 1 && LCDLeft && LCDMode == 2)
            LCDLeft = false;

Another thing to do with the code is replace all the conditional formatting you have, for example, this;

            writeDebugStream(" channel 1: %d", vexRT[Ch1]); //Start of DebugStream Line 2

            if(vexRT[Ch1] > 9 && vexRT[Ch1] < 100) // If statements are to keep everything aligned correctly
            {
                writeDebugStream("      Button 5U: %d", vexRT[Btn5U]);
            }

            else    if(vexRT[Ch1] > 99)
            {
                writeDebugStream("     Button 5U: %d", vexRT[Btn5U]);
            }

            else if(vexRT[Ch1] < 0 && vexRT[Ch1] > -10)
            {
                writeDebugStream("      Button 5U: %d", vexRT[Btn5U]);
            }

            else if(vexRT[Ch1] < -9 && vexRT[Ch1] > -100)
            {
                writeDebugStream("     Button 5U: %d", vexRT[Btn5U]);
            }

            else if(vexRT[Ch1] < -99)
            {
                writeDebugStream("    Button 5U: %d", vexRT[Btn5U]);
            }

            else
            {
                writeDebugStream("       Button 5U: %d", vexRT[Btn5U]);
            }

            writeDebugStream("      Button 7U: %d", vexRT[Btn7U]);
            writeDebugStreamLine("   Button 8U: %d", vexRT[Btn8U]); //End of DebugStream Line 2

can be replaced by this;

            writeDebugStream(" channel 1: %-4d", vexRT[Ch1]); //Start of DebugStream Line 2
            writeDebugStream("  Button 5U: %d", vexRT[Btn5U]);
            writeDebugStream("      Button 7U: %d", vexRT[Btn7U]);
            writeDebugStreamLine("    Button 8U: %d", vexRT[Btn8U]); //End of DebugStream Line 2

Notice the %-4d in the first line, this tells the function “writeDebugStream” to use 4 positions for the output and to left justify the number (just using %4d would right justify) so if the value was 9 then the output would be 9 plus three spaces.

This is what seems to work for me.

task main()
{
    int LCDMode = 4;
    bool LCDLeft = false;
    bool LCDCenter = false;
    bool LCDRight = false;

    // so I can see the lcd 
    bLCDBacklight = true;
    
    while(true)
        {
        if (nLCDButtons == 1 && !LCDLeft && LCDMode != 1 )
        {
            LCDMode = 1;
            LCDLeft = true;
            wait1Msec(500);
        }
        else
        if (!nLCDButtons == 1 && LCDLeft && LCDMode == 1)
            LCDLeft = false;

        if (nLCDButtons == 1 && !LCDLeft && LCDMode != 2)
        {
            LCDMode = 2;
            LCDLeft = true;
            wait1Msec(500);
        }
        else
        if (!nLCDButtons == 1 && LCDLeft && LCDMode == 2)
            LCDLeft = false;


        switch(LCDMode)
        {
        case 1:// controller 1
            displayLCDString(0,0,"Joystick One ");
            writeDebugStreamLine("|     Analog    | Buttons 5 & 6   | Buttons 7     | Buttons 8     |"); //DebugStream Line 1
            
            writeDebugStream(" channel 1: %-4d", vexRT[Ch1]); //Start of DebugStream Line 2
            writeDebugStream("  Button 5U: %d", vexRT[Btn5U]);
            writeDebugStream("      Button 7U: %d", vexRT[Btn7U]);
            writeDebugStreamLine("    Button 8U: %d", vexRT[Btn8U]); //End of DebugStream Line 2
    
            writeDebugStream(" channel 2: %-4d", vexRT[Ch2]); //Start of DebugStream Line 3
            writeDebugStream("  Button 5D: %d", vexRT[Btn5D]);
            writeDebugStream("      Button 7D: %d", vexRT[Btn7D]);
            writeDebugStreamLine("    Button 8D: %d", vexRT[Btn8D]); //End of DebugStream Line 3

            writeDebugStream(" channel 3: %-4d", vexRT[Ch3]); //Start of DebugStream Line 4
            writeDebugStream("  Button 6U: %d", vexRT[Btn6U]);
            writeDebugStream("      Button 7L: %d", vexRT[Btn7L]);
            writeDebugStreamLine("    Button 8L: %d", vexRT[Btn8L]); //End of DebugStream Line 4

            writeDebugStream(" channel 4: %-4d", vexRT[Ch4]); //Start of DebugStream Line 5
            writeDebugStream("  Button 6D: %d", vexRT[Btn6D]);
            writeDebugStream("      Button 7R: %d", vexRT[Btn7R]);
            writeDebugStreamLine("    Button 8R: %d", vexRT[Btn8R]); //End of DebugStream Line 5
            wait1Msec(350);
            break;
            
        case 2: //Accelerometers
            writeDebugStreamLine("|  Accelerometers  |");
            writeDebugStreamLine(" AccelY: %d", vexRT[AccelY]);
            writeDebugStreamLine(" AccelX: %d", vexRT[AccelX]);
            wait1Msec(350);
        
        // all other cases
        default: 
            break;
    
        wait1Msec(10); // don't hog the cpu !       
        }
    }
}

thank you! I took out all the if statements (I cried when you told me it wasn’t necessary). I also copied the code below into into a separate .c file.

task main()
{
    int LCDMode = 4;
    bool LCDLeft = false;
    bool LCDCenter = false;
    bool LCDRight = false;

    // so I can see the lcd 
    bLCDBacklight = true; //didn't need this, I use the emulated LCD bult into robotc ;)
    
    while(true)
        {
        if (nLCDButtons == 1 && !LCDLeft && LCDMode != 1 )
        {
            LCDMode = 1;
            LCDLeft = true;
            wait1Msec(500);
        }
        else
        if (!nLCDButtons == 1 && LCDLeft && LCDMode == 1)
            LCDLeft = false;

        if (nLCDButtons == 1 && !LCDLeft && LCDMode != 2)
        {
            LCDMode = 2;
            LCDLeft = true;
            wait1Msec(500);
        }
        else
        if (!nLCDButtons == 1 && LCDLeft && LCDMode == 2)
            LCDLeft = false;


        switch(LCDMode)
        {
        case 1:// controller 1
            displayLCDString(0,0,"Joystick One ");
            writeDebugStreamLine("|     Analog    | Buttons 5 & 6   | Buttons 7     | Buttons 8     |"); //DebugStream Line 1
            
            writeDebugStream(" channel 1: %-4d", vexRT[Ch1]); //Start of DebugStream Line 2
            writeDebugStream("  Button 5U: %d", vexRT[Btn5U]);
            writeDebugStream("      Button 7U: %d", vexRT[Btn7U]);
            writeDebugStreamLine("    Button 8U: %d", vexRT[Btn8U]); //End of DebugStream Line 2
    
            writeDebugStream(" channel 2: %-4d", vexRT[Ch2]); //Start of DebugStream Line 3
            writeDebugStream("  Button 5D: %d", vexRT[Btn5D]);
            writeDebugStream("      Button 7D: %d", vexRT[Btn7D]);
            writeDebugStreamLine("    Button 8D: %d", vexRT[Btn8D]); //End of DebugStream Line 3

            writeDebugStream(" channel 3: %-4d", vexRT[Ch3]); //Start of DebugStream Line 4
            writeDebugStream("  Button 6U: %d", vexRT[Btn6U]);
            writeDebugStream("      Button 7L: %d", vexRT[Btn7L]);
            writeDebugStreamLine("    Button 8L: %d", vexRT[Btn8L]); //End of DebugStream Line 4

            writeDebugStream(" channel 4: %-4d", vexRT[Ch4]); //Start of DebugStream Line 5
            writeDebugStream("  Button 6D: %d", vexRT[Btn6D]);
            writeDebugStream("      Button 7R: %d", vexRT[Btn7R]);
            writeDebugStreamLine("    Button 8R: %d", vexRT[Btn8R]); //End of DebugStream Line 5
            wait1Msec(350);
            break;
            
        case 2: //Accelerometers
            writeDebugStreamLine("|  Accelerometers  |");
            writeDebugStreamLine(" AccelY: %d", vexRT[AccelY]);
            writeDebugStreamLine(" AccelX: %d", vexRT[AccelX]);
            wait1Msec(350);
        
        // all other cases
        default: 
            break;
    
        wait1Msec(10); // don't hog the cpu !       
        }
    }
}

I then put the code below in my actual Debug stream and I kept switching from the accelerators to joystick 2.

        if (nLCDButtons == 1 && !LCDLeft && LCDMode != 1 )
        {
            LCDMode = 1;
            LCDLeft = true;
            wait1Msec(500);
        }
        else
        if (!nLCDButtons == 1 && LCDLeft && LCDMode == 1)
            LCDLeft = false;

        if (nLCDButtons == 1 && !LCDLeft && LCDMode != 2)
        {
            LCDMode = 2;
            LCDLeft = true;
            wait1Msec(500);
        }
        else
        if (!nLCDButtons == 1 && LCDLeft && LCDMode == 2)
            LCDLeft = false;

I will put my entire code below now that it doesn’t have all the if statements

task main
{


	int LCDMode = 4;
	while(true)
	{
		bool LCDLeft = false;
		bool LCDCenter = false;
		bool LCDRight = false;


        if (nLCDButtons == 1 && !LCDLeft && LCDMode != 1 )
        {
            LCDMode = 1;
            LCDLeft = true;
            wait1Msec(500);
        }
        else
        if (!nLCDButtons == 1 && LCDLeft && LCDMode == 1)
            LCDLeft = false;

        if (nLCDButtons == 1 && !LCDLeft && LCDMode != 2)
        {
            LCDMode = 2;
            LCDLeft = true;
            wait1Msec(500);
        }
        else
        if (!nLCDButtons == 1 && LCDLeft && LCDMode == 2)
            LCDLeft = false;



		if (nLCDButtons == 4 && !LCDRight)
		{
			LCDMode = 3;
			LCDRight = true;
		}
		else if (!nLCDButtons == 4)
			LCDRight = false;

		if (nLCDButtons == 2 && !LCDCenter)
		{
			LCDMode = 4;
			LCDCenter = true;
		}
		else if (!nLCDButtons == 2)
			LCDCenter = false;

		switch(LCDMode)
		{
		case 1:// controller 1
			displayLCDString(0,0,"Joystick One ");
			writeDebugStreamLine("|     Analog    | Buttons 5 & 6   |   Buttons 7   |   Buttons 8   |"); //DebugStream Line 1
			
			
			writeDebugStream(" channel 1: %-4d", vexRT[Ch1]); //Start of DebugStream Line 2
			writeDebugStream("      Button 5U: %d", vexRT[Btn5U]);
			writeDebugStream("      Button 7U: %d", vexRT[Btn7U]);
			writeDebugStreamLine("   Button 8U: %d", vexRT[Btn8U]); //End of DebugStream Line 2

			
			writeDebugStream(" channel 2: %-4d", vexRT[Ch2]); //Start of DebugStream Line 3
			writeDebugStream("      Button 5D: %d", vexRT[Btn5D]);
			writeDebugStream("      Button 7D: %d", vexRT[Btn7D]);
			writeDebugStreamLine("   Button 8D: %d", vexRT[Btn8D]); //End of DebugStream Line 3

			
			writeDebugStream(" channel 3: %-4d", vexRT[Ch3]); //Start of DebugStream Line 4
			writeDebugStream("      Button 6U: %d", vexRT[Btn6U]);
			writeDebugStream("      Button 7L: %d", vexRT[Btn7L]);
			writeDebugStreamLine("   Button 8L: %d", vexRT[Btn8L]); //End of DebugStream Line 4

			
			writeDebugStream(" channel 4: %-4d", vexRT[Ch4]); //Start of DebugStream Line 5
			writeDebugStream("      Button 6D: %d", vexRT[Btn6D]);
			writeDebugStream("      Button 7R: %d", vexRT[Btn7R]);
			writeDebugStreamLine("   Button 8R: %d", vexRT[Btn8R]); //End of DebugStream Line 5
			wait1Msec(350);
			break;

		case 2: //Accelerometers
			displayLCDString(0,0,"Accelerometers ");
			writeDebugStreamLine("|  Accelerometers  |");
			writeDebugStreamLine(" AccelY: %d", vexRT[AccelY]);
			writeDebugStreamLine(" AccelX: %d", vexRT[AccelX]);
			writeDebugStreamLine("");
			writeDebugStreamLine("");
			wait1Msec(350);

		case 3:
			displayLCDString(0,0,"Joystick Two ");
			writeDebugStreamLine("|     Analog    | Buttons 5 & 6   |   Buttons 7   |   Buttons 8   |"); //DebugStream Line 1

			
			writeDebugStream(" channel 1: %-4d", vexRT[Ch1Xmtr2]); //Start of DebugStream Line 2
			writeDebugStream("      Button 5U: %d", vexRT[Btn5UXmtr2]);
			writeDebugStream("      Button 7U: %d", vexRT[Btn7UXmtr2]);
			writeDebugStreamLine("   Button 8U: %d", vexRT[Btn8UXmtr2]); //End of DebugStream Line 2


			writeDebugStream(" channel 2: %-4d", vexRT[Ch2Xmtr2]); //Start of DebugStream Line 3
			writeDebugStream("      Button 5D: %d", vexRT[Btn5DXmtr2]);
			writeDebugStream("      Button 7D: %d", vexRT[Btn7DXmtr2]);
			writeDebugStreamLine("   Button 8D: %d", vexRT[Btn8DXmtr2]); //End of DebugStream Line 3

			
			writeDebugStream(" channel 3: %-4d", vexRT[Ch3Xmtr2]); //Start of DebugStream Line 4
			writeDebugStream("      Button 6U: %d", vexRT[Btn6UXmtr2]);
			writeDebugStream("      Button 7L: %d", vexRT[Btn7LXmtr2]);
			writeDebugStreamLine("   Button 8L: %d", vexRT[Btn8LXmtr2]); //End of DebugStream Line 4

			
			writeDebugStream(" channel 4: %-4d", vexRT[Ch4Xmtr2]); //Start of DebugStream Line 5
			writeDebugStream("      Button 6D: %d", vexRT[Btn6DXmtr2]);
			writeDebugStream("      Button 7R: %d", vexRT[Btn7RXmtr2]);
			writeDebugStreamLine("   Button 8R: %d", vexRT[Btn8RXmtr2]); //End of DebugStream Line 5
			wait1Msec(350);
			break;

		case 4: //For autonomous debugging, put what you want here.

			break;
		}
	}
}

So does it work OK now?

sorry, it kept switching from the accelerators to joystick 2 after pressing the left button to get to the accelerators. so no it doesn’t work.

You missed a “break” in the “case 2:” condition, that’s probably it. I may rewrite the button handling for you later, it’s not very clear and can be improved. You might wan’t to look at some of the code in this thread.

ROBOTC LCD autonomous selection

Finally got to test it again. It was the missing break (if it wasn’t it would probably cause problems anyways :p) so thanks for seeing that. I find my handling to be pretty clear, but that’s just me and my backwards dyslexic mind. Also, that thread you posted on LCD buttons is for autonomous selection. My debug stream code runs in the background to constantly print the values of the joysticks (if the right button is pressed) to the debug stream during any competition state. I don’t even have an LCD screen, I use the remote LCD built into ROBOTC because it’s easier than pushing a button on the robot while it’s being used. So if you really want to rewrite my button handling for me go ahead, but I don’t see it as a necessity.