Go Down

Topic: store sensor values into an array (Read 2 times) previous topic - next topic

meowth08

Hi,

I have 2 switches: sw1 and sw2 and a sensor.
I declared my sw1 equal to 0.
Also declared my sw2 to 1.
I want to store the the sensor value on an array whenever any of the switches is pressed.
my array would look like this: [sensor value, sw, sensor value, sw, sensor value, sw ..........]
The "sw" will have a value of "0" when sw1 is pressed and "1" when sw2 is pressed.

Problem is. I know what I want but I don't know how to do the code.
I tried reading some references but I am having a hard time understanding the codes.

ash901226

urm could you explain it in another way ?

Arrch

Post the code you have using the proper code tags.

UKHeliBob

Which bit are you having problems with, reading the switches, reading the sensor or storing the data ?

In pseudo code
Code: [Select]
declare myArray[]
arrayIndex = 0
loop()
  if sw 1 is pressed
   set myArray[arrayIndex] to 0
   increment arrayIndex
   set myarray[arrayIndex] to sensor value
   increment arrayIndex
  end if
 
  if sw 2 is pressed
   set myArray[arrayIndex] to 1
   increment arrayIndex
   set myarray[arrayIndex] to sensor value
   increment arrayIndex
  end if
end of loop()

How many entries are there likely to be ? Space for the array is not unlimited.
Will the run ever start over again with previous data in the array ?  If so think of the implications and be prepared to deal with them.
Putting the data into an array implies that you are going to read it, which you can do in a similar way.
You will lose the data in the array if power goes off.  Is that OK ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

meowth08

Thanks for the replies.
My apology for not posting the requirements clear.

The pseudo code posted by UK HB was I think the same as what I am trying to do.
But one thing is missing. I also want to record what switch was pressed.
From my first post, I assumed sw1 as 0 and sw2 as 1.
So my array would look like [ 500, 0, 600, 1, 300, 1 .....and so on and so forth]
The first element (500) is my sensor value.
The second element (0) denotes that sw1 is pressed.
The third element (600) is also a sensor value.
The fourth element (1) denotes that it is sw2 which is pressed.
I hope I'm being able to explain the requirements clearly now.

For UKHB's reply/questions: I am having problems storing the data as an array.
Quote
How many entries are there likely to be ?
The size is not definite but I think it would not exceed 30. Is it ok?
Quote
Will the run ever start over again with previous data in the array ?
I don't understand this question sir.
Quote
Putting the data into an array implies that you are going to read it, which you can do in a similar way.
I will read it.
Quote
You will lose the data in the array if power goes off.  Is that OK ?

OOPS! not OK. I have plan B. How about the EEPROM? Can I use it instead of storing my data in an array?


Go Up