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
anshchawla:
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
30-40 times that is why I didn't make cases like i=1,2,3....
You would have to declare 30 to 40 variables with names like gone1, gone2, gone3, ... Or declare an array of gone variables once.
int gone[40]; // an array of 40 gone varaibles. each int to hold the output of an analogRead()
Then in your function, something like:
void someFunction()
{
static byte index = 0;
gone[index] = analogRead(A0);
index ++;
}
The static keyword lets the index variable keep its value from iteration to iteration of the function.
Thank you man
Very helpful
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.
Ok got it I would place a condition on "index" to be less than 40
Also can I store hexadecimal values in the array rather than integers
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.
Thanks once again for your help
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
The values kept in SRAM will stay there as long as power is not removed so sleeping the processor will not erase the array.