ASCII characters in the Serial Monitor

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

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!

 /* 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);
      }
      
}

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

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.

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.

I suspected that was the case for the delay.

Please review the tutorial on reading serial data : Serial Input Basics - updated - Introductory Tutorials - Arduino Forum.

That should help a bit.

Paul

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')"

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

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 -

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)

audiojulie:

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.

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:
if (Serial.available()) {

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

          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:

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;
}

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

Just a quick note: an ASCII coded number minus ASCII number ‘0’ is the number in decimal.