Serial monitor spits out random characters

Hello, this is my first forum post so I'm not 100% sure if I'm doing this right but heres my problem :

My creation
I coded my arduino Uno to be able to control a little 5v fan using the serial monitor and the inputs I give the serial monitor.

How it works(code)
So the code is setup to that when I type "left" or "right" the direction of the fan changes, when I type the command "switch" it switches the inputs from the serial monitor to take in numbers as you can see in the code below. When the input switches to numbers I can type a number from 0 - 255 to change the speed of the fan or I can type "300"(random number I chose) to change back the letters to switch the direction.

Problem
That is how it is supposed to work but when I type in anything it spits out random characters all random and different lengths. when I typed "left" this is what the serial monitor said :

��;{��?�O?}��������������V��^:��z�i�[^?\�x5�����\�/���=۸�yڿ��j�w?�ϴ~���M�}}�:���~/

I dont even know if you can see the random characters I just pasted because they are very odd but it doesnt seem normal, I have tried this project with 4 different arduinos to see if it was the arduino, but its not.
**Anyways here is the code **

String answer;
int measurement = 1;
int speed = 255;

void setup() {
  pinMode(12,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(8,OUTPUT);
  Serial.begin(9600);
  Serial.println("ready");
}

void loop() {
  while(Serial.available()== 0){
    
  }
  if (measurement == 1){
    answer = Serial.readString();
  }
  if (measurement == 2){
    speed = Serial.parseInt();
  }
  
  
  if (answer == "left"){
    digitalWrite(12,HIGH);
    digitalWrite(8,LOW);
    Serial.println("left");
  }
  if (answer == "right"){
    digitalWrite(12,LOW);
    digitalWrite(8,HIGH);
    Serial.println("right");
  }
  if (answer == "switch"){
    measurement = 2;
    Serial.println("switch to numbers");
  }
  if (speed == 300) {
    measurement = 1;
    speed = 255;
    Serial.println("switch to letters");

  }
  else (speed <= 255 and speed >= 0);{
    analogWrite(9,speed);
    Serial.println("speed = " + speed);
  }
  
}

Thanks for taking the time to try to help me!

Is the baud in your Monitor also 9600?

Your serial monitor is set for 9600 ?

yes it is 9600

Maybe add a delay after the Serial.begin()? The garble might be the immediate Serial.print().

it still doesnt work.

Hmm. Your code sends the "ready" (no pins attached) for me... usually garbled async messages are (1) baud mismatch or (2) missing ground, but you are using the monitor... Maybe unplug all your wires, just let the board be connected via USB...

OMG it worked!!! thanks so much for your time, I would never have thought of that. Thank you so much! have a nice day!

Baud rate radically off.

this ought to get her done:

#include "Arduino.h"

#define minDuty 0
#define maxDuty 255
String answer;
uint8_t measurement = 1;
uint8_t speed = 255;


void setup() {
  pinMode(12,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(8,OUTPUT);
  Serial.begin(9600);
  Serial.println("ready");
}

void loop() {


	if(measurement==1)			//text prompts
		Serial.println("enter direction (left or right or switch)");
	else if(measurement==2)
		Serial.println("enter speed (0-255) or switch");

	while(Serial.available()== 0)
	{  }
	answer = Serial.readString();		//read the value
	//Serial.println(answer);				//print the value (debug)
	speed = answer.toInt();				//convert to int in case its a speed value.

	if(answer!="switch")
	{
		if(measurement == 1)		//directional case
		{
		  if(answer == "left")
		  {
				digitalWrite(12,HIGH);
				digitalWrite(8,LOW);
				Serial.println("new direction: left");
		  }
		  if(answer == "right")
		  {
				digitalWrite(12,LOW);
				digitalWrite(8,HIGH);
				Serial.println("new direction: right");
		  }
		}

		else if (measurement == 2)	//speed change case
		{
			Serial.println("test");

			Serial.print("new speed: ");
			Serial.println(speed);
			analogWrite(9,speed);		//qualify new speed & write

		}
	}

  else if(answer == "switch")		//switch between numbers & directions
  {
	  if(measurement== 1)	measurement=2;
	  else if(measurement==2) measurement=1;
	  answer = "NULL";		//anything other than "switch"
  }
}

couple notes: dont use signed variables when you dont need to. by using a uint8_t (unsigned int), you save yourself having to check to see if its between 0-255, its always will be.

Serial.println("speed = " + speed);

I dont think you can do string addition inside a serial print, at least I couldn't, had to separate it into two lines. If you add "Streaming.h" to your projects, you can use the more native C method:

serial << "hello world" << int1 << int2 << char1 << char2 << endl;
vs
serial.print("hello world");
serial.print(int1);
serial.print(int2);
serial.print(char1);
serial.println(char2);

not sure what you did to fix it, but I compiled your code and had the same problem with an ardunio with no connections at all (other than usb).

I suspect you still have a problem, check out my code & see if you dont like it better...it is a touch more elegant.

edit: another note: I dont know if this is a standalone, or part of a bigger project, but you might consider a universal format (and processing) for commands, something like gcode. In your case, maybe a letter & a number.
"d0" -stop
"d1" -ccw
"d2" -cw
"s10"-set speed to 10/255

you get the point. you dont have to fuss around with what type of command your processing. in addition, you can more readily set up input filters to prevent you from entering something that might cause your program to spaz out.

@nerd35789 you posted in the wrong place where it says not for your project.

I have moved it here. Please be more careful where you post in future.

Please read the getting the most out of this forum sticky post at the start of each forum section.

Hi,
Are you sure it's here at 9600?
SerialMonitor3

I dont know what happened but after it worked, I reuploaded the code and it did it again but your solution helped and know it doesnt happen at all thanks!

glad I could be of assistance. happy sparking!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.