Offline
Newbie
Karma: 1
Posts: 33
|
 |
« on: December 12, 2012, 05:54:23 pm » |
Hi, Before we go any further I am a beginner and still finding my feet. Below is the code i have written to create a simple menu in the serial monitor where i want the user to make choices etc. Firstly, the int's at the beginning...is this the best way to go when creating a menu or should i be using #define. As i'm a beginner i'm not even sure if this is te correct thing to do. Secondly, now that i have printed all the text i need to, to the serial monitor, how do i write an if statement in the void loop that takes the user input from the serial monitor and takes them to where they want to be. How could i test this is working? Logically if they select option one I want to perhaps print something in the serial monitor to confirm they are where they want to be. I will get onto the actual conversions later. I hope that makes sense. Many thanks. int menuoption1 = 1; int menuoption2 = 2; int menuoption3 = 3; int menuoption4 = 4; int menuoption5 = 5; int menuoption6 = 6; int menuoption7 = 7; int menuoption8 = 8; int menuoption9 = 9; int menuoption10 = 10; int menuoption11 = 11; int menuoption12 = 12;
void setup() { //Initialize serial and wait for port to open: Serial.begin(9600); // prints text and menu options for user to select from Serial.println("Welcome to my Bin, Dec, Oct, Hex Converter"); delay(2000); Serial.println(" "); Serial.println("Please select a number from the options available"); delay(2000); Serial.println(" "); Serial.println("Main Menu"); Serial.println("---------"); delay(1000); Serial.println("1. 2's comp Binary to Hex,"); delay(1000); Serial.println("2. 2's comp Binary to Octal,"); delay(1000); Serial.println("3. 2's comp Binary to Decimal,"); delay(1000); Serial.println("4. Octal to 2's comp Binary,"); delay(1000); Serial.println("5. Octal to Hex,"); delay(1000); Serial.println("6. Octal to Decimal,"); delay(1000); Serial.println("7. Hex to 2's comp Binary,"); delay(1000); Serial.println("8. Hex to Octal,"); delay(1000); Serial.println("9. Hex to Decimal,"); delay(1000); Serial.println("10. Decimal to 2's comp Binary,"); delay(1000); Serial.println("11. Decimal to Hex,"); delay(1000); Serial.println("12. Decimal to Octal."); }
void loop() { }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 33
|
 |
« Reply #1 on: December 13, 2012, 06:41:21 am » |
Bump
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #2 on: December 13, 2012, 06:59:14 am » |
Serial.println("1. 2's comp Binary to Hex,"); This is quite wasteful of precious RAM. Better would be: Serial.println(F("1. 2's comp Binary to Hex,"));
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Tasmania - Australia
Offline
Sr. Member
Karma: 7
Posts: 251
|
 |
« Reply #3 on: December 13, 2012, 07:01:54 am » |
delay(1000);
This is wasteful of precious TIME Better would be 
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
God Member
Karma: 37
Posts: 974
Get Bitlash: http://bitlash.net
|
 |
« Reply #4 on: December 13, 2012, 07:02:56 am » |
For user input, read up on Serial.available() and Serial.read().
-br
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 33
|
 |
« Reply #5 on: December 13, 2012, 07:04:48 am » |
Thanks for pointing these issues out. Excuse my ignorance but what is the 'F' in your Serial.println bit of code? Basically why does that save RAM?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: December 13, 2012, 08:07:07 am » |
The "F" macro puts your strings in flash memory, and more importantly, keeps them there.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 33
|
 |
« Reply #7 on: December 13, 2012, 09:58:01 am » |
As simple as that. Thanks.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 33
|
 |
« Reply #8 on: December 14, 2012, 04:55:54 am » |
Hi, Below is refreshed code. int menuselect;
void setup() { //Initialize serial and wait for port to open: Serial.begin(9600); // prints text and menu options for user to select from Serial.println(F("Welcome to my Bin, Dec, Oct, Hex Converter")); delay(1000); Serial.println(F(" ")); Serial.println(F("Please select a number from the options available")); Serial.println(F(" ")); Serial.println(F("Main Menu")); Serial.println(F("---------")); Serial.println(F("1. 2's comp Binary to Hex,")); Serial.println(F("2. 2's comp Binary to Octal,")); Serial.println(F("3. 2's comp Binary to Decimal,")); Serial.println(F("4. Octal to 2's comp Binary,")); Serial.println(F("5. Octal to Hex,")); Serial.println(F("6. Octal to Decimal,")); Serial.println(F("7. Hex to 2's comp Binary,")); Serial.println(F("8. Hex to Octal,")); Serial.println(F("9. Hex to Decimal,")); Serial.println(F("10. Decimal to 2's comp Binary,")); Serial.println(F("11. Decimal to Hex,")); Serial.println(F("12. Decimal to Octal.")); }
void loop() { char ch; if (Serial.available()) //Constantly checks to see if anything has been sent over the USB Connection and if it needs to be processed { menuselect = Serial.read(); //Reads a single letter if (menuselect = '1') { Serial.flush();//Try to clear the serial monitor and then go to conversion delay(1000); Serial.println("2's comp Binary to Hex"); }}} I havent tested this yet, but if a user selects an option from the menu, i then need to clear the serial monitor and make it look to the user they are in that option. How do I do that? Thanks in advance.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #9 on: December 14, 2012, 05:03:19 am » |
i then need to clear the serial monitor If you're using the serial monitor from IDE, you can't, but you could use a terminal emulator like PuTTY, and use a VT52 (or similar) clear screen escape sequence to do all kinds of fancy stuff.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 33
|
 |
« Reply #10 on: December 14, 2012, 06:03:41 am » |
Ok. I will read up on this.
Thanks again.
|
|
|
|
|
Logged
|
|
|
|
|
Tasmania - Australia
Offline
Sr. Member
Karma: 7
Posts: 251
|
 |
« Reply #11 on: December 14, 2012, 07:47:53 am » |
but you could use a terminal emulator like PuTTY, and use a VT52 Gosh that reminds me so much of things back in the '80's, using VT52 terminals from DEC. Paul
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 15
Posts: 463
|
 |
« Reply #12 on: December 14, 2012, 09:49:53 am » |
Below is the code i have written to create a simple menu in the serial monitor where i want the user to make choices etc.
If you like, take a look at the attached ZIP file. It's the source code from a project I recently did. It's a valve controller that has a lot of menu items and selections. You may get some ideas from it. Hope this helps you.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 33
|
 |
« Reply #13 on: December 15, 2012, 06:26:54 am » |
Brilliant thanks.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 33
|
 |
« Reply #14 on: December 15, 2012, 06:32:51 am » |
Ive just used the switch case for the users input and that works fine thanks.
|
|
|
|
|
Logged
|
|
|
|
|
|