Go Down

### Topic: Help in coding (Read 592 times)previous topic - next topic

#### anshchawla

##### 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

#### Paul_KD7HB

#1
##### 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

#### anshchawla

#2
##### Feb 26, 2019, 05:12 amLast Edit: Feb 26, 2019, 05:13 am by anshchawla
30-40 times that is why I didn't make cases like i=1,2,3....

#### groundFungus

#3
##### Feb 26, 2019, 09:54 amLast Edit: Feb 26, 2019, 09:55 am by groundFungus
You would have to declare 30 to 40 variables with names like gone1, gone2, gone3, ...  Or declare an 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;
index ++;
}

The static keyword lets the index variable keep its value from iteration to iteration of the function.

#4
Thank you man

#### groundFungus

#5
##### 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.

#### anshchawla

#6
##### Feb 26, 2019, 09:14 pm
Ok got it I would place a condition on "index" to be less than 40

#### anshchawla

#7
##### Feb 26, 2019, 09:19 pm
Also can I store hexadecimal values in the array rather than integers

#### groundFungus

#8
##### 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.

#### anshchawla

#9
##### Feb 26, 2019, 10:00 pm
Thanks once again for your help

#### anshchawla

#10
##### 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

#### groundFungus

#11
##### 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.

Go Up