Arduino: Write Voltage

You can find the full sketch code at the bottom of the page or you can download the .ino file from DropBox

//voltage measured at vref pin
int vref = 4.9 * 1000; //multiply 1000 to convert to mV

//vars to store values
int mv_val, tmp_val;

void setup(){
Serial.begin(115200);
pinMode(mv_pin, INPUT);
}

void loop(){
mv_val = read_mv(mv_pin);
Serial.print(mv_val);
Serial.println("mv");

int tmpINT1 = write_mv(10, mv_val);
Serial.print(tmpINT1);
Serial.println("duty");

Serial.print(mv_val / 1000.0);
Serial.println("v");

delay(1000);
}

//function to read voltage
int read_mv(byte tmpPIN){
tmp_val = analogRead(tmpPIN); // read value
tmp_val = map(tmp_val, 0, 1023, 0, vref); //remap mv

return tmp_val;
}

//function to write voltage
int write_mv(byte tmpPIN, int tmpMV){
tmp_val = map(tmpMV, 0, vref, 0, 255); //remap d
analogWrite(tmpPIN, tmp_val); //write to pin

return tmp_val;
}