0

I have an 2 analog signal, and I want to calculate the difference between 2 signals (A0 and A1), then integrate an analog, but it doesn't work (I just started studying Arduino).

Please could you show me how to deal with this problem?

Thanks a lot

float analogInPin = A0;
float analogInPin1 = A1;
float analogOutPin = DAC1;
float FeqIn, FeqIn1, FeqIn2, FeqIn3, FeqOut, w, Triarea, Recarea, area, n, y, t;

void setup() {
  Serial.begin(9600); // use the serial port 
}

void loop() {
  FeqIn1 = analogRead(analogInPin);
  FeqIn2 = analogRead(analogInPin1);
  FeqIn3 = FeqIn1 - FeqIn2
  // integration
  area(1) = 0;
  y(1) = 0;
  for (n = 1; n = t; t++) {
    Triarea(n+1) = (0.5*t(2))*(FeqIn3(n+1) - FeqIn3(n));
    Recarea(n+1) = t(2)*(FeqIn3(n));
    area(n+1) = (Triarea(n+1) + Recarea(n+1)) + area(n);
  }
}

The error message is:

Arduino: 1.6.5 (Windows 8.1), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

_02.ino:3: error: 'DAC1' was not declared in this scope
_02.ino.ino: In function 'void loop()':
_02.ino:20: error: expected ';' before 'area'
_02.ino:21: error: 'y' cannot be used as a function
_02.ino:24: error: 'Triarea' cannot be used as a function
_02.ino:24: error: 't' cannot be used as a function
_02.ino:24: error: 'FeqIn3' cannot be used as a function
_02.ino:24: error: 'FeqIn3' cannot be used as a function
_02.ino:25: error: 'Recarea' cannot be used as a function
_02.ino:25: error: 't' cannot be used as a function
_02.ino:25: error: 'FeqIn3' cannot be used as a function
_02.ino:26: error: 'area' cannot be used as a function
_02.ino:26: error: 'Triarea' cannot be used as a function
_02.ino:26: error: 'Recarea' cannot be used as a function
_02.ino:26: error: 'area' cannot be used as a function
'DAC1' was not declared in this scope
  • Please copy and paste your actual code into your question (edit the question to do this) - not a screen shot of it. Then we are in a position to test it ourselves, or to amend it and post the amended code. – Nick Gammon Aug 18 '15 at 7:13
  • You tagged this question with arduino-uno and arduino-due. Which one is it? They are not the same thing. – Nick Gammon Aug 18 '15 at 7:14
  • Now please post the error messages which I can just see a tiny bit of in the screen shot. area(1) = 0; - what is that supposed to be doing? – Nick Gammon Aug 18 '15 at 8:45
1

I think your main problem here is a fundamental lack of understanding about C++. There's numerous basic errors in your code. Let's take a look at them:

FeqIn3 = FeqIn1 - FeqIn2

All C++ statements must be terminated by a ; otherwise it doesn't know where one statement finishes and the next one begins.

area(1) = 0;
(and many others)

You have declared a bunch of float variables in the global scope. You are then trying to use those float variables as functions, and on top of that you are then trying to assign a value to the return value of that function. That makes absolutely no logical sense whatsoever.

I think you meant to be using arrays of float values. Arrays are denoted by [ and ] not ( and ). Further, they have to be defined as arrays, including the size of the array, not a single value. Also in C++ arrays are zero based (i.e., they start at index 0 not index 1).

For example, area could be defined as:

float area[1024]; // 1024 floating point values in an array
// ...
area[0] = 0;

Next:

Recarea(n+1) = t(2)*(FeqIn3(n));

I don't even know what to make of this bit... If I assume you should replace (...) with [...] for indices to arrays, it still makes no sense. You're calculating a single value (FreqIn3 = FreqIn1 - FreqIn2) then using that as an array (FreqIn3[n]) which makes absolutely no sense to me at all. Then you're using another value t which you are incrementing (incorrectly, more on that later), which again you are then trying to use as an array (t[2]). Which is it - arrays or single values? And how do you increment an entire array?

Now the incrementing:

for (n = 1; n = t; t++)

n starts at the value 1. While n is assigned the value t, increment t. Now, does that sound logical? And what is t? when that loop starts? It has no defined value, so it could be anything at all. And when does the loop end? Well, since you're using float values for your counters (which is idiotic at best) you won't ever get any integer wraparound, so t will never equal 0 and consequently the result of assigning t to n will never be 0, so the loop will never end.

I think you need to go back to absolute basics and learn a bit more about syntax of C++, arrays, functions, loops, etc, before embarking on something complex like signal integration.

0
float FeqIn, FeqIn1, FeqIn2, FeqIn3, FeqOut, w, Triarea, Recarea, area, n, y, t;

...

      Triarea(n+1) = (0.5*t(2))*(FeqIn3(n+1) - FeqIn3(n));

I honestly don't know what you are doing here.

Triarea is a float, right? A single variable of type float.

What is this?

      Triarea(n+1) = (0.5*t(2))*(FeqIn3(n+1) - FeqIn3(n));

Triarea(n+1) looks like you are calling a function called Triarea with the argument n+1.

That doesn't make any sense because it isn't a function.


Maybe you mean to treat it as an array?

  Triarea[n+1] = (0.5*t[2])*(FeqIn3[n+1] - FeqIn3[n]);

But you have not declared those variables as arrays.

It is pure guesswork what is going on in your mind.

However your syntax certainly explains the error messages, for example:

_02.ino:24: error: 'Triarea' cannot be used as a function

If I may suggest, read a tutorial on basic C++ programming.

0

As previous answers have pointed out, your understanding of C syntax has shortcomings, and you should study C further. Besides those syntax issues, you may need to revise some of your concepts of how to program.

Firstly, the references you looked at regarding integration may have shown forms like Tₓ₊₁ = (δ/2)·(Vₓ₊₁ – Vₓ) for “triangle area” and Rₓ₊₁ = δ·Vₓ for “rectangle area” [where I've written δ in place of your t(2)]. In spite of the use of subscripts in these formulas, the array concepts mentioned in other answers are not relevant here. Instead of saving voltage data in arrays, just save and work with two voltage values: the current differential voltage reading and the previous differential voltage reading. Letting Vᵣ and Vₓ stand for previous and new voltage differences, use Vᵣ where Vₓ is called for, and Vₓ where Vₓ₊₁ is called for. At the end of each step, set Vᵣ = Vₓ.

Secondly, making separate computations of “triangle area” and “rectangle area” is verbose, unnecessary, and less accurate than a properly combined computation. Note that Tₓ₊₁ + Rₓ₊₁, as used in computing areaₓ₊₁, is equal to (δ/2)·(Vₓ₊₁ – Vₓ) + δ·Vₓ = (δ/2)·Vₓ₊₁ – (δ/2)·Vₓ + δ·Vₓ = (δ/2)·Vₓ₊₁ + (δ/2)·Vₓ = δ·(Vₓ₊₁ + Vₓ)/2. Thus, instead of those three lines of code using two temporary variables and lots of indices, use code like the following for the integral:

area += deltaT*(Volt_r + Volt_s)/2;
Volt_r = Volt_s;

(In the above, Volt_s corresponds to your FeqIn3 value.)

Thirdly, your attempted for loop needs to be replaced by an actual for loop; and in addition, deltaT, a time-step delta, needs to be computed. (Your t(2) needs to represent a change in time, not an absolute time, to represent the time-width of the rectangle and triangle you are adding to the total.) To get a deltaT, read millis() after the analogRead lines. For example, you could say:

Volt_s = analogRead(analogInPin) - analogRead(analogInPin1);
t_s = millis();
deltaT = t_s - t_r;
area += deltaT*(Volt_r + Volt_s)/2;
t_r = t_s;
Volt_r = Volt_s;

Fourthly, using two analogReads to get a differential voltage has two problems: first, any common-mode voltage wastes bits of ADC resolution, so contributes to error; second, the two reads occur at different times so their voltages are not comparable. Instead, you could use an external op-amp to subtract voltages, or could use the Due's differential-front-end ADC mode, per Section 43.6.8, Differential Inputs, on page 1326 in the ARM Cortex M3 Processor manual, plus possibly a gain of 1, 2, or 4 (per Section 43.6.9). Also see: Analog performance ... of Due's 12 bit ADC (including how to speed up analogReads on the Cortex), Differential Sensing with Arduino, Setting ADC compare mode in a differential input, and Due pinout diagram.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.