Go Down

Topic: Project with the DAC on the Arduino Due  (Read 449 times) previous topic - next topic

Grumpy_Mike

Map returns a value, you are throwing it away.
Check you have the right input / output ranges.

Good diagram that is, think about it.

bulls54

Dlloyd, based on the chart you posted,  did you mean something like this? Mapping the values so that each pin and value are the same.

Code: [Select]
int analogPin = 0;
int val = 0;
void setup() {
  Serial.begin(9600);
  pinMode(DAC1, OUTPUT);
  pinMode(A0, INPUT);
 
}

void loop() {
  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (3.3 / 1023.0);
analogWriteResolution(10);
  Serial.println(voltage);
  int val = sensorValue - ((0.55) / (3.3 / 1023.0));
  int valhigh = sensorValue - ((2.75) / (3.3 / 1023.0));
  map(sensorValue,0,1023, val, valhigh);
  map (A0, 0, 1023, val, valhigh);
  map (DAC1, 0, 1023, val, valhigh);
  analogWrite(DAC1, sensorValue);
}


still .55 in is 0.9 out the DAC1. Can i see what you did dlloyd?

Grumpy_Mike

#32
Dec 19, 2014, 08:41 pm Last Edit: Dec 19, 2014, 08:43 pm by Grumpy_Mike Reason: To get the stupid code box the right size.
Quote
Mapping the values so that each pin and value are the same.
No.
You are doing it again, ignoring what I said.
Quote
Map returns a value, you are throwing it away.
http://arduino.cc/en/reference/map

The map function has to have something to the left of an equals sign in order to put the results in.

All that messing about and you end up doing:-
Code: [Select]
analogWrite(DAC1, sensorValue);
Which is what you always did and it is wrong.

dlloyd

A0 is providing the input "value" to the map function.
Looking at A0, what is the A0's code when DAC1 = 0 and when DAC1 = 1023?
These numbers are important, because its where you're starting "from".

You only need to use the map function once. When you get it working, you'll get meaning-full values only from 1/6 to 5/6.
You'll have to add some statements to cleanup the ends so that:
Anything less than 0 returns 0.
Anything greater than 1023 returns 1023.   

The end result should be:
As A0 goes from 0-0.55V, DAC1 stays at 0.55V.
As A0 goes from 0.55V to 2.75V, DAC1 does the same.
As A0 goes from 2.75V to 3.3V, DAC1 stays at 2.75V.

bulls54

Sorry about that I didn't see Grumpy_Mike's message on the next page. I think i understand what to do here. According to dlloyds picture, we want it to that when A0 is 0, its at the low of the DAC1 which is .55. So it should be
Code: [Select]
int valDAC1 = map (A0, 0, 1023 ,.55 , 2.75); ?

I keep trying to put my val, and valhigh into the mapping function. In terms of
Code: [Select]
analogWrite(DAC1, sensorValue);

It should be (after the mapping command):


Code: [Select]
analogWrite(DAC1,valDAC1 );

In terms of the clean up. I should use the constarin command in order to get


Quote
As A0 goes from 0-0.55V, DAC1 stays at 0.55V.

Grumpy_Mike

Code: [Select]
int valDAC1 = map (A0, 0, 1023 ,.55 , 2.75);
A0 is a pin number so no. read that link on how to use the map function again.
Also do not mix up voltages with numbers from the A/D you are mixing things up in that line of code.

bulls54

okay so if i solve the equation voltage = sensorValue * (3.3 / 1023.0) where the voltage is .55. the sensorValue will be 170.5. Do the same for a voltage of 2.75, you get 852.5.

The map then turns into

Code: [Select]
int valDAC1 = map (sensorValue, 0, 1023 ,170.5, 852.5 );

I think i am solving the right equation...

Then
Code: [Select]
analogWrite(DAC1,valDAC1 );

for the final line?

dlloyd

#37
Today at 12:32 am Last Edit: Today at 06:01 am by dlloyd
The numbers are OK but you're compressing rather than expanding.
The "from" and "to" need to be swapped...
Code: [Select]
int valDAC1 = map (sensorValue, 170.5, 852.5, 0, 1023);

EDIT:

Will be away for a while, so here's the few things I've tried (not completely tested):

Using map:

Code: [Select]
unsigned short sensorValue = 0;
float voltage = 0.0;

void setup() {
  Serial.begin(9600);
  analogWriteResolution(10);
}

void loop() {
  sensorValue = analogRead(A0);
  voltage = sensorValue * (3.3 / 1023.0);

  sensorValue = map(sensorValue, 170.5, 852.5, 0, 1023);
  if (sensorValue < 0) {
    sensorValue = 0;
  }
  else if (sensorValue > 1023) {
    sensorValue = 1023;
  }

  Serial.print(voltage);
  Serial.print("  ");
  Serial.println(sensorValue);

  analogWrite(DAC1, sensorValue);
  delay(100);
 }


Not using map:

Code: [Select]
unsigned short sensorValue = 0;
float voltage = 0.0;

void setup() {
  Serial.begin(9600);
  analogWriteResolution(10);
}

void loop() {
  sensorValue = analogRead(A0);
  voltage = sensorValue * (3.3 / 1023.0);
  sensorValue -= 170;                // shift down by 1/6
  sensorValue = sensorValue * 1.5;   // multiply by (2/6 for range + 1/6 for shift)

  if (sensorValue < 0) {
    sensorValue = 0;
  }
  else if (sensorValue > 1023) {
    sensorValue = 1023;
  }

  Serial.print(voltage);
  Serial.print("  ");
  Serial.println(sensorValue);

  analogWrite(DAC1, sensorValue);
  delay(100);
}


Map with 10-bit ADC, 12-bit DAC

Code: [Select]
unsigned short sensorValue = 0;
float voltage = 0.0;

void setup() {
  Serial.begin(9600);
  analogWriteResolution(12);
}

void loop() {
  sensorValue = analogRead(A0);
  voltage = sensorValue * (3.3 / 1023.0);

  sensorValue = map(sensorValue, 170.5, 852.5, 0, 4095);
  if (sensorValue < 0) {
    sensorValue = 0;
  }
  else if (sensorValue > 4095) {
    sensorValue = 4095;
  }

  Serial.print(voltage);
  Serial.print("  ");
  Serial.println(sensorValue);

  analogWrite(DAC1, sensorValue);
  delay(100);
 }

Go Up