Im trying to write the code for what will later be used to control the position of a DC motor. as of right now I've set up my joystick (two pots) as both command and feedback so I can work on just the code portion of it while only having to carry a small circuit around with my laptop (the rest of the hardware is large.) "VRx" is the command and the "VRy" is the feedback from the joystick. as you can see from my code I simply subtract the feedback from the command to calculate the error which is then output back into the Arduino so I can confirm my voltage. All three of these values are analog and work out fine. the problem that im having is that the analog error that I am reading is digital?
ive tried changing the variable types and changing inputs/outputs to different pins but I always get the same results.
here is my code
//A0 input
int command ;
//A1 input
int feedback;
//A3 output
int error;
//A5 input
byte errormeasure;
Analog output is done with PWM. That can't be measured on an analog input pin without first passing it through an integrator (low pass filter). If I understand correctly, you have pin A3 connected to pin A5, as a loopback test.
so the whole point of this circuit is to read two analog values (command and feedback) subtract one from the other to get the error. then I output this analog value to which I hooked back into the arduino (this so I don't have to carry my multimeter with me all the time)
should I just measure the error portion with my meter
53ledsled:
so the whole point of this circuit is to read two analog values (command and feedback) subtract one from the other to get the error. then I output this analog value to which I hooked back into the arduino (this so I don't have to carry my multimeter with me all the time)
should I just measure the error portion with my meter
YES pin A3 is connected to pin A5
If it's an analog meter, yes. But a digital meter will probably not be able to read it, because it's effectively AC (... and certainly won't give you an accurate reading). It will probably freak out.
53ledsled:
so the whole point of this circuit is to read two analog values (command and feedback) subtract one from the other to get the error. then I output this analog value to which I hooked back into the arduino (this so I don't have to carry my multimeter with me all the time)
should I just measure the error portion with my meter
YES pin A3 is connected to pin A5
analogWrite does not generate an analog signal (except for DAC0 and DAC1
pins on the Due only).
There's no point doing this, you already have the error value in your hand.
However I guess you are aiming to close a servo loop so you want to drive PWM
and direction to a motor driver/shield? If so you need to cast that error
value to a float and put through a PID loop, then generate PWM and direction
from the output value.
analogWrite does not generate an analog signal (except for DAC0 and DAC1
pins on the Due only).
There's no point doing this, you already have the error value in your hand.
However I guess you are aiming to close a servo loop so you want to drive PWM
and direction to a motor driver/shield? If so you need to cast that error
value to a float and put through a PID loop, then generate PWM and direction
from the output value.
so I cant output an analog value and read it back?
Read post 5 again. A simple low pass filter can be used to find
the average value to be read as an analog input.
If the PWM uses a slow enough frequency, you can read the on/off
time. You'd need to determine which is best for your application.
Dwight
so basically my mistake was to try to measure a voltage from my circuit with same circuit? I can just use the error value itself to power the motor driver. if this is true then I'm going to be mad at my self.
53ledsled:
so basically my mistake was to try to measure a voltage from my circuit with same circuit? I can just use the error value itself to power the motor driver. if this is true then I'm going to be mad at my self.
Sorry to make you mad at yourself. Do you understand why it doesn't work?
Well, the same circuit puts out PWM, so it's the same mistake all around... I did suggest a circuit to fix it, earlier in the thread... if you (@53ledsled) researched it, you would have found that you can do it with one resistor and one capacitor.
'Analog' does not mean 'floating point', it means that the whatever-it-is notionally runs over a continuum. Of course, a digital computer can only approximate a continuum with discrete numeric values at some limited degree of resolution. For arduino analog input pins, that's a 10 bit int. For output pins, it's a pulse-width-modified digital signal with a resolution of 8 bits..