arithmetic using analog values

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;

void setup() {
Serial.begin(4800);

;
}

void loop() {

command = analogRead(A0);
feedback = analogRead(A1);

error = (command - feedback);
analogWrite(A3,error);
errormeasure = analogRead(A5) ;

Serial.print("command = " );
Serial.print(command);
Serial.print("\t feedback = ");
Serial.print(feedback);
Serial.print("\t error");
Serial.print(error);
Serial.print("\t errormeasure");
Serial.println(errormeasure);

delay(2000);

}

None of your values is ever analog. Its a digital computer.

Can you try to explain what you are expecting? A floating point number perhaps?

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.

Are you confused by the different resolutions?

analogRead - 10 bit
analogWrite - 8 bit

All the analog stuff is based on integers, coming in or going out.

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

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.

You don't trust your serial prints?

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

  1. analogWrite does not generate an analog signal (except for DAC0 and DAC1
    pins on the Due only).

  2. 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.

MarkT:

  1. analogWrite does not generate an analog signal (except for DAC0 and DAC1
    pins on the Due only).

  2. 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?

53ledsled:
so I cant output an analog value and read it back?

Did you or did you not look up what PWM is? It is digital.

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..

And when feedback > command, error is negative which digitalWrite() might see as a big number.

But.... your sketch KNOWS what you output, unless something else affects that output before it gets read, what are you doing?

Read is 10 bits, write is 8 bits. Divide read by 4 before you write.

For AVR chip Arduinos:
Analog read is through an ADC, Analog Digital Converter.
Analog write is PWM, Pulse Width Modulation.

https://www.arduino.cc/en/Reference/AnalogRead