Pages: 1 [2] 3   Go Down
Author Topic: store sensor values into an array  (Read 2205 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You need to consider how you are going to read back the data and what it needs to look like in order for your "replay" function to parse/understand it.
This will be my next problem after storing the data.
Quote
If you write sensor and switch data in sequentially different spots in eeprom this might confuse as to what type of data it is.
I write "sensor data" first in odd address then the corresponding "switch data" on the next address (even). Data always come in pair and they are stored alternately.
Quote
Also consider how fast this data can be written because this will affect your length and "resolution" of playback. You will likely get a much finer resolution then you desire.
How do I do this?
Quote
So you will have to make rule about when to sample the data, which you are kinda doing with the switch.
Sensor Data is sampled while "footsw" is at logic high. It stops when "footsw" becomes low. Sensor data is stored in lastsensorVal. The sensor data for the previous step will be added to the next sensor data. The value will continue to add until "leftsw" or "rightsw" is pressed.

You're absolutely right. The totalsensorVal will continue to rise assuming that each step is over 400.

Quote
Third and most importantly, this sounds like a run time operation, unless the user needs recount their steps after they have turned off and on the device, you don't need eeprom. In which case its back to storing this in an array. This is better considering  your sensor value will be over 400. 400 wont fit in the byte you address in eeprom it can only store up to 255.
Can you help me transform my code so it will be stored on an array instead of the EEPROM? I could not find a good link for me to learn storing data in arrays. It would be better if I learn it too. The arduino examples for array don't help. EEPROM read, write, clear are easier to understand.

Quote
I suggest estimating the greatest length of playback(in recording steps) this will help whether you need persistence(eeprom) or not. This way you can pre-define the size of the data structure and be sure the recording fits where you want to put it.
But wait! I think I can do this.
Code:
sensorVal = analogRead(A0) / 1000
The value 400/1000 would just be 0.4
But I will have to change my "int" to "float".
Is this a possible solution?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Given the quantity of data you're looking to store and the limitations of the Arduino, if I were you I'd look for a more efficient encoding scheme than recording the direction as a byte/int. I suggest encoding the directin in as few bits as possible (one bit would be enough for left/right) and pack that in a word with the remaining bits used to hold the count. Depending on the resolution you need, you may be able to scale the count as a 7-bit value and store the direction in the eighth bit, enabling you to fit a sample in a byte. If that's too small, the next option is a 16-bit word, but I'd still suggest encoding your sample carefully to make the best use of it - don't go wasting bits by storing a boolean as a byte.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Your code would not work because even if the sensor value was larger than 400.  As soon as you work that out the new value is bigger than 400 you read the sensor again, get a new value for the sensor value and overwrite the old one.
When looking for a maximum value having an initial absurdly low value is always a good idea as it will soon be updated.
Let me explain how I understand how to get max value so you could point out where I went wrong.
1. I read the sensorVal. 2. Constrain it to a range (330 to 470) so any lower readings than 330 will be 330 and larger reading than 470 will be 470.
3. Map it to a range (which my range for my code is terribly wrong). 4. Compare a value of say "x". Every time it reads a sensor value, it will perform step 2 and 3, then it will compare to "x". Then it will choose which is higher and store it to sensorVal.

Oh yes!!! I got your point now. For the next reading, the sensorVal from the max(sensorVal, 400) will be overwritten.

Quote
Did you get your initial question of saving the switch number and a value saved to an array sorted out ?
No sir. I am having difficulty transforming the pseudo code in C.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Given the quantity of data you're looking to store and the limitations of the Arduino, if I were you I'd look for a more efficient encoding scheme than recording the direction as a byte/int. I suggest encoding the directin in as few bits as possible (one bit would be enough for left/right) and pack that in a word with the remaining bits used to hold the count. Depending on the resolution you need, you may be able to scale the count as a 7-bit value and store the direction in the eighth bit, enabling you to fit a sample in a byte. If that's too small, the next option is a 16-bit word, but I'd still suggest encoding your sample carefully to make the best use of it - don't go wasting bits by storing a boolean as a byte.
Thanks for the advice. This is efficient use of memory. I'll try to think of a way to fit my sensor and switch reading into an 8-bit if possible.
Logged

Manchester NH
Offline Offline
Full Member
***
Karma: 1
Posts: 132
Like to build typing devices, and heard unicorns
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The arduino examples for array don't help. EEPROM read, write, clear are easier to understand.
Picture it this way
Code:
int addressableDataSpace=150// addressable space within your needs
//something like over 9000! would exceed ram size and crash the program
// thats why you need to keep the size of your data in check
//or look into using an sd card

int mySensorArray[addressableDataSpace]={};
//then to manipulate it
int myNewData=400// something within int value space
int address=stepSketchIsAt;
mySensorArray[address]= myNewData;
stepSketchIsAt++;
// to read
for (int i=0;i<addressableDataSpace;i++)
{
readBackToUser=mySensorArray[ i ];
}
works similar eeprom except you can define the value include as any data type you want not just a byte.
« Last Edit: February 05, 2013, 12:15:47 pm by Paul Beaudet » Logged

Manchester NH
Offline Offline
Full Member
***
Karma: 1
Posts: 132
Like to build typing devices, and heard unicorns
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'll try to think of a way to fit my sensor and switch reading into an 8-bit if possible.
If the sensor is measuring the bridth of someones stride you could covert this to you own idea of distance.
ie instead of storing, "my user concatenated the sensor value over 9000!"
your could say once combined sensor values reach 1000 this is 1 pace
then only the fact that your user moved 9 paces needs to be stored and reiterated to them.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int address = 0;
int arraySize = 150;
int myArray[arraySize] = {};
int address = stepSketchIsAt;                        //what is this for?
myArray[address] = lastsensorVal;
stepSketchIsAt++;                                       //and this...
The switch (either leftsw or rightsw is pressed) is not yet included.

« Last Edit: February 05, 2013, 12:30:20 pm by meowth08 » Logged

Manchester NH
Offline Offline
Full Member
***
Karma: 1
Posts: 132
Like to build typing devices, and heard unicorns
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
what is this and this for?
your going to need a way to increment to the next index/address in the array in order to add a new value to it
that part gets really figured out when you build the program
Those were substitutes for results your program will create
expect the index to occur from 0 like so 0,1,2,3,4.

Quote
The switch (either leftsw or rightsw is pressed) is not yet included.
how you implement your program is up to you, that was just an illustration of the concept of an array.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int address = 0;
int arraySize = 150;
int myArray[arraySize] = {};
myArray[address] = lastsensorVal;
address ++;
myArray[address] = 1;
address ++;

I'm a bit sleepy now. 2 am here. I am reviewing for my 2 exams for tomorrow.
Is the above code correct.

If it is, I'll try to sum up every advice/details from this thread and make the necessary corrections by tomorrow after my exams.
Thank you.
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Did you get your initial question of saving the switch number and a value saved to an array sorted out ?
No sir. I am having difficulty transforming the pseudo code in C.
Something (untested) to get you started.
Code:
byte dataArray[200];          //room for 200 entries.  100 switch numbers, 100 data entries
int leftsw = 11;
int rightsw = 10;
int sensorPin = A0;
int sensorData;
int arrayIndex = 0;

void setup()
{
  pinMode(leftsw,INPUT);
  pinMode(rightsw, INPUT);
  pinMode(sensorPin, INPUT);
}

void loop()
{
  if (digitalRead(leftsw) == HIGH)        //left switch pressed
  {
    sensorData = analogRead(sensorPin);   //read sensor
    //constrain and mapping here if required
    dataArray[arrayIndex] = 0;            //save zero to array to indicate left switch pressed
    arrayIndex++;                         //move index to next array position
    dataArray[arrayIndex] = sensorData;   //save sensor data to array
    arrayIndex++;                         //move index to next array position
  }

  if (digitalRead(rightsw) == HIGH)       //right switch pressed
  {
    sensorData = analogRead(sensorPin);   //read sensor
    //constrain and mapping here if required
    dataArray[arrayIndex] = 1;            //save 1 to array to indicate right switch pressed
    arrayIndex++;                         //move index to next array position
    dataArray[arrayIndex] = sensorData;   //save sensor data to array
    arrayIndex++;                         //move index to next array position
  }

  //***************************************************************
  //neater way to do the same thing - DO NOT LEAVE BOTH IN LOOP() !
  //***************************************************************
  if (digitalRead(leftsw) == HIGH)        //left switch pressed
  {
    saveData(0);
  }

  if (digitalRead(leftsw) == HIGH)        //left switch pressed
  {
    saveData(1);
  }
}

//***********************************************************************
void saveData(int swNum)              //function to save switch number and data
{
  sensorData = analogRead(sensorPin);   //read sensor
  //constrain and mapping here if required
  dataArray[arrayIndex] = swNum;            //save switch number
  arrayIndex++;                         //move index to next array position
  dataArray[arrayIndex] = sensorData;   //save sensor data to array
  arrayIndex++;                         //move index to next array position
}
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Manchester NH
Offline Offline
Full Member
***
Karma: 1
Posts: 132
Like to build typing devices, and heard unicorns
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

whenever I sleep on these sort of things, I understand them better in the mourning, seems like you basically got the idea however.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for the code UKHB and Paul B.

Clarification: If I put : int sensorVal=14; I have to put: pinMode (sensorVal, INPUT) at the set-up so pin 14 of arduino is designated as INPUT and named as sensorVal?
I am to continue reviewing now. Two exams are waiting to be perfected by tomorrow.  smiley-lol
« Last Edit: February 05, 2013, 01:20:03 pm by meowth08 » Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for the code UKHB and Paul B.

Clarification: If I put : int sensorVal=14; I have to put: pinMode (sensorVal, INPUT) at the set-up so pin 14 of arduino is designated as INPUT and named as sensorVal?
Correct
The advantages of giving pins names are that you can see more easily what you are doing in your code and if you ever have to change the pin numbers you do it in just one place and not throughout the code.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You got +1 Karma from me. Although I don't know what karma is.  smiley-razz I just think it works like reputation points.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the size of my array is 250.
And only 200 slots hold a data.
Is it correct to say that the remaining slots contain a data equal to "zero"?
Logged

Pages: 1 [2] 3   Go Up
Jump to: