Go Down

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

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

Paul_KD7HB

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 am Last 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 am Last 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;
  gone[index] = analogRead(A0);
  index ++;
}

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

anshchawla

Thank you man
Very helpful

groundFungus

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

Ok got it I would place a condition on "index" to be less than 40

anshchawla

Also can I store hexadecimal values in the array rather than integers

groundFungus

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

Thanks once again for your help

anshchawla

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

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