Pages: [1]   Go Down
Author Topic: AnalogWrite suggestion.  (Read 1558 times)
0 Members and 1 Guest are viewing this topic.
Queens, New York
Offline Offline
Faraday Member
**
Karma: 98
Posts: 3576
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im looking at the wiring_analog.c file and I noticed that if val == 0 it outputs LOW as a digital signal and if val == 255 the output is digital HIGH. I see that if val is not 0 or 255, it goes to the timer. Usually we need to map the input signal to 0 and 255 because you get weird data if the input is less than or greater than 0 and 255.

Why not make set a limit in the wiring_analog.c file, instead of val == 0 (LOW) or val == 255 (HIGH), make it val <= 0 (LOW) and val >= 255 (HIGH).
My reason I noticed that when I mapped my data, I still for some reason got out numbers greater and less than what I wanted to get.

Example, controlling two independent motors from one value ranging -255 and 255, negative number would represent reverse and likewise for positive.

So I did,

DRVFor = map(Drive, 0,255,0, 255); // not really needed
DRVRev = map(Drive, -255,0,0,255);
MotorFor = analogWrite(9, DRVFor);
MotorRev = analogWrite(6, DRVRev);

But when the I did a print out of the Motor1 & 2, this is what I noticed.  When Drive was less than 0, MotorFor was a negative number and when Drive was greater than 0, MotorRev was negative. Why is that?

So this was when I checked the analog Read/Write file.

In the end, I changed my code and used an IF/ELSE statement, but it's still weird to see that if a number is negative (and mapped that way), the results will be wrong. I was looking to get 0 when Drive was negative for DRVFor, but nope.

Again, it's just a suggestion.
« Last Edit: January 11, 2013, 10:12:09 am by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26242
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My reason I noticed that when I mapped my data, I still for some reason got out numbers greater and less than what I wanted to get.
You could always apply this function before calling analogWrite.
I don't see why a library should be subject to bloat.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 206
Posts: 12855
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't see why a library should be subject to bloat.

Huh?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Im looking at the wiring_analog.c file and I noticed that if val == 0 it outputs LOW as a digital signal and if val == 255 the output is digital HIGH. I see that if val is not 0 or 255, it goes to the timer. Usually we need to map the input signal to 0 and 255 because you get weird data if the input is less than or greater than 0 and 255.

I believe this was the result of a patch several years ago to the original analogWrite function where if you did a analogWrite(pin#,0); instead of a pure 0% duty cycle you actually got a duty cycle that had one timer bit time of HIGH. This could easily be seen with a scope on the output pin, so they decided to just patch the function to digitalWrite a hard LOW if a 0 duty cycle value was sent to the function. Not sure why the clamp for the 100% value also but I'm sure it's related to the hardware timer behaviour.

Lefty
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49080
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From: http://arduino.cc/en/Reference/AnalogWrite
Quote
Syntax

analogWrite(pin, value)

Parameters

pin: the pin to write to.

value: the duty cycle: between 0 (always off) and 255 (always on).

Returns

nothing
So, why are you assigning the return value to a variable?
Logged

Dallas, TX USA
Online Online
Faraday Member
**
Karma: 67
Posts: 2692
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From: http://arduino.cc/en/Reference/AnalogWrite
Quote
Syntax

analogWrite(pin, value)

Parameters

pin: the pin to write to.

value: the duty cycle: between 0 (always off) and 255 (always on).

Returns

nothing
So, why are you assigning the return value to a variable?
Exactly. analogWrite() doesn't return a value, it is a void function,
so there is no telling what you get for the assignment.

In the larger picture I do think that analogWrite() should check for >= HIGH
instead of == 255 to provide a "known behavior" for values outside the real range.
This may start to be an issue as people use hard coded numbers like 255, 1023, etc
or even mapping functions that return values outside the usable range on the
different "..duino" boards and the code starts to be used on different processors
since they all may not have the same range.

For this specific case, wouldn't it be easier to use absolute value: abs() instead of map() since
there is no real need for mapping?


--- bill
Logged

Pages: [1]   Go Up
Jump to: