Go Down

Topic: ASCII characters in the Serial Monitor (Read 280 times) previous topic - next topic

audiojulie

Hello, it's me again. Sorry for making multiple posts so close together.  :smiley-confuse:

For an assignment in my introductory microprocessors class, I have to create a program that reads a phrase typed into the serial monitor and prints how many vowels are in said string.

I've seen a lot of examples online about how to do this with integers, but I can't seem to figure out how to make it work with letters.

Please let me know if you have any ideas. Thank you!

Code: [Select]
/* HW 3: Vowel Count
 * Adapted from https://www.arduino.cc/en/Serial/Read
 */

char input = 0;
int count = 0;
int countA = 0;
int countE = 0;
int countI = 0;
int countO = 0;
int countU = 0;
int counta = 0;
int counte = 0;
int counti = 0;
int counto = 0;
int countu = 0;


void setup()

{
       
 Serial.begin( 9600 );

}

void loop()
{
       count = 0;
        while ( Serial.available() == 0 )
        {
          input = Serial.read();
          delay ( 3000 );
          Serial.print (input);
          Serial.print (" ");

          if ( input = 65 )
         {
          count = count++;
         }
          if ( input = 69 )
         {
          count = count++;
         }
          if ( input = 73 )
         {
          count = count++;
         }
          if ( input = 79 )
         {
          count = count++;
         }
          if ( input = 85 )
         {
          count = count++;
         }
           if ( input = 97 )
         {
          count = count++;
         }
           if ( input = 101 )
         {
          count = count++;
         }
          if ( input = 105 )
         {
          count = count++;
         }
           if ( input = 111 )
         {
          count = count++;
         }
          if ( input = 117 )
         {
          count = count++;
         }

       
        }                                                                           
      if (count > 0)
      {
      Serial.print (count);
      Serial.println (" vowel(s)");
      delay (200);
      }
     
}

Paul_KD7HB

Well, first of all, you have a "phrase" of unknown length coming to the Serial port, yet you allow for only a single character to be moved from the buffer to your 'input". You must have storage for the entire phrase.

Then you wait 3 seconds for what?

Paul

audiojulie

Hi,

Thanks for getting back to me.

Do you mean that I need a string to read it? I thought that might be the case, but we had not talked about that in class yet when this was assigned.

The delay was left over from previous troubleshooting, sorry.

Paul_KD7HB

Hi,

Thanks for getting back to me.

Do you mean that I need a string to read it? I thought that might be the case, but we had not talked about that in class yet when this was assigned.

The delay was left over from previous troubleshooting, sorry.
I suspected that was the case for the delay.

Please review the tutorial on reading serial data : http://forum.arduino.cc/index.php?topic=396450.

That should help a bit.

Paul

westfw

hint 1:  watch your "=" vs "==" !
hint 2: in C, a single ascii character inside single quotes has an integer value equal to the ascii value of the character.
So "if (input == 97)" can be written as "if (input == 'a')"

J-M-L

#5
Dec 07, 2017, 12:57 am Last Edit: Dec 07, 2017, 01:04 am by J-M-L
If you need to print how many vowels you have you only need 1 count, and just check any incoming char against the vowels (small caps or not - what about Y?). Print the result when you receive '\n' for example

What are those for then
Code: [Select]
int countA = 0;
int countE = 0;
int countI = 0;
int countO = 0;
int countU = 0;
int counta = 0;
int counte = 0;
int counti = 0;
int counto = 0;
int countu = 0;


This - in plain English -
Code: [Select]
while ( Serial.available() == 0 )
        {
          input = Serial.read();
would read as « while I don't have anything to read, try to read it anyway ».... how successful do you think that is likely to be?

(plus the = versus == etc)
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

wvmarle

#6
Dec 07, 2017, 06:38 am Last Edit: Dec 07, 2017, 09:25 am by wvmarle
Code: [Select]

char input = 0;
int count = 0;
int countA = 0;
int countE = 0;
int countI = 0;
int countO = 0;
int countU = 0;
int counta = 0;
int counte = 0;
int counti = 0;
int counto = 0;
int countu = 0;
You define all of them, but never use them.

Quote
Code: [Select]

void loop()
{
       count = 0;
        while ( Serial.available() == 0 )
        {
          input = Serial.read();
          delay ( 3000 );
          Serial.print (input);
          Serial.print (" ");

          if ( input = 65 )
         {
Three errors.
1) you reset count to 0 every time loop runs. So it can never go past 1.
2) you only run this when there's nothing to read. Replace the while line with:
Code: [Select]

if (Serial.available()) {

This will run the block when you have something on the Serial buffer: read the character and process it.

Quote
Code: [Select]

          if ( input = 65 )
And this one of course.

If you don't care about which vowel but just want to count them all you can do this in a single if statement:
Code: [Select]

if (input == 'a' || input == 'e' || input == 'i') { // fill in the rest
  count++;
}
if (input == '\n') { // newline character terminates the input.
  Serial.print("Vowels counted in this input: ");
  Serial.println(count);
  count = 0;
}
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

J-M-L

Never put a delay() when you deal with Serial input - don't second guess asynchronous behavior with synchronous code

Would suggest you read  Serial Input Basics
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

Power_Broker

Just a quick note: an ASCII coded number minus ASCII number '0' is the number in decimal.
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Go Up