Pages: [1]   Go Down
Author Topic: Need to sample voltage readings and compare two values.  (Read 1072 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am a arduino beginner. Now I have a project: I need to sample voltage data from a sensor via Pin A0. At the same time, I need to compare the consecutive two data:
if there is a data jump between the two data, Pin 13 will output HIGH; otherwise the Pin 13 will output LOW. My code is as the following, but a error "varuabke-suzed object 'voltageValue' may not initialized" happens. I really don't know why. How to code it?
Code:
void loop(){
  //read the input on analog pin A0;
  int i;
  int voltageValue[i];
  float voltage[i];
  for(i=0;;i++){
    int voltageValue[i]= analogRead(A0);
    float voltage[i] = voltageValue[i] * (5.0 / 1023.0);
    float deltaVoltage = voltage[i+1]-voltage[i];
    if (deltaVoltage>=0.5||deltaVoltage<=-0.5){
      digitalWrite(led,HIGH);
    }
    else{
      digitalWrite(led,LOW);
    }
    // Serial.println("voltage = ");
    Serial.println(voltage[i]);
  }

}

Moderator edit: code tags make things very much clearer.

Moderator edit: Proper indenting helps too. (Nick Gammon)
« Last Edit: September 21, 2012, 02:57:19 am by Nick Gammon » Logged

UK, Southwest
Offline Offline
Full Member
***
Karma: 5
Posts: 138
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I will have a quick look at your code.
However, you would get a much better response if you read the "Read this before posting a programming question" sticky post at the top of the forum.

Before many people will bother answering they might like to think that you have bothered to go through the basic
steps.

The first thing you should do is post *ALL* your code for the sketch and use CODE tags.  Its much easier to read.  Also, how do you expect anyone else to compile your code fragment - I've just tried and it will not compile.
Then post your code formatted.  Its much easier to read.

Having forced you to read that,
- you have declared a global variable thus:
int VoltageValue;

You then redeclared another (local) variable with the same name:
int VoltageValue =  blah...;

The language and compiler both think you have asked for 2 separate variables, and you are using one of them (probably the first one)
somewhere in the code you have not shown, wrongly.

Please go back and look at the guidelines, then post again

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24454
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ask yourself "what is the value of i ?"
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for your suggestion, I will post again with following the rules.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24454
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Posting the actual error message also helps.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31550
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read up on how a for loop works, yours go on forever and never stop.
Don't use an array just use two values, newValue and oldValue.
Declair oldValue outside a function to make it global and give it a value of -1.
In setup read the analogue input into oldValue.
As the last thing in the loop make oldValue equal to newValue.
Loop will run hundreds of times a second so any LED on will only last a tiny fraction of a second, use a delay to keep it on or long enough to see.
Logged

Pages: [1]   Go Up
Jump to: