Can't figure out how to reset variable.

Working with an Arduino Uno..

I'm coming from VB.NET, and it seems impossible to reset the variable, and continue increamenting it after I've reset it back to zero.

int LMT86 = A0;
int A0_Read = 0;
float voltage = 0;
float Tmp = 0;
float TmaxA = 0;
int  MyCnt = 0; // Count's the seconds.
int a = 1;
int b = 5;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  // Structural Code.
  A0_Read = analogRead(LMT86); // Don't leave it out. It's important.
  Tmp = (426 - A0_Read) / 2.14;
  Tmp = (Tmp * 9.0 / 5.0) + 42.9; //Added 10.9

  // Write some code to store and show max tempature reading.
  if (Tmp > TmaxA)
  {
    TmaxA = Tmp;
  }
  else
  {
    TmaxA = TmaxA;
  }

  //Code to reset counter (MyCnt).
  if (MyCnt > b)
  {
    MyCnt = 0;
    TmaxA = 0;
  }
  else
  {
    MyCnt = ++a;
    TmaxA = Tmp;
  }


  Serial.print(Tmp, 1);
  Serial.print(" F   ");
  Serial.print("Tmax : ");
  Serial.print(TmaxA, 1);
  Serial.print("    ");
  Serial.print(MyCnt);
  Serial.println(" Seconds");

  delay(1000);
}

I'm having trouble with //Code to reset Counter (MyCnt).

I want the Max Temperature from the LMT86 chip to reset. It's my code to track the max temp. I got that part written correctly, but I can't seem to reset the variable.

It's supposed to reset the MyCnt (Counter).
It's supposed to reset the Tmax Variable too.

But this is what I'm getting as an output.


How would I do it in VB.NET?

Fun fact : Val turns a string into a double, on the fly.

Imports System.Math

Public Class Form1

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles_ Timer1.Tick

TextBox1.Text = Val(TextBox1.Text) + 1

If Val(TextBox1.Text) > 5 = True Then
TextBox1.Text = "0"
End If

End Sub
End Class

Took me like a minute to write that. It's inside a timer. Doesn't need the Imports System.Math, but figured I'd put it there just for demonstration.

I've also messed with Android code too, inside the Android Studio IDE. Made a couple simple calculator type programs for my Samsung Galaxy Note 4, Lollipop 5.0 API

Just got my Arduino like 3 days ago, figured I'd try practicing some code, but been at this code for about an hour now, and I can't seem to figure it out.

In all honesty.. I really underestimated how different it is.

When posting code, please format it correctly first using Ctrl-T in the IDE. It's much harder to follow without correct indenting.

And then please post it in [code]code tags[/code], not inline.
(It's not too late to edit and fix things a little. :wink: )

I made a couple of changes. See if this does what you want:-

const int LMT86 = A0;
int A0_Read = 0;
float voltage = 0;
float Tmp = 0;
float TmaxA = 0;
int  MyCnt = 0; // Count's the seconds.
//int a = 1;
const int b = 5;

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    // Structural Code.
    A0_Read = analogRead(LMT86); // Don't leave it out. It's important.
    Tmp = (426 - A0_Read) / 2.14;
    Tmp = (Tmp * 9.0 / 5.0) + 42.9; //Added 10.9

    // Write some code to store and show max tempature reading.
    if (Tmp > TmaxA)
        TmaxA = Tmp;

    //Code to reset counter (MyCnt).
    if (MyCnt > b)
    {
        MyCnt = 0;
        TmaxA = 0;
    }
    else
        MyCnt++;

    Serial.print(Tmp, 1);
    Serial.print(" F   ");
    Serial.print("Tmax : ");
    Serial.print(TmaxA, 1);
    Serial.print("    ");
    Serial.print(MyCnt);
    Serial.println(" Seconds");

    delay(1000);
}

N.B. If you run it with nothing connected to A0, the max value will be more apparent. It'll stay with the highest of the random values until reset.

It gives results like this:-

83.3 F   Tmax : 0.0    0 Seconds
72.3 F   Tmax : 72.3    1 Seconds
72.3 F   Tmax : 72.3    2 Seconds
84.1 F   Tmax : 84.1    3 Seconds
91.7 F   Tmax : 91.7    4 Seconds
85.8 F   Tmax : 91.7    5 Seconds
74.0 F   Tmax : 91.7    6 Seconds
71.5 F   Tmax : 0.0    0 Seconds
79.1 F   Tmax : 79.1    1 Seconds
89.2 F   Tmax : 89.2    2 Seconds
88.3 F   Tmax : 89.2    3 Seconds
78.2 F   Tmax : 89.2    4 Seconds
70.7 F   Tmax : 89.2    5 Seconds
75.7 F   Tmax : 89.2    6 Seconds

Edit: At zero seconds, Tmax looks wrong at 0 degrees F. If you change the reset line from:-TmaxA = 0;toTmaxA = Tmp;it looks better, and the Tmax value resets to the current temperature like so:-

71.5 F   Tmax : 71.5    0 Seconds
74.9 F   Tmax : 74.9    1 Seconds
79.9 F   Tmax : 79.9    2 Seconds
84.1 F   Tmax : 84.1    3 Seconds
88.3 F   Tmax : 88.3    4 Seconds
90.0 F   Tmax : 90.0    5 Seconds
91.7 F   Tmax : 91.7    6 Seconds
90.8 F   Tmax : 90.8    0 Seconds
88.3 F   Tmax : 90.8    1 Seconds
84.1 F   Tmax : 90.8    2 Seconds
79.1 F   Tmax : 90.8    3 Seconds
74.9 F   Tmax : 90.8    4 Seconds
71.5 F   Tmax : 90.8    5 Seconds
69.8 F   Tmax : 90.8    6 Seconds

Edit2: By the way, you never use your 'voltage' variable.

    MyCnt = ++a;

does not do the same as

TextBox1.Text = Val(TextBox1.Text) + 1

The first one increments the variable 'a' and next assigns the incremented value to 'MyCnt'.
After resetting 'MyCnt' to 0, 'a' still has the old incremented value.

The C style of incrementing

MyCnt++;

or if you want to use 'a'

MyCnt += a;

Yeah.. I would normally get in trouble for not removing unused variable when programing for Android.

I know, I'm really in no position to correct you, but I have a question..

Isn't this the correct way to do the else? Or does it really matter.

  else
  {
    MyCnt++;
  }

I'm new to the Arduino IDE syntax. But from what I've read online, I think that might be right, but I'm not sure. Do I need to use the opening curly brackets before and after the code after the else statement?

Because it just seems that if the opening and closing curly brackets, then the stuff after the else, would be included.

Something like this.. I've simplified the wording of the variables a little, and included what I just asked you about.

const int LMT86 = A0;
int Val_A0 = 0;
float Tmp = 0;
float Tmax = 0;
int  MyCnt = 0; // Count's the seconds.
//int a = 1;
const int b = 5;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  // Structural Code.
  Val_A0 = analogRead(LMT86); // Don't leave it out. It's important.
  Tmp = (426 - Val_A0) / 2.14;
  Tmp = (Tmp * 9.0 / 5.0) + 42.9; //Added 10.9

  // Handling Max Temperatures.
  if (Tmp > Tmax)
    Tmax = Tmp;

  //Code to reset counter variable (MyCnt).
  if (MyCnt > b)
  {
    MyCnt = 0;
    Tmax = 0;
  }
  else
  {
    MyCnt++;
  }
  
  Serial.print(Tmp, 1);
  Serial.print(" F   ");
  Serial.print("Tmax : ");
  Serial.print(Tmax, 1);
  Serial.print("    ");
  Serial.print(MyCnt);
  Serial.println(" Seconds");

  delay(1000);
}

Is that correct?

I also just noticed no opening and closing curly brackets for the if statements.. Is that allowed?

Fixed it..

const int LMT86 = A0;
int Val_A0 = 0;
float Tmp = 0;
float Tmax = Tmp;
int  MyCnt = 0; // Count's the seconds.
//int a = 1;
const int b = 5;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  // Structural Code.
  Val_A0 = analogRead(LMT86); // Don't leave it out. It's important.
  Tmp = (426 - Val_A0) / 2.14;
  Tmp = (Tmp * 9.0 / 5.0) + 42.9; //Added 10.9

  // Handling Max Temperatures.
  if (Tmp > Tmax)
  {
    Tmax = Tmp;
  }
  
  //Code to reset counter variable (MyCnt).
  if (MyCnt > b)
  {
    MyCnt = 0;
    Tmax = 0;
  }
  else
  {
    MyCnt++;
  }
  
  Serial.print(Tmp, 1);
  Serial.print(" F   ");
  Serial.print("Tmax : ");
  Serial.print(Tmax, 1);
  Serial.print("    ");
  Serial.print(MyCnt);
  Serial.println(" Seconds");

  delay(1000);
}

Is that right? or do I not need the curly brackets?

So let me see if I got this now..

If MyCnt is larger than 5, which will go up to 6.

Open curly bracket.
Then MyCnt = 0, and Tmax = 0.
Close curly bracket.

else..

MyCnt++;

Which increments MyCnt.

By what value? Nothing? How is it supposed to increment itself, if itself was reset to zero?

Regardless if it's else, it's lower than 6, or whatever, how is it supposed to Increment by zero?

Why is Integer b a constant?
It never changes, nor is there anything that changes it's value. It's used as a reference value..?

Other than that it looks pretty good.. I honestly don't understand the syntax.

I know I'm a noob, but if have the patience, and time, could you please explain..

What does N.B. mean also?

How is it supposed to increment itself, if itself was reset to zero?

The value of the variable is incremented by 1 by default when using the ++ syntax. So

myVariable++;

adds 1 to the value of myVariable. If this troubles you then you could always use

myVariable = myVariable + 1;

or do I not need the curly brackets?

Without the curly brackets only the first line of code after if/else/while etc will be executed. If you need to execute more than one line of code then the curly brackets are needed. Personally I always use curly brackets even with only one line of code to be executed as it makes the code block more obvious, as does Auto Formatting the code in the IDE.

UKHeliBob:
Personally I always use curly brackets even with only one line of code to be executed as it makes the code block more obvious, as does Auto Formatting the code in the IDE.

Not me Bob, but I have my IDE set up in "formatter.conf" and "preferences.txt" to indent 4 spaces rather than 2, so it's very easy to see what's going on.

@Ralph_1982, these are the three common valid ways of incrementing a variable by 1:-

myVariable++;
myVariable = myVariable + 1;
myVariable += 1;

And as Bob re-affirms, with 'if' 'else' and 'while', if there's only one line of code to be executed depending on the conditional, brackets are optional.

Why is Integer b a constant?
It never changes, nor is there anything that changes it's value. It's used as a reference value..?

You got it - because it's a constant value and never changes. The same applies to pin allocations, that's why I made this 'const' as well:-const int LMT86 = A0;It ensures that you don't accidentally try to change the value in the code, plus gives the compiler an opportunity to hard-code the value in flash program memory instead of SRAM.

N.B. :-

nota bene
N.B. is an abbreviation for the Latin nota bene, which means "note well." It is normally used at the beginning of a sentence in order to inform the reader that the following words are of great importance.

I've always used it a little more loosely, more like P.S. , not necessarily "of great importance". (I thought it simply meant "Note".)
(See, I just learned something too. :smiley: )

Edit:

I'm new to the Arduino IDE syntax.

It's actually just standard C++. There are many good online references for the language and the standard libraries like "stdlib.h".
I find this site particularly good:- cplusplus.com

I'm really sorry for sounding like a pain in the 455 OldSteve. I really am. You didn't have to remove the N.B., I was just curious what it meant.

nota bene (verb)
Observe carefully or take special notice (used in written text to draw attention to what follows).

That's very good that you learn new words, and try to use them more often. Even if it's not english.

It makes you smarter regardless if it's english or not, than the guy sitting next to you.

Now I know it, or at least am aware of what it means thanks to you.

I honestly really like the fact that there seems to be educated people on here. That's reassuring, that there's educated people trying to help me. Thank You.

I'm a well educated person as well, but I'm not omniscient. So I still gotta learn, if I wanna work with microcontrollers, and code, etc.

  1. Removed Voltage variable.. Even though I should learn more about the code I found, and use it, as it's the most accurate through hole tempature sensor on the market, before getting into SMD components.

  2. I formatted my code. (Edited, etc) Didn't know how to do that. Thanks for the help.

My Code
  1. I would like to learn how to change the settings mentioned.

  2. MyInteger++; (Increments MyInteger by 1). Understood.

  3. MyInteger = MyInteger +1; (Increments MyInteger by 1). Uses more memory, not good.

  4. Curly brackets after if/else/while etc I'll keep using. It's only a single byte for the curly brackets, and makes my code look well formated. Thanks for the info though on the fact that if there's only a single line of code after the if/else/while etc. then it executes, and compiles. Understood.

  5. Took a look at C:\Program Files (x86)\Arduino\lib\formatter.conf using Notepad++, and it seems the setting is there to be set to whatever a person wants it to be. I was a little curious, and took a look at that. I'm familar with files like that, and just never thought of it. So thanks for the reminder, and I'll go ahead and give it a shot and set it to 4.

  6. As for the C:\Program Files (x86)\Arduino\lib\preferences.txt file, it says at the top of the page..

!!!!!!!! UNLIKE PREVIOUS VERSIONS OF PROCESSING !!!!!!!!!!

DO NOT MODIFY THIS FILE, OR DELETE SETTINGS FROM THIS FILE

So idk.. I don't think I'm gonna mess with that file, unless someone can tell me a little more about it.

However.. I do see a setting in here that I might want to mess with..

editor.window.width.min = 400
editor.window.height.min = 290

When I open a saved project, or sketch as people call them, the stupid window opens tiny.. the 400 x 290 sounds about right.. So if I modify that 400px x 290px, then I'm going to take a guess and say my sketches will open up to the 1920 x 1080 of my screen resolution..

Anyways.. looks like a good read.

08a. myVariable++;
08b. myVariable = myVariable + 1;
08c. myVariable += 1;

08a. Even though there's not a 1 or whatever, it still increments it by 1. (Understood).
08b. It's kinda like Textbox1.Text = Textbox1.Text + 1, but Microsoft changed that in the IDE, and it was a lazy and incorrect way of people to do it that way. So Microsoft corrected it, and when they released Visual studio 2010 from 2008, people weren't able to do that anymore. So then that's when I learned about Textbox1.Text = Val(Textbox1.Text) + 1
08c. Seems a little backwards.. MyInt += 1 do anything.. idk.. just seems strange. It's foreign to me.

Anyways I understand, and with repetition, I'll remember it.

  1. If there's only one line of code after the if/else/while, etc. it's compiles, and executes. (Understood, thanks guys.)

  2. You got it - because it's a constant value and never changes. The same applies to pin allocations, that's why I made this 'const' as well :-

I'm not going to change it.. Idk why I would change it by mistake or whatever, but never in the many years of programming for VB.NET, never once declared a const. NEVER. In all honesty, I actually just learned about const like a couple months ago. But I'm aware of what a constant variable type is. It can NEVER be changed. Even if a value is assigned to it, it is CONSTANT. :slight_smile:

  1. It's actually just standard C++.. lol.. yeah.. it is isn't it.

Once Upon a time..

An older man, by the name of Mc Drummed... taught me HTML & JavaScript in Jail.
(8 Months, county bullet, first time in Jail.. for real) Never been back.. unlike some people..

Anyways, he taught me HTML & JavaScript in Jail.
How..? With the ol Pencil and Paper trick. and I also got to write code in Jail.. LMAO.. yup.

I got mixed up with people stealing 6 ATVs & 4 Sea-doo Watercrafts. I was stupid..

HTML & JavaScript around the time MySpace was still hot stuff.
Then I was like, I'd like to learn BASIC.
BASIC..? I found out that was OLD SCHOOL. I did actually write some basic, before I learned HTML & JavaScript on an old computer a while back.

Then I went to Barnes & Noble, spent &F@!)# $50 - $60 on Visual C#, which I can get online free today.

Didn't work.. Wans't interested.. Looked too difficult.

Then I was like Visual Basic..? Wow.. cool.

Anyways kept.. didn't get a book, started really rough out in Visual Basic, moved to VB.NET, and mainly wrote kiddy scripts, and simple, but very LONG programs, that others could probably do with less code.

HTML & JavaScript is my first language, I can still write some simple code, but need reminders. I made an eJuice calc written in it, and wrote an Ohm's law type calc in HTML & JavaScript for my brother with an iMac. I actually wanna learn programming for AVR's and stuff. I've been watching videos and stuff, and spend VERY LONG HOURS watching videos about the subject.. But for now, I'm trying to keep it simple. I figured I'd just practice my Arduino Uno programming.. (C++) CODING. Got it. I'll just take it one step at a time.. Could be worse.. Could be C, or Assembly. I'll take a look at the site a little bit latter, it's 5:43AM right now.. Seems there's just not enough time with my attention span, and my vast curiosity for this hardware stuff I've gotten into. 12. Please do not PM me for help. I am not a personal consultant. I have never PM'd anyone, In any Forum, anywhere.. Ever. I can reassure you, nobody will get a PM from me. They midas well just delete the feature in forums, because I never PM anyone. Had this video in queue for about several hours.. I honestly would like to do stuff like this, but I have to spend more money, and more time in learning about it. Not today Ralph.. Not today. https://www.youtube.com/watch?v=61V8xty6MPA Step by step Ralph.. step by step.. Thanks for the help guys, much appreciated.

You didn't have to remove the N.B., I was just curious what it meant.

I didn't remove it, it's still there. (Way back in reply #2)

  1. Took a look at C:\Program Files (x86)\Arduino\lib\formatter.conf using Notepad++, and it seems the setting is there to be set to whatever a person wants it to be. I was a little curious, and took a look at that. I'm familar with files like that, and just never thought of it. So thanks for the reminder, and I'll go ahead and give it a shot and set it to 4.

Don't edit that copy of "formatter.txt". It's best to leave it as-is, but copy it to your "Arduino15" folder and edit that copy. It takes preference over the original.

You can find the "Arduino15" folder by clicking on ">File >Preferences" in the IDE, then at the bottom of the window is the path.

  1. As for the C:\Program Files (x86)\Arduino\lib\preferences.txt file, it says at the top of the page..

!!!!!!!! UNLIKE PREVIOUS VERSIONS OF PROCESSING !!!!!!!!!!

DO NOT MODIFY THIS FILE, OR DELETE SETTINGS FROM THIS FILE

Leave this file alone. It's a different "preferences.txt", and shouldn't be edited, (as the warning says).
You'll find the other "preferences.txt" in your "Arduino15" folder, linked from ">File >Preferences"

08c. Seems a little backwards.. MyInt += 1 do anything.. idk.. just seems strange. It's foreign to me.

You soon get used to it. :slight_smile:

But I'm aware of what a constant variable type is. It can NEVER be changed. Even if a value is assigned to it, it is CONSTANT. :slight_smile:

Yep. The compiler will throw an error if you try to modify a constant. I see lots of people declare pin allocations as 'int', then accidentally change the value later in their code. 'const byte' is much safer and more appropriate.

Once you get the hang of it, you'll find C++ much more powerful than the various flavours of Basic, (which is where I also started centuries ago). Basic in the TRS-80, Commodore64 and Apple IIe, Visual Basic for Windows, PICBasic for PIC chips, then C/C++ for Windows/PICs/AVR chips. I wouldn't go back to Basic now if you paid me. :smiley:

Edit: By the way, if you prefer all opening brackets on a new line, like this:-

void setup()
{
}

instead of like this:-

void setup(){
}

you can add this to the copy of "formatter.conf" that you place in the "Arduino15" folder:-

#Move opening brackets onto new line
--style=allman --style=bsd --style=break -A1

It's a personal preference thing, but I hate the default for opening brackets and prefer them on a separate line.

And there's nothing wrong with PMs in general, in my opinion. Just not for people who want specialised help. That's what the forums are for, not PMs.

It's only a single byte for the curly bracket

The brackets don't end up in the compiled code so there is no "cost" in terms of executable code size but in my opinion they improve the readability of the code as does putting each curly bracket on its own line, but both of these are personal preferences rather than requirements to make code work.

Excellent.

I'm gonna try to work on my code for my LMT86 code..

I copied and pasted it, and I don't really understand it fully.

So maybe I could get some additional help added to the thread..?

If not that's fine. I'm gonna take a look at it regardless. I see I got a couple more replies back from it.

NOBODY has ANYTING on the LMT86. There was a single post made on another website, that I found.

It's accurate to 0.25°C (0.45°F), and from what I've seen on the search results on Mouser Electronics, it's the most accurate chip before getting into the SMD chips.

So I'd like to learn a little more about the chip, how the voltage, resistance, and degrees in Celsius is figured out..

I skimmed past the last couple posts on my thread, and looks like I might have some additional info on my question.. I'm needing help with the actual code, rather than the title "LMT86, stray '\342' in program".. so..

It's only a single byte for the curly bracket

UKHeliBob:
The brackets don't end up in the compiled code so there is no "cost" in terms of executable code size...

I noticed that and meant to say something, then forgot. :frowning:

And I'm glad I'm not the only one who likes opening brackets on a new line. :slight_smile:

I can't help with the LMT86, I'm afraid - I've never used one.

Ralph_1982:
I have never PM'd anyone, In any Forum, anywhere.. Ever.

I can reassure you, nobody will get a PM from me. They midas well just delete the feature in forums, because I never PM anyone.

I guess that you're referring to OldSteve's signature (which is not part of the actual post).

OldSteve:
And I'm glad I'm not the only one who likes opening brackets on a new line. :slight_smile:

And count me as number three :wink: So that makes a score of 100% of the respondents in this thread.

sterretje:
I guess that you're referring to OldSteve's signature (which is not part of the actual post).

Thanks for clarifying that. I did say this in response: "And there's nothing wrong with PMs in general, in my opinion. Just not for people who want specialised help. That's what the forums are for, not PMs.", but I forgot to add that it was only my signature for all replies.

And count me as number three :wink: So that makes a score of 100% of the respondents in this thread.

Pretty good score so far. :slight_smile:
I always auto-format other people's code to move opening brackets to a new line before I even try to read it. It makes it sooo much clearer.

OldSteve:
I always auto-format other people's code to move opening brackets to a new line before I even try to read it. It makes it sooo much clearer.

I did not know that that option existed; so always got irritated when I paste a 'wrong' code into the IDE. Going to look at that now.

sterretje:
I did not know that that option existed; so always got irritated when I paste a 'wrong' code into the IDE. Going to look at that now.

It's worked fine for me in both IDE V1.6.5 and V1.6.9.

Just paste it into the bottom of the "formatter.conf" file. Best to make a copy of the file before editing and place it in your "Arduino15" folder, as I mentioned earlier.

Here it is again:-

#Move opening brackets onto new line
--style=allman --style=bsd --style=break -A1

There's more info on statements that can be used here:-

Thanks, seems to work wonderful. One annoyance less :slight_smile: IDE 1.6.6

Had to dig a little as Windows (8) loves to hide user's AppData folder :frowning:

Karma added

sterretje:
Thanks, seems to work wonderful. One annoyance less :slight_smile: IDE 1.6.6

Oh good. It makes life much easier.

Had to dig a little as Windows (8) loves to hide user's AppData folder :frowning:

Sorry, I should have mentioned that. Windows 10 does the same.

I found (by luck) that if I click on ">File >Preferences" in the IDE, then down the bottom where it shows the path to the "Preferences.txt" file, a left-click opens "Preferences.txt", (as expected), and a right-click opens the "Arduino15" folder. Not sure if it works in other versions of Windows, but it works in Win10. (No need to change settings to "View hidden files", either.)

Karma added

Thank you for that. :slight_smile: