Go Down

### Topic: The ² & ³ calculator (Read 4412 times)previous topic - next topic

#### Nanopixel5

##### Dec 29, 2014, 03:19 amLast Edit: Dec 29, 2014, 11:03 am by Nanopixel5
I wrote the code to find the ²  and ³  of numbers. But it only works sometimes and sometimes it doesn't work. I am just a beginner.

#### Nanopixel5

#1
##### Dec 29, 2014, 03:23 am
Sorry,  i just started using this forum. Here's the code:

int number;
int test;
long cubed;
long squared;

void setup() {
Serial.begin(9600);
Serial.println("********************************************");
Serial.println("* -The Amazing Square and Cube Calculator- *");
Serial.print("********************************************");
Serial.println();
Serial.println("Instructions: ");
Serial.println("1) Type in the number that is to be squared and cubed in the Serial Monitor.;");
Serial.println("2) Voila, the square and cube of the number you typed will be displayed.");
delay(5000);
}

void loop() {
boolean error = false; // error is used when number cannot be used

while(Serial.available() == 0)
test = Serial.read() - '0'; // when there is something to read, save it into test

if(-1290 <= test <= 1290) { // is the number can be used
error = false;
number = test;
Serial.print("You typed '");  //display what was typed
Serial.print(number);
Serial.println("'");
}
else { //is the number can't be used
error = true;
number = test;
}

if(error == true) {
Serial.println("Make sure you typed a number between -1290 and 1290 without decimal points."); // printed if number can't be used
Serial.print("You typed '");
Serial.print(number);
Serial.println("'");
}
else if(error == false) {
squarecube();        // calculate square and cube then add 3 empty lines
Serial.println();
Serial.println();
Serial.println();
}
while(Serial.available() > 0) {  // flush all unwanted stuff
}
}

void squarecube() {
squared = number * number;    // calculate square and print
Serial.print(number);
Serial.print(" squared is ");
Serial.println(squared);

cubed = squared * number;    // calculate cube and print
Serial.print(number);
Serial.print(" cubed is ");
Serial.println(cubed);
}

#### DVDdoug

#2
##### Dec 29, 2014, 03:31 am
What happens when it "doesn't work"?

Is it repeatable?  Are there certain values that don't work?

#### Nanopixel5

#3
##### Dec 29, 2014, 03:37 amLast Edit: Dec 29, 2014, 03:41 am by Nanopixel5
Thanks for the quick reply. Sometimes when i type a 2, it says i typed a different number. I also can't use more than 1 digit, it only reads the first digit.

#### keeper63

#4
##### Dec 29, 2014, 03:44 am
What do you mean by "But it only works sometimes and sometimes it doesn't work"? Can you explain things more clearly - perhaps with an example - of when it works, and when it doesn't work?

That said, I see a few things which concern me:

1. Line 22 - Serial.read() looks for incoming bytes (http://arduino.cc/en/Serial/Read) - you then subtract a single character '0' (see http://arduino.cc/en/Reference/Char) from a byte - since char '0' is the same as the decimal value of 48 (see http://www.asciitable.com/) - your value of test is being set to whatever is read, then subtracting 48 from that - so if you input 255, test would be set to 207. That probably isn't what you want...?

3. Line 24 - this looks wonky; what you really want here is something like:

Code: [Select]
`if (test >= -1290 && test <= 1290) {`

I am certain that line 24 compiles, and from a mathematical representation is correct - I am just not certain it will do what you are expecting it to do (which with the current code you can't easily test, because of the issues outlined above). For all I know, it may work perfectly, but the syntax isn't one I am familiar with in C/C++ (though I am familiar with it from a mathematical standpoint).

Everything else looks ok, otherwise.

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

#### Nick_Pyner

#5
##### Dec 29, 2014, 03:52 am
I also can't use more than 1 digit, it only reads the first digit.
That may be just a matter of format. Try this and see if you can work on from there. This is about using bluetooth, but the essential code is the same

Code: [Select]
`/* This is a simple test for two way traffic via bluetooth    but you can try it first using the USB cable to the serial    monitor without the bluetooth module connected.      Note that some bluetooth modules come set to a different baud rate.   This means you need to alter the command                Serial.begin(9600) accordingly   Same goes for the setting in the bottom right corner on the    serial monitor */             byte byteRead;void setup(){    Serial.begin(9600);    Serial.println("OK then, you first, say something.....");    Serial.println("Go on, type something in the space and hit Send,");    Serial.println("or just hit the Enter key, then I will repeat it!");    Serial.println("");}void loop() {   //  check if data has been sent from the computer:   if (Serial.available()) {    /* read the most recent byte */    byteRead = Serial.read();    /*ECHO the value that was read, back to the serial port. */    Serial.write(byteRead);  }}`

#### Nanopixel5

#6
##### Dec 29, 2014, 03:59 amLast Edit: Dec 29, 2014, 07:00 am by Nanopixel5
When the code works, it is like this :
You typed '2'
2 squared is 4
2 cubed is 8

When it doesn't, it is like this:
You typed '-49'
-49 squared is 2401
-49 cubed is -117649

(i typed 2)

#### Nanopixel5

#7
##### Dec 29, 2014, 04:07 am
That may be just a matter of format. Try this and see if you can work on from there. This is about using bluetooth, but the essential code is the same

Code: [Select]
`/* This is a simple test for two way traffic via bluetooth    but you can try it first using the USB cable to the serial    monitor without the bluetooth module connected.      Note that some bluetooth modules come set to a different baud rate.   This means you need to alter the command                Serial.begin(9600) accordingly   Same goes for the setting in the bottom right corner on the    serial monitor */             byte byteRead;void setup(){    Serial.begin(9600);    Serial.println("OK then, you first, say something.....");    Serial.println("Go on, type something in the space and hit Send,");    Serial.println("or just hit the Enter key, then I will repeat it!");    Serial.println("");}void loop() {   //  check if data has been sent from the computer:   if (Serial.available()) {    /* read the most recent byte */    byteRead = Serial.read();    /*ECHO the value that was read, back to the serial port. */    Serial.write(byteRead);  }}`

Why did you use Serial.write? What is the difference between Serial.write and Serial.println?

#### Ps991

#8
##### Dec 29, 2014, 04:08 amLast Edit: Dec 29, 2014, 07:08 am by Ps991
Code: [Select]
`while(Serial.available() == 0);delay(10); //let the buffer fill up with the entire numbertest = Serial.parseInt();`
@cr0sh, I do not believe entering 255 in the monitor will return as 255, I think it will return 3 bytes '2','5','5'. Although I could be wrong...in which case dont bother reading this post
If you can't write your program in plain english where anyone could understand it then you have no hope of writing code for it.  -Delta_G

#### Nick_Pyner

#9
##### Dec 29, 2014, 04:16 am
When it doesn't, it is like this:
You typed '-49'
-49 squared is 2401
-49 cubed is -117649

Looks pretty kosher to me

#### Nanopixel5

#10
##### Dec 29, 2014, 09:40 am
I am trying to make a square and cube calculator, but it doesn't seem to be working. Does anybody know what is wrong with the code? Can someone help me?

This is the code:

int number;
byte incoming;
long cubed;
long squared;

void setup() {
Serial.begin(9600);
Serial.println("********************************************");
Serial.println("* -The Amazing Square and Cube Calculator- *");
Serial.print("********************************************");          //print when sketch starts first time or resets
Serial.println();
Serial.println("Instructions: ");
Serial.println("1) Type in the number that is to be squared and cubed in the Serial Monitor.;");
Serial.println("2) Voila, the square and cube of the number you typed will be displayed.");
}

void loop() {
boolean error = false; // error is used when what is typed cannot be used, it prevents it from entering the if loop(at the end) and being squared and cubed
number = 0;          //blank out number number from last time loop ran

while(Serial.available() == 0)  // do nothing when nothing is typed

while(Serial.available() > 0) {     //filter to remove anything which isn't a number

if(incoming >= '0' && incoming <= '9') {
number = (number * 10) + (incoming - '0');
delay(5);
}
else{               //if it is not a number, display error messge and break out of while loop
errorWarn();
error = true;
break;
}
}

if(error == false) {      //if it isn't a number it can't enter here
squarecube();          //calculate square and cube
}

while(Serial.available() > 0) {    //clears serial buffer so extra stuff won't be in the buffer the next time the loop runs
}

}

void squarecube() {
Serial.print("You typed '");  //display what was typed
Serial.print(number);
Serial.println("'");

squared = number * number;    // calculate square and print
Serial.print(number);
Serial.print(" squared is ");
Serial.println(squared);

cubed = squared * number;    // calculate cube and print
Serial.print(number);
Serial.print(" cubed is ");
Serial.println(cubed);

Serial.println();
Serial.println();           // add three empty lines
Serial.println();
}

void errorWarn() {
Serial.println("Make sure you typed a NUMBER WITHOUT DECIMAL POINTS."); //display error message
Serial.print("You typed '");
Serial.print(number);
Serial.println("'");
Serial.println();
}

#11
##### Dec 29, 2014, 10:04 am

@Nanopixel5, do not cross post.  Threads merged.

#### Robin2

#12
##### Dec 29, 2014, 10:17 am
Look at the examples in serial input basics for a more robust way to receive data - especially example 2 and the code for parsing numbers

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

#### AJITnayak

#13
##### Dec 29, 2014, 10:28 am
Code: [Select]
`int number;byte incoming;long cubed;long squared;void setup() {  Serial.begin(9600);}void loop(){    while(Serial.available() > 0) {     //filter to remove anything which isn't a number    incoming = Serial.read();    if(incoming >= '0' && incoming <= '9') {                  number = (number * 10) + (incoming - '0');       delay(5);          }   Serial.print("number:");Serial.println( number); squarecube();  }Serial.flush();number=0;}void squarecube() {  Serial.print("You typed '");  //display what was typed  Serial.print(number);  Serial.println("'");    squared = number * number;    // calculate square and print  Serial.print(number);  Serial.print(" squared is ");  Serial.println(squared);  cubed = squared * number;    // calculate cube and print  Serial.print(number);  Serial.print(" cubed is ");  Serial.println(cubed);  Serial.println();  Serial.println();           // add three empty lines  Serial.println();}void errorWarn() {  Serial.println("Make sure you typed a NUMBER WITHOUT DECIMAL POINTS."); //display error message  Serial.print("You typed '");  Serial.print(number);  Serial.println("'");  Serial.println("Please type the number again.");  Serial.println();}`

Check output it must work
AMPS

#### Ps991

#14
##### Jan 06, 2015, 06:15 amLast Edit: Jan 06, 2015, 06:16 am by Ps991
I feel like nobody read my post...This should work just fine, keyword "should"

Code: [Select]
`while(Serial.available() <= 0);delay(1); //let the buffer fill up with the entire numbertest = Serial.parseInt();`
If you can't write your program in plain english where anyone could understand it then you have no hope of writing code for it.  -Delta_G

Go Up