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:
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)
"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.
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.
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"?
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.
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.
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.