Understanding the language, error messages, etc.
by wwtom » Sun Jul 12, 2015 11:21 am
Hey,
im trying to build a little Calculator for myself.
Today I saw, that there is a gb.keyboard function. This really helps me a lot, but I ask myself, if I can use this function to make an only number keyboard, or atleast how can I convert the char, given by the keyboard to a int/byte/something else, to calculate with.
Tom
-
wwtom
-
- Posts: 16
- Joined: Tue Jun 02, 2015 6:28 pm
by Valden » Sun Jul 12, 2015 12:56 pm
[EDIT] Corrected the error in the code that made the string-to-int an infinite loop. Also, for some reason ,the code for '0' is 48 and not 40A 'char' is simply a number between -127 and 127. Each positive number has a character associated (hence the name of the variable). That is called the ASCII system. So for example, the number 48 correspond to the '0' character. Converting a character in ASCII to its number is simply done with a subtraction. In order to get your number, you only have to do
- Code: Select all
char your_digit = '5';
int number = your_digit - 48;
// OR you can do it like this
int number = your_digit - '0'; //because '0' = 48
This only works for 1 digit. If you have a string containing your number, you'll have to do something like this
- Code: Select all
char number[] = "1234";
int final_number = 0;
int i = 0; //(position in the string)
while (number[i] != '\0') //while we aren't at the end of the string ('\0' is the end of string character)
{
final_number *= 10; //shift all the digits in the number by one rank
final_number += number[i]-'0'; // Add the current digit
i ++;
}
(I don't have tested this code. It should work in theory)
You have to be careful to not have character other than numbers, or it will produce some strange results.
Last edited by
Valden on Sun Jul 12, 2015 3:07 pm, edited 1 time in total.
-
Valden
-
- Posts: 31
- Joined: Mon Jun 08, 2015 5:33 pm
by wwtom » Sun Jul 12, 2015 2:33 pm
Sadly this does not work. There is no result or the result cant be displayed using printin.
-
wwtom
-
- Posts: 16
- Joined: Tue Jun 02, 2015 6:28 pm
by Valden » Sun Jul 12, 2015 3:04 pm
It's because I'm dumb and i added a ';' at the end if the while(), effectively creating a infinite-loop.
This code works :
- Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;
void setup(){
gb.begin();
gb.titleScreen(F("TEST"));
}
void loop(){
if(gb.update()){
char number[] = "01234";
long final_number = 0;
long i = 0; //(position in the string)
while (number[i] != '\0') //while we aren't at the end of the string ('\0' is the end of string character)
{
final_number *= 10; //shift all the digits in the number by one rank
final_number += ((int) number[i] ) - 48; // Add the current digit
i ++;
}
gb.display.print(final_number);
}
}
-
Valden
-
- Posts: 31
- Joined: Mon Jun 08, 2015 5:33 pm
by wwtom » Sun Jul 12, 2015 3:43 pm
Now it works well! Thank you
-
wwtom
-
- Posts: 16
- Joined: Tue Jun 02, 2015 6:28 pm
by wwtom » Sun Jul 12, 2015 7:41 pm
Now I'm at the next problem:
- Code: Select all
void setup{
... Setup things
}
//Select the right variable to change/the calculator button
void loop{The Menu thing copied of the reference page.
case 0:
number();
break;
....
case 7:
calculate();
break;
}
//Open the keyboard and get the number to calculate with
void number(){
gb.Keyboard(Var., Number);
}
void calculate(){
while(Some things)
//Some operations done 1 time
......
while(1){
if(gb.update()){
Display the result
}
}
}
//99% simplified
Everything works well, but I need to delete the variables everytime I want to change only 1, or just want to go back to the menu.
So is there way to go back to the void loop thing from the void calculate and display thing?
I already tried to simply go back to title screen by pressing a button, but when I press a to get to the menu again I instant get to the result display.
And whats the name of this void calculate thing outside the loop? I dont know much about it, but it is really great.
Before this I programmed everything in the loop and this is really confusing.
Are there some more tricks?
-
wwtom
-
- Posts: 16
- Joined: Tue Jun 02, 2015 6:28 pm
by wwtom » Thu Jul 16, 2015 3:02 pm
I fixxed my old mistake and got a new one.
I first want to tell the Gamebuino the number of points in a three dimensional room and then I want him to ask me for 3 coordinates (x, y and z) per point.
- Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;
char aSt[10] = "Anzahl S";
int aS = 0;
char Eingabe[10] = "Write";
int x[10];
int y[10];
int z[10];
int i = 0;
int final_number = 0;
void setup(){
gb.begin();
gb.titleScreen(F("WOOW"));
AnzahlSP();
SpwnC();
}
void loop(){
if(gb.update()){
gb.display.print(x[0]);
}
}
void AnzahlSP(){ //Get Anzahl Spawner
gb.keyboard(aSt, 10); //aS = Anzahl Spawner
//aSt umrechnen
while (aSt[i] != '\0') //while we aren't at the end of the string ('\0' is the end of string character)
{
final_number *= 10; //shift all the digits in the number by one rank
final_number += ((int) aSt[i] ) - 48; // Add the current digit
i ++;
}
aS = final_number;
i = 0;
final_number = 0;
}
void SpwnC(){
for(int abar = 0; abar < aS; abar ++){
for(int xyz = 0; xyz < 3; xyz ++){
gb.keyboard(Eingabe, 10);
while(Eingabe[i] != '\0'){
final_number *= 10;
final_number += ((int) Eingabe[i] ) - 48;
i ++;
}
if(xyz = 0){
x[abar] = final_number;
}
if(xyz = 1){
y[abar] = final_number;
}
if(xyz = 2){
z[abar] = final_number;
}
final_number = 0;
i = 0;
}
}
}
For some reason the program asks me only one coordinate per point. And the coordinate isnt saved in the right variable.
And an other Question.
Is there a way to check a if-thing in a loop and do something if every loop-run the if-thing is true?
Like:
- Code: Select all
for(int abar; abar < aS; abar ++){
if(x[abar] < 10){
?????
}
}
So it should only do something when every number in the array "x" is smaller than 10.
-
wwtom
-
- Posts: 16
- Joined: Tue Jun 02, 2015 6:28 pm
Return to Programming Questions
Who is online
Users browsing this forum: No registered users and 2 guests