variables not read in loop

I have an issue with variables in a loop not being read correctly.

Following program flow is used (the variables not correctly read are in loop a):

  1. initialisation
  2. setup
  3. loop

read variables a, b, c
display variables a, b, c

loop a (repeat 250 times)

read variables a, b, c
make decision based on read variables

end loop a

The reason I use a loop with repeat 250x is that the controller needs to react very fast as soon as a short circuit is present. After performing the loop 250x then the loop is exited, the variables voltage and current read again and displayed on a LCD (thsi program part works fine, so not included in the code here). After this the loop is started again.

Code (abbreviated to the absolute minimum required to solve this problem):

// display voltage and current, and act when short circuit occurs (loop a)

    int V1 = 2; // max voltage when short circuit occurs
    int C1 = 3; // min current when short circuit occurs
    int OC; // declare short circuit threshold integer
    #define RELAIS 8

void setup()
{
  pinMode(RELAIS, OUTPUT);
  digitalWrite(RELAIS, HIGH);
}

void loop()

{

  int voltagevalue = analogRead(6); // read voltage
  int currentvalue = analogRead(7); // read current
  float voltage = voltagevalue * (30.00 / 1023.00) - 0.09;
  float current = currentvalue * (30.00 / 1023.00);

// here voltage and current are being read for display; this works fine so code omitted

  for (int j1 = 0; j1 < 250; j1++) // start loop a, repeat 250x
{
    int voltagevalue = analogRead(6); // read voltage in loop a
    int currentvalue = analogRead(7); // read current in loop a
    int voltage = voltagevalue * (30.00 / 1023.00) - 0.09;
    int current = currentvalue * (30.00 / 1023.00);
    if (voltage < V1 && current > C1)  
        {int OC = 1;}                  // if short circuit present set C1
    else
        {int OC = 0;}                  // if no short circuit reset C1
    if (OC > 0)
        {
          digitalWrite(RELAIS, LOW);
          delay (200);
        }
    if (OC < 1)
        {
          digitalWrite(RELAIS, HIGH);
        }
}
}

Edited: v1 should be V1
Edited: forgot {} for loop A

@brice3010 , after 22 posts on these forums, you should be aware that code must be posted in [code]code tags[/code], not simply pasted inline.
There are very good reasons for this:-

  1. It's placed into a scrolling window rather than taking up excessive page space.
  2. It's much easier to copy then paste into an IDE for checking/modifying/verifying.
  3. Often, code pasted inline is corrupted by italics, with the [i] that causes the corruption being completely removed from the code, making it look like an error.
  4. Also very often, some of the code is converted into smilies, especially 8), making it impossible to read.

It's not too late to edit your post and add code tags. :wink:
(It'll make potential helpers much happier.)

Edit: And even before digging in deeper, I see one error - you forgot to make 'RELAIS' an output with 'pinMode()':-pinMode(RELAIS, OUTPUT);

OldSteve:
@brice3010 , after 22 posts on these forums, you should be aware that code must be posted in [code]code tags[/code], not simply pasted inline.
There are very good reasons for this:-

  1. It's placed into a scrolling window rather than taking up excessive page space.
  2. It's much easier to copy then paste into an IDE for checking/modifying/verifying.
  3. Often, code pasted inline is corrupted by italics, with the [i] that causes the corruption being completely removed from the code, making it look like an error.
  4. Also very often, some of the code is converted into smilies, especially 8), making it impossible to read.

It's not too late to edit your post and add code tags. :wink:
(It'll make potential helpers much happier.)

Thank you OldSteve, now I know how to do this (and now if I knew how to draw a smiley)

OldSteve:
@brice3010 , after 22 posts on these forums, you should be aware that code must be posted in

[code]code tags[/code]

, not simply pasted inline.
There are very good reasons for this:-

  1. It’s placed into a scrolling window rather than taking up excessive page space.
  2. It’s much easier to copy then paste into an IDE for checking/modifying/verifying.
  3. Often, code pasted inline is corrupted by italics, with the [i] that causes the corruption being completely removed from the code, making it look like an error.
  4. Also very often, some of the code is converted into smilies, especially 8), making it impossible to read.

It’s not too late to edit your post and add code tags. :wink:
(It’ll make potential helpers much happier.)

Edit: And even before digging in deeper, I see one error - you forgot to make ‘RELAIS’ an output with ‘pinMode()’:-

pinMode(RELAIS, OUTPUT);

The pinMode line is present in my actual program, I forgot to include it in my post. Thank you for pointing this out, I will edit my post.

 if (voltage < V1 && current > C1) 
        {int OC = 1;}                  // if short circuit present set C1
    else
        {int OC = 0;}                  // if no short circuit reset C1
    if (OC > 0)

Scope of “OC”.

brice3010:
The pinMode line is present in my actual program, I forgot to include it in my post. Thank you for pointing this out, I will edit my post.

It's always a good idea to post the actual code, copied directly from the IDE. Then you won't make omissions like that.

Looks like AWOL might have nailed it. Let us know if something else isn't working after you fix that. (You declared 'OC' globally, so remove 'int' from in front of the other instances of it's use.)

Edit: And thanks for adding the code tags. It looks much more civilised. :slight_smile:

GREAT!!!

That did it, kudos to AWOL and OldSteve!! :smiley:

brice3010:
GREAT!!!

That did it, kudos to AWOL and OldSteve!! :smiley:

Well, AWOL really. (My 'pinMode()' comment doesn't count, if it was in the 'real' code. :wink: )

Anyway, I'm glad that you got it sorted. :slight_smile: