Terminating my loop?

I have a simple first project that tests the voltage drop over a resistor in order to test the capacity of Lithium cells and I've installed a Reed relay to control the circuit. I'm sure I'm missing something, but I've been searching for hours on how to write for the shut off. I thought an if statement would work, but it's not. Any help or pointers for a newbie would be appreciated. I can't seem to find the proper approach.

Below is my code:

// My Power Cell Tester
// tests voltage drop across resistor, calculates current and records capacity

const float resistor = 2.0;

int LED = 13;

int Relay = 8;

float capacity=0, value1, value2, voltage1, voltage2, current, time=0;

void measure (void) {

value1= analogRead(0);

value2= analogRead(3);

voltage1=abs(value1/1024*5.0);

voltage2=abs(value2/1024*5.0);

current = abs((voltage1-voltage2)*1000/resistor);

capacity=abs(current*time/3600);

time++;

}

boolean x=false;

ISR(TIMER1_OVF_vect) {
TCNT1=0x0BDC;
x=!x;

measure();

}

void setup() {

pinMode(LED, OUTPUT);

pinMode(Relay, OUTPUT);

TIMSK1=0x01; // enabled global and timer overflow interrupt;
TCCR1A = 0x00; // normal operation page 148 (mode0);
TCNT1=0x0BDC; // set initial value to remove time error (16bit counter register)
TCCR1B = 0x04; // start timer/ set clock

Serial.begin(9600);

};

void loop ()
{

digitalWrite(LED, HIGH);

digitalWrite(Relay, HIGH);

Serial.print("Voltage1= ");
Serial.print(voltage1);
Serial.print(" V. ");

Serial.print("Voltage2= ");
Serial.print(voltage2);
Serial.print(" V. ");

Serial.print("Voltage drop= ");
Serial.print(voltage1-voltage2);
Serial.print(" V. ");

Serial.print("Current= ");
Serial.print(current);
Serial.print(" mA ");

Serial.print("Capacity= ");
Serial.print(capacity);
Serial.print(" mAh ");

Serial.print("Discharge time= ");
Serial.print(time);
Serial.print(" sec. ");

Serial.print("\n");

delay(1000);

{
if (voltage1 < 3.68);
digitalWrite (Relay, LOW);
}

};

The cut off value listed was arbitrary. I intend on using 3.0 V for the Lithium cells when I get this right.

Also, my current calculations are better since using abs numbers instead of floating, they were all over the place, but they still varies quite a bit. Is there an error there?

Why have you got 200+ microseconds of delay in interrupt context?

Why did you decide to post your code without code tags?

if (voltage1 < 3.68);

What's that semicolon doing there?

my current calculations are better since using abs numbers instead of floating

I don't understand that sentence.

With out going truw the whole thing, there is 2 mistakes here.

 {
  if (voltage1 < 3.68);
  digitalWrite (Relay, LOW);
  }
  

};

Should be:

  if (voltage1 < 3.68){
  digitalWrite (Relay, LOW);
  }
  

}

As far as the semicolon placement, I copied that from the format instructions on if statements without the semicolon, but when I compiled the sketch it wouldn't accept without the semicolon. However, I figured it was changing the meaning.

The suggested change

If (voltage1 < 3.68) {
DogitalWrite (Relay, LOW);
}

}

Cut and pasted over the previous code had no effect.

As for the previous comments, I'm self-teaching and I'm afraid you lost me. I used a delay(1000) merely to slow down sampling so I could read results easier as I was working on this sketch. Using floating numbers, gave very irregular results. Using absolute number calculation gave much more consistent results. I read where floating mathematics gives erroneous values even in simple calculations. This is why I used abs values. The source of this info is in the Arduino info on the abs command.

 If (voltage1 < 3.68) {
    DogitalWrite (Relay, LOW);
    }

It's best to cut and paste your actual code, not something you think you wrote.
And post it all between code tags please.

Dockjl:
As far as the semicolon placement, I copied that from the format instructions on if statements without the semicolon, but when I compiled the sketch it wouldn't accept without the semicolon.

Don't, just! throw? in punctuation; for the sake: of it...

{
  if (voltage1 < 3.68);
  digitalWrite (Relay, LOW);
  }

The way I see it, if the If case is true, it will just do nothing cause of the ";".

And this line:

 digitalWrite (Relay, LOW);

will get executed every time independently of the If. I'm I right?

For some reason, this:

ISR(TIMER1_OVF_vect) {
  TCNT1=0x0BDC;
  x=!x;
 
  measure();
 
}

Make me think you are not a real Newbee. Newbee don't tend to play with timers and pointers.

Yet I could be wrong.

I read where floating mathematics gives erroneous values even in simple calculations. This is why I used abs values.

Well I don't know if the maker of Arduino change the rule of Math in it's programming, but the "abs" or absolute value of a number simply mean that it's the positive value of a value. Example:

abs(-5) = 5

abs(2 - 3) = 1

abs(-x) = x

abs(3 + 2) = 5

I'm just saying that cause i hit lot of people who has a esoteric view of what absolute mean, it's just to make it clear.

@Frederic_Plante: you do know you're allowed to edit earlier posts and add to them, don't you?

Yeah, but to me a post represent a idea. If I have 2 perspectives to present about a subject I prefer to use 2 post then just 1. Other wise it become unclear and weird.

If I have some thing more to say about a perspective, I will edit the post that is about that perspective.

And most of the time when some one has read a post, he will not read it a second time, then the new stuff added is lost to that person. It's a bad habit, but that how human work, we want new stuff to read all the time.

Knowing that I prefer using a new post.

Also, being a Québécois, so a french speaking person communicating, here, in English, I tend to often use the modify option to reformulate, to make though more clear to you guys in your language. And I also modify to correct the spelling.

So yes, I do know that I can modify my post. :wink:

And, to conclude about that subject, I don't hang out on the French part of the forum, cause for some reason I found that that part kinda suck. So I'd ratter make the extra effort to communicate in a language that is not mine to get to the best part of the forum.

OK. Just to clarify, I began this sketch by looking at another sketch that was posted online. I did not mess with what is apparently something to do with timer interrupt. The last time I tried to write code was 1982 and it wasn't C or C+, so believe me, this is new to me. If you want to play detective and try to find a reason why you think I'm not telling you the truth, that's fine. I honestly didn't understand what the moderator meant by code tags. I think I understand that now. I did about 40 hours of reading and studying on this stuff before giving it a try. Thought the forum might be a good place to get a little help or ask questions. Since posting this, I've done some more reading and see a lot of things I missed or misconstrued from the Arduino site, the section on abs and the writers opinion of floating math being one of them. I think my understanding of interrupts is part of the problem. Maybe I'll come back in a few months when I've figured it out on my own.

Hey don't take it like that :wink:

It's just that, it's obvious that even if it was not C, you got some programing experience. I don't like when people under value them self that's all.

I was looking at you stuff and I was thinking hey this guy is not a newbee for sure. Maybe your new to the Arduino community, that i believe.

Thanks, I guess, but after 20 years in medicine, trying to figure out how to write a simple program has been more frustrating than removing a ruptured appendix! I tried to alter a program to suit my needs. Didn't have a clue what you were talking about because that portion of the code was copied verbatim until could figure this stuff out. Obviously, it's going to take a while. Just thought the forum might speed up the learning curve.

Dockjl:
Thought the forum might be a good place to get a little help or ask questions.

The forum's pretty good, and friendly.

We kind-of hope people will read the "sticky" at the head of the programming section. Then you would know what we mean by code tags.

Your first post is pretty vague, if you re-read it. You seem to be saying something is not working, without being very specific about what it is. What happened, what you expected to happen, that sort of thing.

As an example, you output a lot of stuff, right?

Serial.print("Voltage1= ");
  Serial.print(voltage1);
  Serial.print(" V.  ");
 
  Serial.print("Voltage2= ");
  Serial.print(voltage2);
  Serial.print(" V.  ");
 
  Serial.print("Voltage drop= ");
  Serial.print(voltage1-voltage2);
  Serial.print(" V.  ");

How about sharing that output? Then we have some idea of what you are seeing and what perhaps is going wrong.

Reformulating cause it make no sens. lol

If you don't put bracket after the if statement, it will execute a single command that is after the if statement in this case nothing cause you put ";" right after.

If you want many command to be executed, you absolutely need to put them between brackets.

Yeah that is better :wink:

It's pretty much like in basic, if you say:

If x = 6 then y = y +1
j=9
g=0
...

If the If statement, if true, Basic will only execute y=y+1, just like if you don't put brackets.
j=9 and g=0 will get executed whether the if statement is true or not

But if you say:

If f = 9 then

   h=h+3
   j=6
   h=h+j

end if

If the if statement is true, Basic will execute every thing between the "then" and the "end if". The brackets serve the same job.

Bon! While we are there. The code tag

The code tag are to detach you code from you normal text. Basicaly the code tag are to make you code clearer. Example:

This text is in a code tag

This text is not in a code tag, but is between 2 texts that are in code tag

And this text is also in a code tag.

To make a code tag you must hit the little button that has a "#" at the top of the editing windows. it will put (code) (/code) in your text and you put your code in between.

But mister Gammon is right you should go read the protocol on how to use this forum it's full of cool trick to learn on how to make your communication here better.

You can find it here: http://forum.arduino.cc/index.php/topic,148850.0.html

OK... So this is where I'm at with this sketch...

// My Power Cell Tester
// By arduino.cc user "Dockjl"
// August 18, 2013
/* Tests voltage, voltage drop across resistor,
calculates current and records battery capacity
with output to serial port */

// Using Arduino 1.0.5

const float resistor = 3.75; // the resitor net value is 3.75 Ohms
int LED = 13; // initialize LED on pin 13
int Relay = 8; // initialize Reed Relay on pin 8

float capacity=0, value1, value2, voltage1, voltage2, current, Average_Current=0, time=0;

void measure (void) {

value1= analogRead(0);

value2= analogRead(3);

voltage1=abs(value1/1024*5.0);

voltage2=abs(value2/1024*5.0);

current = abs((voltage1-voltage2)*1000/resistor);

Average_Current=abs(current+current-1)/2; // define Average Current for use in calculating Capacity

capacity=abs(Average_Current*time/3600);

time++;

}

boolean x=false;

ISR(TIMER1_OVF_vect) {
TCNT1=0x0BDC;
x=!x;

measure();

}

void setup() {

pinMode(LED, OUTPUT);
pinMode(Relay, OUTPUT);
pinMode(A0, INPUT);
pinMode(A3, INPUT);

TIMSK1=0x01; // enabled global and timer overflow interrupt;
TCCR1A = 0x00; // normal operation page 148 (mode0);
TCNT1=0x0BDC; // set initial value to remove time error (16bit counter register)
TCCR1B = 0x04; // start timer/ set clock

Serial.begin(9600);

};

void loop ()
{

digitalWrite(LED, HIGH);

digitalWrite(Relay, HIGH);

analogRead (0);
analogRead (3);

Serial.print("Voltage1= ");
Serial.print(voltage1);
Serial.print(" V. ");

Serial.print("Voltage2= ");
Serial.print(voltage2);
Serial.print(" V. ");

Serial.print("Voltage drop= ");
Serial.print(voltage1-voltage2);
Serial.print(" V. ");

Serial.print("Current= ");
Serial.print(current);
Serial.print(" mA ");

Serial.print("Average Current= ");
Serial.print(Average_Current);
Serial.print(" mA ");

Serial.print("Capacity= ");
Serial.print(capacity);
Serial.print(" mAh ");

Serial.print("Discharge time= ");
Serial.print(time);
Serial.print(" sec. ");

Serial.print("\n");

delay(1000);

if (voltage1 < 3.68){
digitalWrite (Relay, LOW);
}

}

And the data I'm getting looks like this...

Voltage1= 0.00 V. Voltage2= 0.00 V. Voltage drop= 0.00 V. Current= 0.00 mA Average Current= 0.00 mA Capacity= 0.00 mAh Discharge time= 0.00 sec.
Voltage1= 3.45 V. Voltage2= 1.08 V. Voltage drop= 2.37 V. Current= 631.51 mA Average Current= 631.01 mA Capacity= 0.00 mAh Discharge time= 1.00 sec.
Voltage1= 3.43 V. Voltage2= 1.07 V. Voltage drop= 2.35 V. Current= 627.60 mA Average Current= 627.10 mA Capacity= 0.17 mAh Discharge time= 2.00 sec.
Voltage1= 3.42 V. Voltage2= 1.12 V. Voltage drop= 2.30 V. Current= 613.28 mA Average Current= 612.78 mA Capacity= 0.34 mAh Discharge time= 3.00 sec.
Voltage1= 3.41 V. Voltage2= 1.12 V. Voltage drop= 2.29 V. Current= 611.98 mA Average Current= 611.48 mA Capacity= 0.51 mAh Discharge time= 4.00 sec.
Voltage1= 3.42 V. Voltage2= 1.09 V. Voltage drop= 2.33 V. Current= 621.09 mA Average Current= 620.59 mA Capacity= 0.69 mAh Discharge time= 5.00 sec.
Voltage1= 3.45 V. Voltage2= 1.08 V. Voltage drop= 2.37 V. Current= 631.51 mA Average Current= 631.01 mA Capacity= 0.88 mAh Discharge time= 6.00 sec.
Voltage1= 3.43 V. Voltage2= 1.06 V. Voltage drop= 2.36 V. Current= 630.21 mA Average Current= 629.71 mA Capacity= 1.05 mAh Discharge time= 7.00 sec.
Voltage1= 3.41 V. Voltage2= 1.08 V. Voltage drop= 2.33 V. Current= 621.09 mA Average Current= 620.59 mA Capacity= 1.21 mAh Discharge time= 8.00 sec.
Voltage1= 3.43 V. Voltage2= 1.04 V. Voltage drop= 2.39 V. Current= 638.02 mA Average Current= 637.52 mA Capacity= 1.42 mAh Discharge time= 9.00 sec.
Voltage1= 3.44 V. Voltage2= 1.09 V. Voltage drop= 2.35 V. Current= 626.30 mA Average Current= 625.80 mA Capacity= 1.74 mAh Discharge time= 11.00 sec.
Voltage1= 3.42 V. Voltage2= 1.07 V. Voltage drop= 2.35 V. Current= 626.30 mA Average Current= 625.80 mA Capacity= 1.91 mAh Discharge time= 12.00 sec.
Voltage1= 3.42 V. Voltage2= 1.11 V. Voltage drop= 2.31 V. Current= 617.19 mA Average Current= 616.69 mA Capacity= 2.06 mAh Discharge time= 13.00 sec.
Voltage1= 3.42 V. Voltage2= 1.09 V. Voltage drop= 2.33 V. Current= 622.40 mA Average Current= 621.90 mA Capacity= 2.25 mAh Discharge time= 14.00 sec.
Voltage1= 3.43 V. Voltage2= 1.08 V. Voltage drop= 2.35 V. Current= 627.60 mA Average Current= 627.10 mA Capacity= 2.44 mAh Discharge time= 15.00 sec.
Voltage1= 3.41 V. Voltage2= 1.08 V. Voltage drop= 2.33 V. Current= 621.09 mA Average Current= 620.59 mA Capacity= 2.59 mAh Discharge time= 16.00 sec.
Voltage1= 3.43 V. Voltage2= 1.09 V. Voltage drop= 2.34 V. Current= 623.70 mA Average Current= 623.20 mA Capacity= 2.77 mAh Discharge time= 17.00 sec.
Voltage1= 3.42 V. Voltage2= 1.07 V. Voltage drop= 2.34 V. Current= 625.00 mA Average Current= 624.50 mA Capacity= 2.95 mAh Discharge time= 18.00 sec.
Voltage1= 3.42 V. Voltage2= 1.02 V. Voltage drop= 2.40 V. Current= 640.63 mA Average Current= 640.13 mA Capacity= 3.20 mAh Discharge time= 19.00 sec.
Voltage1= 3.41 V. Voltage2= 1.08 V. Voltage drop= 2.33 V. Current= 621.09 mA Average Current= 620.59 mA Capacity= 3.28 mAh Discharge time= 20.00 sec.
Voltage1= 3.40 V. Voltage2= 1.06 V. Voltage drop= 2.34 V. Current= 625.00 mA Average Current= 624.50 mA Capacity= 3.47 mAh Discharge time= 22.00 sec.
Voltage1= 3.42 V. Voltage2= 1.08 V. Voltage drop= 2.33 V. Current= 622.40 mA Average Current= 621.90 mA Capacity= 3.80 mAh Discharge time= 23.00 sec.
Voltage1= 3.43 V. Voltage2= 1.08 V. Voltage drop= 2.34 V. Current= 625.00 mA Average Current= 624.50 mA Capacity= 3.99 mAh Discharge time= 24.00 sec.
Voltage1= 3.42 V. Voltage2= 1.07 V. Voltage drop= 2.35 V. Current= 626.30 mA Average Current= 625.80 mA Capacity= 4.17 mAh Discharge time= 25.00 sec.
Voltage1= 3.42 V. Voltage2= 1.04 V. Voltage drop= 2.38 V. Current= 634.11 mA Average Current= 633.61 mA Capacity= 4.40 mAh Discharge time= 26.00 sec.
Voltage1= 3.42 V. Voltage2= 1.08 V. Voltage drop= 2.33 V. Current= 622.40 mA Average Current= 621.90 mA Capacity= 4.49 mAh Discharge time= 27.00 sec.
Voltage1= 3.42 V. Voltage2= 1.08 V. Voltage drop= 2.34 V. Current= 623.70 mA Average Current= 623.20 mA Capacity= 4.67 mAh Discharge time= 28.00 sec.
Voltage1= 3.41 V. Voltage2= 1.08 V. Voltage drop= 2.33 V. Current= 622.40 mA Average Current= 621.90 mA Capacity= 4.84 mAh Discharge time= 29.00 sec.
Voltage1= 3.43 V. Voltage2= 1.07 V. Voltage drop= 2.35 V. Current= 627.60 mA Average Current= 627.10 mA Capacity= 5.05 mAh Discharge time= 30.00 sec.

Comments? Suggestions? Hope this is how it's supposed to be posted.

Sorry... Thought I tagged it this time and just saw your explanation.

So, now I see what you're talking about