Avoiding Serial.available repetitions

Greetings,

I want to run a motor usign pin 11 (pwm) and change the dutyCycle when someone types a value on the serial.

I tried using the same sort of coding as seen in rf24master example: "Getting Started".

However, whereas in this example the program reads only 1 value from the serial, in my version I get the impression that it reads 4 times, distorting the result.

I use a variable "DT" for dutyCycle and an IF LOOP combined with the "Serial.available" to read when some1 inputs a number in Serial.

Don't mind the comments inside the program, please:

#include <SoftwareSerial.h>


int motorPin = 11;           
int DT = 50;    
int temp;

void setup() {
  
  Serial.begin(9600);
  pinMode(motorPin, OUTPUT);
}


void loop() {
  
    
  if ( Serial.available() )
  {
    DT = (Serial.read());
    if ( (DT >= 0) & (DT <= 255))
    {
      
      delay(7);
      //Serial.println("DT atualizado para:");
      Serial.println(DT);
    }
   }
  Serial.flush();
    
  analogWrite(motorPin, DT);
  
  
  
}

The phenomenon I can't explain is the following, where not only the program enters 3 times inside the IF LOOP, but it does not accept the new value i set for dutyCtcle (DT) which was 60, AS WELL as decreasing it down to 13:

(Since I can't post an image, I'll copy part of the text inside Serial window)

50
50
50
50
50
50
50
DT atualizado para:
55
55
DT atualizado para:
48
48
DT atualizado para:
13
13
13
13
13
13
13

Thanks for your assistance.

and an IF LOOP combined

An if STATEMENT does NOT loop.

  if ( Serial.available() )
  {
    DT = (Serial.read());

What is sending the serial data? If it is the Serial Monitor application, it is sending a string, not a binary value.

(The) (parentheses) (around) (Serial.read()) (are) (useless).

  Serial.flush();

Block until all outgoing serial data has been sent. How can that possibly help?

    if ( (DT >= 0) & (DT <= 255))

& and && are not interchangeable.

"if" is not a loop.
Serial.read() does not need an extra pair of parentheses around it (but they do no harm).
The code presented does not match the copy of part of the serial window. Where is the real code?

13 is the value of a Carriage Return being added by the serial window application.
50, 55, and 48 represent various digits and lead me to suspect that more than one key is being used.

What was entered from the keyboard? How did you expect to enter a value of 60, which would require you to enter a less-than sign without any Carriage Return being added?

I think that you are going about the task of reading characters and converting them the wrong way. Others can probably be of help in providing examples of the correct way to accomplish this.

This is probably closer to what you want:

DT = Serial.parseInt();

Have a look at the examples in Serial Input Basics

...R

That code did not make that output. Did you change anything but to comment out this line,

//Serial.println("DT atualizado para:");

And without knowing the inputs that made that output you present mystery without all the clues.

The if () statement here will either always be true or true for all chars 0 to 127. I could check but I won't.

    DT = (Serial.read());
    if ( (DT >= 0) & (DT <= 255))

Most important:
Why do you have Serial.flush() in there? If you send multiple characters the code you have will read the first byte and dump the rest EVERY TIME.

#include <SoftwareSerial.h>


int motorPin = 11;           
int DT = 50;    
int temp;

void setup() {
  
  Serial.begin(9600);
  pinMode(motorPin, OUTPUT);
}


void loop() {
  
    
  if ( Serial.available() )
  {
    DT = (Serial.read());
    if ( (DT >= 0) & (DT <= 255))
    {
      
      delay(7);
      //Serial.println("DT atualizado para:");
      Serial.println(DT);
    }
   }
  Serial.flush();
    
  analogWrite(motorPin, DT);
  
  
  
}
50
50
50
50
50
50
50
DT atualizado para:
55
55
DT atualizado para:
48
48
DT atualizado para:
13
13
13
13
13
13
13

Why do you have Serial.flush() in there? If you send multiple characters the code you have will read the first byte and dump the rest EVERY TIME.

That is NOT what flush() does.

Not any more apparently.

flush()
Description

Waits for the transmission of outgoing serial data to complete. (Prior to Arduino 1.0, this instead removed any buffered incoming serial data.)

My mistake. Too bad, it might have explained a problem.

The problem here is someone hasn't read anything about the Serial "thing". I had to try and use the only thing I got: examples.

I never like to work with stuff without knowing what it is about, now that I've read the source supplied by Robin I have a much better understanding.

Thanks to all for your efforts, I'll be working on this, armed with that invaluable new knowledge.

I'll be creating a function to read and store data from Serial (monitor?), validate it to be only integers (using ascii table) and then convert using atoi. A lot to do, just to get a mere number from a user...

Is there any book a guy can read on Arduino "things"?

Best Regards

aimaty:
The problem here is someone hasn't read anything about the Serial "thing". I had to try and use the only thing I got: examples.

I never like to work with stuff without knowing what it is about, now that I've read the source supplied by Robin I have a much better understanding.

Thanks to all for your efforts, I'll be working on this, armed with that invaluable new knowledge.

I'll be creating a function to read and store data from Serial (monitor?), validate it to be only integers (using ascii table) and then convert using atoi. A lot to do, just to get a mere number from a user...

Is there any book a guy can read on Arduino "things"?

Best Regards

Perhaps you're not familiar with a new thing called "Google"? It will teach you everything you need to know.

http://lmgtfy.com/?q=Arduino+Serial

Regards,
Ray L.

aimaty:
I'll be creating a function to read and store data from Serial (monitor?), validate it to be only integers (using ascii table) and then convert using atoi. A lot to do, just to get a mere number from a user...

Is there any book a guy can read on Arduino "things"?

Best Regards

if ( dataChar >= '0' && dataChar <= '9' ) // it must be a digit and you don't need a table, but '0' == 48

The Arduino site has pages and pages of Arduino info. The green bar at the top has links. Start with Learning->Reference, bookmark that and everything that looks interesting.

I keep mine in a bookmark folder, Mozilla allows that and makes it pretty easy.
When I work on code in the IDE I keep the browser open with as many tabs as I need to have all needed pages open and ready for whatever I'm doing.

The standard AVR C library (used in Arduino) reference is here:
http://www.nongnu.org/avr-libc/user-manual/modules.html

In my signature space below are addresses to 3 of Nick Gammon's Arduino blogs. Those and the rest on his site are complete, detailed yet straightforward simple explorations on many Arduino topics with hardware as well as software laid out and provided.

aimaty:
The problem here is someone hasn't read anything about the Serial "thing". I had to try and use the only thing I got: examples.

I never like to work with stuff without knowing what it is about, now that I've read the source supplied by Robin I have a much better understanding.

Thanks to all for your efforts, I'll be working on this, armed with that invaluable new knowledge.

I'll be creating a function to read and store data from Serial (monitor?), validate it to be only integers (using ascii table) and then convert using atoi. A lot to do, just to get a mere number from a user...

Is there any book a guy can read on Arduino "things"?

Best Regards

The problem here is someone hasn't read anything about the Serial "thing"

That is only a part of the "problem".

This is not the best way you should resolve this. Instead of writing a function to collect "serial" data simplify your original , if it is in fact an actual code you are / were using. At this point the actual output format is irrelevant, as long as SINGLE (visible / printable) character entered will results in SINGE character printed.
When you get that working you can add / insert whatever you need to accomplish your task.
BTW you probably will find that you need to do more than just let loop() monitor the serial input.

void loop() {                          since serial detection / processing  is the only function performed by loop() - it is de facto a "serial loop" 
   if ( Serial.available() )           detect serial data in buffer - single yes / no analysis 
  {
    Serial.print("Input detected  ");
    Serial.println( Serial.read()); prints single character from buffer and clears it from buffer 
   }
    no character detected - you could put another Serial. print here just to remind you that the if() failed , but no reason to introduce any questionable delays for now 
}

Than describe the source of you serial data. Are you using "Serial Monitor", right?
How are you terminating the input(s) ?
How many characters do you input before terminating the input?
This simple loop() code will repeat ALL of them , one at a time.
What is the terminating option set to - next to baud rate option in Serial Monitor?
These terminating options are adding some "invisible" characters to your output.