Show Posts
Pages: [1] 2 3
1  Using Arduino / Programming Questions / Re: Bin Dec Hex Oct Converter on: December 20, 2012, 05:41:42 pm
Quote
You decided to ask the same question again, huh?

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.


Yeah, I used your sample with some modifications but the program did not work. So I thought i'd ask again to see if anyone else new. Forgot the house rules.

Apologies Nick
2  Using Arduino / Programming Questions / Re: Bin Dec Hex Oct Converter on: December 20, 2012, 05:39:36 pm
Quote
That's an interesting question.
What if you entered 011?
Would you interpret as 3 or 9?

Sorry, I wasnt clear enough. I was using 012 as an example. I want the user to input anything as long as it is read an octal value.
3  Using Arduino / Programming Questions / Urgent help! Reading Serial Input as Binary and Oct on: December 20, 2012, 04:17:45 pm
Hi, does anyone know a simple way to read serial input as binary.

To clarify instead of taking an input say number 1 (ascii 49) and reading it as its binary format i want a user to be able to input a binary value say 00101011 in binary format.

Ive tried using byte but when using the serialRead function, quite simply nothing works so ive gone back to the drawing board to see if anyone has a simple solution.

Also how could i take serial read as an octal value, so the user could put in 012 (Dec = 10) and treat it as octal.

Many thanks.
4  Using Arduino / Programming Questions / Re: Bin Dec Hex Oct Converter on: December 15, 2012, 06:24:56 pm
Quote
If I read the code correctly you are assuming one or two digit input. What if someone enters 0004?

You are best off:

    Collect the input into a buffer, delimited by something (eg. newline)
    Do a conversion on that buffer


My link above shows exactly how to do that for decimal numbers, so it shouldn't take weeks to do.

Apologies for my lack of knowledge (as a beginner)-Ive looked at your link but i dont understand exactly what bit (excuse the pun) I should be looking at. I dont exactly understand it all and how i can apply this to my code.

Thanks.
5  Using Arduino / Programming Questions / Re: Bin Dec Hex Oct Converter on: December 15, 2012, 06:20:20 pm
Quote
What are you inputting? What is being output? You don't expect us to guess, do you?

Im trying to take input from the serial monitor and convert it between the four formats of Hex, Bin, Dec, Oct. The user makes the choice of what conversion fromat they want to use by choosing from the menu.

I have come across mathematical type conversions in the past but cant find anymore. Like if using the serial.read, as you'll know serial input is ascii so to read it as decimal val = val - '0'. I was wondering if you could do this simple code for all formats after the serial.read function.

Code:
void setup() {
 //Initialize serial and wait for port to open:
  Serial.begin(9600);
 }

void loop()
 {
 int menuselect;
 int conversion1;
 int conversion2;
 int conversion3;
 int conversion4;
 int conversion5;
 int conversion6;
 int conversion7;
 int conversion8;
 int conversion9;
 int conversion10;
 int conversion11;
 int conversion12;
     
   { // prints text and menu options for user to select from
     Serial.println(F(" "));
     Serial.println(F("Welcome to my Bin, Dec, Oct, Hex Converter"));
     delay(500);
     Serial.println(F(" "));
     Serial.println(F("Please select a number from the options available..."));
     Serial.println(F("*Please Note* The options below are Case Sensitive!"));
     Serial.println(F(" "));
     Serial.println(F("Main Menu"));
     Serial.println(F("---------"));
     Serial.println(F("a) 2's comp Binary to Hex,"));
     Serial.println(F("b) 2's comp Binary to Octal,"));
     Serial.println(F("c) 2's comp Binary to Decimal,"));
     Serial.println(F("d) Octal to 2's comp Binary,"));
     Serial.println(F("e) Octal to Hex,"));
     Serial.println(F("f) Octal to Decimal,"));
     Serial.println(F("g) Hex to 2's comp Binary,"));
     Serial.println(F("h) Hex to Octal,"));
     Serial.println(F("i) Hex to Decimal,"));
     Serial.println(F("j) Decimal to 2's comp Binary,"));
     Serial.println(F("k) Decimal to Hex,"));
     Serial.println(F("l) Decimal to Octal."));   
   
      while (Serial.available ()== 0){}  //Constantly checks to see if anything has been sent over the USB Connection and if it needs to be processed
        {
          menuselect = Serial.read();
          switch (menuselect)
          {
            case 'a':   //option a) ASCII "a"
              Serial.println(F(" "));
              Serial.println(F("2's comp Binary to Hex"));
                while (Serial.available ()== 0){}
                conversion1 = Serial.read();
                Serial.println(conversion1);
                Serial.println(conversion1, HEX);
                delay(2000);     
            break;
            case 'b': 
              Serial.println(F(" ")); 
              Serial.println(F("2's comp Binary to Octal"));
                while (Serial.available ()== 0){}
                conversion2 = Serial.read();
                Serial.println(conversion2);
                Serial.println(conversion2, OCT);
                delay(2000);
            break;
            case 'c':   
              Serial.println(F(" "));
              Serial.println(F("2's comp Binary to Decimal"));
                while (Serial.available ()== 0){}
                conversion3 = Serial.read();
                Serial.println(conversion3);
                Serial.println(conversion3, DEC);
                delay(2000);
            break;
            case 'd':   
              Serial.println(F(" "));
              Serial.println(F("Octal to 2's comp Binary"));
                while (Serial.available ()== 0){}
                conversion4 = Serial.read();
                Serial.println(conversion4);
                Serial.println(conversion4, BIN);
                delay(2000);
            break;
            case 'e':   
              Serial.println(F(" "));
              Serial.println(F("Octal to Hex"));
                while (Serial.available ()== 0){}
                conversion5 = Serial.read();
                Serial.println(conversion5);
                Serial.println(conversion5, HEX);
                delay(2000);
            break;
            case 'f':
              Serial.println(F(" "));
              Serial.println(F("Octal to Decimal"));
                while (Serial.available ()== 0){}
                conversion6 = Serial.read();
                Serial.println(conversion6);
                Serial.println(conversion6, DEC);
                delay(2000);
            break;
            case 'g':   
              Serial.println(F(" "));
              Serial.println(F("Hex to 2's comp Binary"));
                while (Serial.available ()== 0){}
                conversion7 = Serial.read();
                Serial.println(conversion7);
                Serial.println(conversion7, BIN);
                delay(2000);
            break;
            case 'h':
              Serial.println(F(" "));
              Serial.println(F("Hex to Octal"));
                while (Serial.available ()== 0){}
                conversion8 = Serial.read();
                Serial.println(conversion8);
                Serial.println(conversion8, OCT);
                delay(2000);
            break;
            case 'i':   
              Serial.println(F(" "));
              Serial.println(F("Hex to Decimal"));
                while (Serial.available ()== 0){}
                conversion9 = Serial.read();
                Serial.println(conversion9);
                Serial.println(conversion9, DEC);
                delay(2000);
            break;
            case 'j':   
              Serial.println(F(" "));
              Serial.println(F("Decimal to 2's comp Binary"));
                while (Serial.available ()== 0){}
                conversion10 = Serial.parseInt();
                Serial.println(conversion10); //Serial.println(" Decimal");
                Serial.println(conversion10, BIN); //Serial.println(" Binary");
                delay(2000);
            break;
            case 'k':
              Serial.println(F(" ")); 
              Serial.println(F("Decimal to Hex"));
                while (Serial.available ()== 0){}
                conversion11 = Serial.parseInt();
                Serial.println(conversion11);
                Serial.println(conversion11, HEX);
                delay(2000);
            break;
            case 'l': 
              Serial.println(F(" ")); 
              Serial.println(F("Decimal to Octal"));
                while (Serial.available ()== 0){}
                conversion12 = Serial.parseInt();
                 //could have used conversion12 = conversion12 - '0'; when using Serial.read
                Serial.println(conversion12);
                Serial.println(conversion12, OCT);
                delay(2000);
            break;
          }
        }
   }
 }
6  Using Arduino / Programming Questions / Re: Bin Dec Hex Oct Converter on: December 15, 2012, 04:41:16 pm
Quote
Easier to read, don't you think? Ditto for the others.
That seems so obvious now, lol.

Quote
The rest of the code looks incredibly repetitive. You should be able to collect their input into a buffer in one place and then process it afterwards.

Completely agree but at the moment i'm more concerned that my conversions dont work except the last three. This needs to be completed by Friday, so if you can help me in anyway with the conversions that would be great. I plan on doing the "buffer" thing last.

Many thanks.
7  Using Arduino / Programming Questions / Re: Serial Monitor Menu on: December 15, 2012, 06:32:51 am
Ive just used the switch case for the users input and that works fine thanks.
8  Using Arduino / Programming Questions / Re: Bin Dec Hex Oct Converter on: December 15, 2012, 06:32:12 am
Hi,

Below is the code for my program, it takes the user input when making a choice from the menu and then takes the user to convert between the specified types.

A couple of questions. Ive used the parseInt for the dec to other ypes but how can i make sure the result for example in binary is 8-bit. This is because i need the user to be able to put in a number between -255 and 255. The minus returns the binary in 32-bit, I need it to be 8-bit.

Also, is there any calculations (or anyway in fact-without using a similar thing to isxdigit) that can ensure the user input is eith HEX, OCT or binary?

Many thanks once again.

Code:
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(500);
  Serial.println(F(" "));
  Serial.println(F("Please select a number from the options available..."));
  Serial.println(F("*Please Note* The options below are Case Sensitive!"));
  Serial.println(F(" "));
  Serial.println(F("Main Menu"));
  Serial.println(F("---------"));
  Serial.println(F("a) 2's comp Binary to Hex,"));
  Serial.println(F("b) 2's comp Binary to Octal,"));
  Serial.println(F("c) 2's comp Binary to Decimal,"));
  Serial.println(F("d) Octal to 2's comp Binary,"));
  Serial.println(F("e) Octal to Hex,"));
  Serial.println(F("f) Octal to Decimal,"));
  Serial.println(F("g) Hex to 2's comp Binary,"));
  Serial.println(F("h) Hex to Octal,"));
  Serial.println(F("i) Hex to Decimal,"));
  Serial.println(F("j) Decimal to 2's comp Binary,"));
  Serial.println(F("k) Decimal to Hex,"));
  Serial.println(F("l) Decimal to Octal."));
}

void loop()
 {
   int menuselect;
   int conversion1;
   int conversion2;
   int conversion3;
   int conversion4;
   int conversion5;
   int conversion6;
   int conversion7;
   int conversion8;
   int conversion9;
   int conversion10;
   int conversion11;
   int conversion12;
     
   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();
      switch (menuselect) {
  case 97:   //option a) ASCII "a"
    Serial.println(F(" "));
    Serial.println(F("2's comp Binary to Hex"));
       while (Serial.available ()== 0){}
       conversion1 = Serial.read();
       Serial.println(conversion1, HEX);
             
    break;
  case 98: 
    Serial.println(F(" ")); 
    Serial.println(F("2's comp Binary to Octal"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       Serial.println(conversion2, OCT);
       
    break;
  case 99:   
    Serial.println(F(" "));
    Serial.println(F("2's comp Binary to Decimal"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       conversion2 = conversion2 - '0';
       Serial.println(conversion2, DEC);
       
    break;
  case 100:   
    Serial.println(F(" "));
    Serial.println(F("Octal to 2's comp Binary"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       Serial.println(conversion2, BIN);
       
    break;
  case 101:   
    Serial.println(F(" "));
    Serial.println(F("Octal to Hex"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       Serial.println(conversion2, HEX);
       
    break;
  case 102:
    Serial.println(F(" "));
    Serial.println(F("Octal to Decimal"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       Serial.println(conversion2, DEC);
       
    break;
  case 103:   
    Serial.println(F(" "));
    Serial.println(F("Hex to 2's comp Binary"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       Serial.println(conversion2, BIN);
       
    break;
  case 104:
    Serial.println(F(" "));
    Serial.println(F("Hex to Octal"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       Serial.println(conversion2, OCT);
       
    break;
  case 105:   
    Serial.println(F(" "));
    Serial.println(F("Hex to Decimal"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       Serial.println(conversion2, DEC);
       
    break;
  case 106:   
    Serial.println(F(" "));
    Serial.println(F("Decimal to 2's comp Binary"));
    while (Serial.available ()== 0){}
       conversion2 = Serial.parseInt();
       Serial.println(conversion2, BIN);
       
    break;
  case 107:
    Serial.println(F(" ")); 
    Serial.println(F("Decimal to Hex"));
    while (Serial.available ()== 0){}
       conversion2= Serial.parseInt();
       Serial.println(conversion2, HEX);
       
    break;
  case 108: 
    Serial.println(F(" ")); 
    Serial.println(F("Decimal to Octal"));
    while (Serial.available ()== 0){}
       conversion2= Serial.parseInt();
       //if using serial.read i could of used conversion2 = conversion2 - '0'
       Serial.println(conversion2, OCT);
       
    break;
  } }}
     
     
9  Using Arduino / Programming Questions / Re: Serial Monitor Menu on: December 15, 2012, 06:26:54 am
Brilliant thanks.
10  Using Arduino / Programming Questions / Re: Bin Dec Hex Oct Converter on: December 14, 2012, 06:05:11 am
Many thanks for both I your replies.

As I'm a beginner I have never herd of isdigit function etc. I will do some digging on this.
11  Using Arduino / Programming Questions / Re: Serial Monitor Menu on: December 14, 2012, 06:03:41 am
Ok. I will read up on this.

Thanks again.
12  Using Arduino / Programming Questions / Re: Serial Monitor Menu on: December 14, 2012, 04:55:54 am
Hi,

Below is refreshed code.

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.
13  Using Arduino / Programming Questions / Bin Dec Hex Oct Converter on: December 14, 2012, 04:53:16 am
Hi,

Below is a quick and simple program that should convert a decimal value into binary. At the moment it converts any value to Bin.

A few questions...

1) How can I ensure that the user input in the serial monitor is a decimal value?

2) If I wanted to convert from Hex to Bin, how would i do this and again, how would I ensure that the user input was Hex?

Many Thanks

Code:
int val;

void setup() {
 //Initialize serial and wait for port to open:
  Serial.begin(9600);
   // prints text and menu options for user to select from

void loop() {
   if (Serial.available())  //Constantly checks to see if anything has been sent over the USB Connection and if it needs to be processed
   {
     val = Serial.read(); //Reads a single letter
     //Serial.println(val);
     Serial.print(val, BIN); 
  }}
14  Using Arduino / Programming Questions / Re: Serial Monitor Menu on: December 13, 2012, 09:58:01 am
As simple as that. Thanks.
15  Using Arduino / Programming Questions / Re: Serial Monitor Menu 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?
Pages: [1] 2 3