Code: Select all
//The key points of this program to take note of are lines 10, 12, 70, and 99
bool capsLock = 1, stopLoop = 0;
byte fileHandle;
int fSize, p1score = 365;
int characterPos = 0; //Which character is currently selected
int ASCIIPos; //The raw ASCII value of the character that will be added
int character, allowedCharacters;
string scores;
string current = ""; //*What has been typed so far
string name;
string currentCharacter = "A"; //*The character that is to be added
task main()
{
SetSensor(IN_1, SENSOR_TOUCH);
SetSensor(IN_2, SENSOR_ROTATION); //Initialize an NXT touch sensor and an RCX rotation sensor
allowedCharacters = 10; //This executes insead of the four lines below for the purpose of letting you guys debug this
//if(p1score >= 600)
//allowedCharacters = 9;
//else
//allowedCharacters = 10;
//OpenFileRead("HighScores.txt", fSize, fileHandle); //Don't worry about this and the three lines below it
//ReadLnString(fileHandle, scores);
//CloseFile(fileHandle);
//p1score = StrToNum(SubStr(scores, Pos("/", scores) + 1, Pos("\\", scores) - Pos("/", scores) - 1));
TextOut(3, LCD_LINE1, "Enter your name:");
TextOut(0, LCD_LINE3, "Caps Lock is ON");
TextOut(0, LCD_LINE4, "Next: A");
TextOut(0, LCD_LINE6, "_");
TextOut(9, LCD_LINE8, "Chars left: " + NumToStr(allowedCharacters - 1));
PlayTone(610,140);
Wait(220);
PlayTone(511,100);
Wait(180);
PlayTone(610,140);
Wait(140);
while(true)
{
PlayTone(610,140); //Just an indicator for debugging purposes
Wait(140);
stopLoop = 0;
while(stopLoop == 0)
{
if(characterPos >= 0 && characterPos <= 25) //If the option the user is about to select is a letter of the alphabet...
{
if(capsLock) //If Caps Lock is on...
ASCIIPos = characterPos + 65; //Make the letter uppercase
else
ASCIIPos = characterPos + 97; //Make the letter lowercase
currentCharacter = FlattenVar(ASCIIPos); //Make the current character the flattened raw ASCII value of the character position
}
if(characterPos == 26) //If we want to press space...
currentCharacter = " "; //Make the next character a space
TextOut(3, LCD_LINE1, "Enter your name:");
if(capsLock) //If Caps Lock is on...
TextOut(0, LCD_LINE3, "Caps Lock is ON"); //Say that Caps Lock is on
else //If Caps Lock is NOT on...
TextOut(0, LCD_LINE3, "Caps Lock is OFF");//Say that Caps Lock is off
if(characterPos >= 0 && characterPos <= 25) //If the character position is between 0 and 25...
TextOut(0, LCD_LINE4, "Next: " + currentCharacter); //Then make the next character a letter of the alphabet
if(characterPos == 26) //If the character position is 26...
TextOut(0, LCD_LINE4, "Next: SPACE"); //Then make the next character a space
if(characterPos == 27) //If the character position is 27...
TextOut(0, LCD_LINE4, "Next: BACKSPACE"); //Then make the next character a backspace
if(characterPos == 28) //If the character position is 28...
TextOut(0, LCD_LINE4, "Next: CAPS LOCK"); //Then toggle Caps Lock
if(characterPos == 29) //If the character position is 29...
TextOut(0, LCD_LINE4, "Next: DONE"); //Then submit
TextOut(0, LCD_LINE6, current + "_"); //*Show what has been typed so far
TextOut(9, LCD_LINE8, "Chars left: " + NumToStr(allowedCharacters - 1)); //Show how many characters are left (not finished with this line of code yet; what is displayed is only temporary)
Wait(30); ClearScreen();
if(SENSOR_2 < -8) //If the rotation sensor gets moved to the left...
{
ClearSensor(IN_2); //Reset the rotation sensor,
if(characterPos == 0)
characterPos = 29; //and set the character position to 29 if it's 0
else
characterPos--; //and decrement the character position if it's not equal to 0
}
if(SENSOR_2 > 8) //If the rotation sensor gets moved to the right...
{
ClearSensor(IN_2); //Reset the rotation sensor,
if(characterPos == 29)
characterPos = 0; //and set the character position to 0 if it's 29,
else
characterPos++; //and increment the character position if it's not equal to 29
} //Essentially, the above 15 lines of code keep the character position between 0 and 29
if(SENSOR_1 == 1)
{
until(SENSOR_1 == 0);
stopLoop = 1; //End the loop when the touch sensor has been pressed and released
}
}
//Once the loop has ended...
if(characterPos == 28) //If the user has selected the Caps Lock option...
capsLock = !capsLock; //Then toggle Caps Lock
if(characterPos >= 0 && characterPos <= 26) //If the user's selection is a letter of the alphabet, or a space, then...
current = StrCat(current, currentCharacter); //*This SHOULD add the selected character onto what you're typing, correct? It doesn't for me. I've also tried removing the if, and it still didn't work.
//current = current + currentCharacter; //Tried this as an alternative, and it didn't work
//name = StrCat(current, currentCharacter);
//current = name; //Also tried it this way just in case it didn't like string variables on both sides, and it still didn't work
}
}
What you're supposed to do is move the rotation sensor until you have selected the character/typing option you want, and then press and release the touch sensor. It should then add the selected character or perform the indicated typing operation. Note that the actions for the backspace and done functions have not been implemented yet.
I find it strange that it didn't work, because this quick test worked just fine for me:
Code: Select all
string current = "", character = "A";
task main()
{
while(true)
{
repeat(16)
{
TextOut(0, LCD_LINE4, current);
Wait(1000); ClearScreen();
current = current + character;
}
current = "";
}
}