Arduino Forum

Community => Gigs and Collaborations => Topic started by: anshchawla on Feb 25, 2019, 10:12 pm

Title: Help in coding
Post by: anshchawla on Feb 25, 2019, 10:12 pm
So I want to create a code so that Arduino remembers how many times it has gone through that part of the code and accordingly take a reading from an analog pin and store it as "gone i""(i. = No of times Arduino has gone through that part )"



Eg if the code is run for first time let's say we introduce a variable i and thus now i=1 and "gone1" should contain the value

And if i=2 then "gone2" should contain the value
Title: Re: Help in coding
Post by: Paul_KD7HB on Feb 26, 2019, 12:16 am
So I want to create a code so that Arduino remembers how many times it has gone through that part of the code and accordingly take a reading from an analog pin and store it as "gone i""(i. = No of times Arduino has gone through that part )"



Eg if the code is run for first time let's say we introduce a variable i and thus now i=1 and "gone1" should contain the value

And if i=2 then "gone2" should contain the value
How much time do you want this to continue?

Paul
Title: Re: Help in coding
Post by: anshchawla on Feb 26, 2019, 05:12 am
30-40 times that is why I didn't make cases like i=1,2,3....
Title: Re: Help in coding
Post by: groundFungus on Feb 26, 2019, 09:54 am
You would have to declare 30 to 40 variables with names like gone1, gone2, gone3, ...  Or declare an array (https://www.arduino.cc/reference/en/language/variables/data-types/array/) of gone variables once.
Code: [Select]
int gone[40]; // an array of 40 gone varaibles. each int to hold the output of an analogRead()

Then in your function, something like:
Code: [Select]

void someFunction()
{
  static byte index = 0;
  gone[index] = analogRead(A0);
  index ++;
}

The static keyword (https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/static/) lets the index variable keep its value from iteration to iteration of the function.
Title: Re: Help in coding
Post by: anshchawla on Feb 26, 2019, 10:36 am
Thank you man
Very helpful
Title: Re: Help in coding
Post by: groundFungus on Feb 26, 2019, 10:46 am
I should add: in the above code you need some code to make sure that no more elements are written to the array than there are places for.  Writing past the end of an array can cause some very bad behavior.
Title: Re: Help in coding
Post by: anshchawla on Feb 26, 2019, 09:14 pm
Ok got it I would place a condition on "index" to be less than 40
Title: Re: Help in coding
Post by: anshchawla on Feb 26, 2019, 09:19 pm
Also can I store hexadecimal values in the array rather than integers
Title: Re: Help in coding
Post by: groundFungus on Feb 26, 2019, 09:57 pm
Decimal, octal, hexidecimal all are human readable representations of binary.  Ultimatly the values stored in the array are binary.  So yes, store them however you like and, when you read them back, display them however you like.
Title: Re: Help in coding
Post by: anshchawla on Feb 26, 2019, 10:00 pm
Thanks once again for your help
Title: Re: Help in coding
Post by: anshchawla on Mar 01, 2019, 02:28 pm
will the values erase if i am using arduino in low power mode
like i wake it up for some time and then make it go to sleep
will this erase the array and also the index value
if yes how to store these values in eeprom
Title: Re: Help in coding
Post by: groundFungus on Mar 01, 2019, 02:44 pm
The values kept in SRAM will stay there as long as power is not removed so sleeping the processor will not erase the array.