I am currently working on a project that utilizes encoders to track the position of a series of lead screws. This code block is being used to grab the position of each encoder and display it to the user with a while loop (no key pressed), as well as also giving them the option to reset any of the three positions to zero (by pressing either 'x', 'y' or 'z' on the keyboard).
As is, the user would need to press the 'x' key once to zero the x-position, press the 'y' key 2 times to zero the y-position, the 'z' key 3 times to zero the z-position and 4 times to end the program. The number of times the key needs to be pressed in order for the switch/case to happen corresponds to where it falls numerically in the list of cases. However, I need is for each key to be pressed only once for the action to take place. How would I go about fixing the switch/case structure (or whatever else) to make that happen?
Also, it does not seem to like the !Console.KeyAvaible in the first switch statement to check if a key has been pressed. The error is "cannot convert bool to System.ConsoleKey" Is there another way to phrase this?
while (true){
long Position0 = encoder0.Position;
long Position1 = encoder1.Position;
long Position2 = encoder2.Position;
ConsoleKeyInfo input = Console.ReadKey();
switch (input.Key){
case !Console.KeyAvailable:
// initialize encoder positions and do math to convert to mm
float Position0f = Convert.ToSingle(Position0);
float Position1f = Convert.ToSingle(Position1);
float Position2f = Convert.ToSingle(Position2);
float LinearPosition0 = Position0f / 400;
float LinearPosition1 = Position1f / 400;
float LinearPosition2 = Position2f / 400;
Console.SetCursorPosition(0, 0);
Console.WriteLine("x Position: {0:0.0000##} mm", LinearPosition0);
Console.SetCursorPosition(0, 1);
Console.WriteLine("y Position: {0:0.0000##} mm", LinearPosition1);
Console.SetCursorPosition(0, 2);
continue;
default:
switch (input.Key){
case ConsoleKey.X:
Console.WriteLine("\nYou selected X!");
Position0 = 0;
break;
case ConsoleKey.Y:
Console.WriteLine("\nYou selected Y!");
Position1 = 0;
break;
case ConsoleKey.Z:
Console.WriteLine("\nYou selected Z!");
Position2 = 0;
break;
case ConsoleKey.Escape:
Console.WriteLine("\nYou selected Esc! Exiting program.");
break;
default:
break;
}
break;
}
}
In addition to the problem with the keys, the position displayed in the first if statement does not change. Where should I initialize the Position0/1/2 so that they can be reset to zero in the second if statement while still being updated every loop in the first?
You need to check if a key is available before reading a key, for example:
long Position0 = 0;
long Position1 = 0;
long Position2 = 0;
bool isEnd = false;
while ( !isEnd )
{
Position0 = encoder0.Position;
Position1 = encoder1.Position;
Position2 = encoder2.Position;
//Position0++; Position1++; Position2++;
float LinearPosition0 = Convert.ToSingle(Position0) / 400;
float LinearPosition1 = Convert.ToSingle(Position1) / 400;
float LinearPosition2 = Convert.ToSingle(Position2) / 400;
Console.SetCursorPosition(0, 0);
Console.WriteLine("x Position: {0:0.0000##} mm", LinearPosition0);
Console.SetCursorPosition(0, 1);
Console.WriteLine("y Position: {0:0.0000##} mm", LinearPosition1);
Console.SetCursorPosition(0, 2);
Console.WriteLine("z Position: {0:0.0000##} mm", LinearPosition2);
if ( Console.KeyAvailable )
switch ( Console.ReadKey(true).Key )
{
case ConsoleKey.X:
Console.WriteLine("\nYou selected X!");
Position0 = 0;
break;
case ConsoleKey.Y:
Console.WriteLine("\nYou selected Y!");
Position1 = 0;
break;
case ConsoleKey.Z:
Console.WriteLine("\nYou selected Z!");
Position2 = 0;
break;
case ConsoleKey.Escape:
Console.WriteLine("\nYou selected Esc! Exiting program.");
isEnd = true;
break;
}
}
You can also hide the cursor if needed.
Console.CursorVisible = false;