Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years ago.
input alphabet to an LCD via PS3/PS2 Controller
can someone give me the logic on how i can post letters to an LCD.
basically, the scenario would be like this.
you have an 2x16 LCD, and you use the PS2/PS3 controller to input a message or a letter.
initially you start at lcd.locate(0,0); when i click PS3 (X) button, i start with letter A.
if i continuously clicked on the (X) button, it will change to letter B, until it reaches letter Z.
arrow keys, will navigate to the column/row of the lcd. so if i click right button, itll move 1 row lcd.locate(0,1);
i just need the logic syntax, and not quite knowledgeable yet on how i code it properly.
does anybody managed to develop a program like this?
thanks :)
1 Answer
11 years ago.
Haven't done something like this, but I would probably store the current location, then if a certain button is detected, move the current position, of course taking into account you stay in an allowed location.
Regarding the letter, something like:
//Global variables: char currentLetter; //You must initialize it somewhere to 'A' int posX, posY; //And this must be set correctly also somewhere else void incrementLetter( void ) { //Increment current letter: currentLetter++; //Check if we are past Z, then restart at A: if (currentLetter > 'Z') currentLetter = 'A'; //Write the letter lcd.putc(currentLetter); //I assume the LCD autoincremented the position, so set it back lcd.locate(posX, posY); }
what is the equivalent of "foreach" in c++? what i had in mind is to put all characters into a string, and then call each character in a loop.
char alpha = "abcdefg"; foreach (char c in alpha) { //loop }
not sure if this is the best approach..
and thanks for giving me a layout on how to start
posted by 09 Aug 2013I don't think there is an equivalent for that in C++. I think you could do something similar with the knowledge that last char in the alpha string is '\0', however I think it is easier to just 'calculate' the required char by adding 1 to the current char.
posted by 09 Aug 2013can you fix this code?
const string teststring[] = { "a", "b", "c", "d", }; if (_ps3report->PressureCircle > 0 ) { int i = 0; printf("output: %s\r\n", teststring[i]); i++
this isnt correct, i just wanted to express this idea but not sure how to make the syntax correct in c++. when a button is pushed, it will run throught the array, starting at 0 which is A, then when button is pushed again, it will add 1 which is now equivalent to B.
posted by 10 Aug 2013Few things, you lack an '}'. Also you need to have your variable defined as global, or at least outside your if statement, now it is set to 0 everytime. Also I think best to simply use a char array. So something like:
int main() { int i = 0; const char testString[] = "abcd"; while(1) { if (_ps3report->PressureCircle > 0 ) { i++; if (i>3) i=0; printf("output: %s\r\n", teststring[i]); } } }
Hi erik, thanks for you input :) im trying to test your code, but theres one thing i cant get passed during the compilation.
im getting error
Expected a " ; "
on this line
int main() { // error number 65 int i = 0; const char testString[] "abcd";
when i try to look for it, "https://mbed.org/cookbook/Compiler-Error-65" it says, include "mbed.h", in which im very much sure is already included in my program.
also, just to let you know, i put this code at the bottom of my Ps3USB.cpp, and not in the main.cpp
posted by 10 Aug 2013You are missing an '=' in your testString declaration. And you may not have more than one main function.
I think it is easiest if you declare some of these variables as global, and then call a function if it needs to increment the letter, and just put that in your main.
Problem is for me it is hard to know exactly what your code is. Even if you publish your entire code I don't have your hardware, so it stays hard :)
posted by 10 Aug 2013