Connecting DAC directly to powered speakers. Result?

Status
Not open for further replies.
I am using the Arduino IDE, so yes, it exists there in my case.

Yes, the f suffix can be used - see code below. Other suffixes exist too, for binary, long etc, to force the compiler to use a specific type for that constant/literal.

Running your example gives results as expected.
Also running this modified example which is much closer to my code gives results as expected:
Code:
   float i[2] = {0.0f, 0.0f};
   uint16_t idx = 0; 
   float ix = -1.0f;
   float iy = 1.0f;

void setup() {
  // put your setup code here, to run once:
	Serial.begin(9600);
	delay(2000);
}

void loop() {
   i[idx] += 0.01;
   int16_t smp = 2048 + (i[idx] * 2048); 
   Serial.println( smp );
   if (i[idx] >= iy) i[idx] = ix;
   delay(20);
}

However, the matching code snippet within my project (also in Arduino IDE) gives the error that I explained.

The compiler is very clever and makes assumptions based on all sorts of things. The resulting assembler for this basic example is sure to be different from the same code in my full project because of the differences in the code that surrounds it. For example - in the code snippets above, the compiler will probably deduce that idx, ix and iy are all just constants, and compile differently than in other cases. There are many other subtle differences between my full code and these smaller examples.
 
Thanks for info on use of suffix.....
I have tried your code, and it works, a good clean sawtooth...
I plotted the print data and looks good, and recorded in Audacity and looks and sounds good.
But doesn't seem to need the "f" suffix......I have tried several combinations of in and out...all work.
Can you give example using this simple type of code where it is actually needed.....just to see it work/or not work in practice so I could be aware when to definately use it.???
Thanks.


Code:
  float i[2] = {0.0f, 0.0f};
   uint16_t idx = 0; 
   float ix = -1.0f;
   float iy = 1.0f;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(2000);
  analogWriteResolution(12);
}

void loop() {
   i[idx] += 0.01;
   int16_t smp = 2048 + (i[idx] * 2048.0f); 
  // Serial.println( smp );
    analogWrite(A14, smp);
   if (i[idx] >= iy) i[idx] = ix;
  // delay(20);
}
 
the “f” states that your telling the compiler that the value is a float

example for a baudrate you should specify its type as well, ex

115200UL

f being float
UL being unsigned long

if your doing multiple calculations without giving the types, you can sometimes have wrong results

in your code above, the compiler does not know if “2048” is a float or not, so its probably treated as an int... it shouldnt matter though because your adding it, and returning unsigned long anyways :)
but yeah because you use 0.0 the compiler can deduce that its a float as well
 
Last edited:
yes, OK, got it, thanks.......but it doesn't like 2048f....... it needs 2048.0f........which seems to be double jobbing...???..so it has to be written as a float to use the float f suffix........
 
Status
Not open for further replies.
Back
Top