Pages: 1 2 [3]   Go Down
Author Topic: store sensor values into an array  (Read 2155 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

Code:
//********************************************//
//NAVIGATION SYSTEM FOR THE VISUALLY IMPAIRED //
//********************************************//

int sensorVal = 14;
int  footsw = 12;
int  leftsw = 11;
int  rightsw = 10;
int  resetsw = 9;
int  returnsw = 8;
int audio_out = 7;
int arrayIndex = 0;
int maxsensorVal = 0;
int lastsensorVal = 0;
int Error;
int totalsensorVal;
byte dataArray[250];

void setup()

{
  Serial.begin(9600);
 
  pinMode(sensorVal, INPUT);
  pinMode(footsw, INPUT);
  pinMode(leftsw, INPUT);
  pinMode(rightsw, INPUT);
  pinMode(returnsw, INPUT);
  pinMode(resetsw, INPUT);
}

void loop()

{
while (digitalRead(footsw) == HIGH)
  {
    sensorVal = analogRead(A0);
    sensorVal = constrain(sensorVal, 330, 470);
    sensorVal = map(sensorVal, 330, 470, 0, 560);
    maxsensorVal = max(maxsensorVal, sensorVal);
  }
if (digitalRead(footsw) == LOW)
  {
    totalsensorVal = maxsensorVal + lastsensorVal;
    lastsensorVal = totalsensorVal;
    maxsensorVal = 0;
  }
if (digitalRead(leftsw) == HIGH)
  {
    if (dataArray[0] == 0)
      {
        Error = 1;
      }
    else
      {
        dataArray[arrayIndex] = 1;
        arrayIndex ++;
        dataArray[arrayIndex] = totalsensorVal;
        arrayIndex ++;
        maxsensorVal = 0;
        lastsensorVal = 0;
      }
  }
if (digitalRead(rightsw) == HIGH)
  {
    if (dataArray[0] == 0)
      {
        Error = 1;
      }
    else
      {
        dataArray[arrayIndex] = 2;
        arrayIndex ++;
        dataArray[arrayIndex] = totalsensorVal;
        arrayIndex ++;
        maxsensorVal = 0;
        lastsensorVal = 0;
      }
  }
if (Error == 1)
  {
    //do something here
  }
if (digitalRead(resetsw) == HIGH)
  {
    for (arrayIndex = 0; arrayIndex < 250; arrayIndex ++)
    {
      dataArray[arrayIndex] = 0;
      arrayIndex = 0;
      maxsensorVal = 0;
      lastsensorVal = 0;
    }
  }
if (digitalRead(returnsw) == HIGH)
  {
    //to be added
  }
}

I have modified my post.
Please comment if you still see some flaws or if you have suggestions.
« Last Edit: February 06, 2013, 07:33:39 am by meowth08 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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"?
Not unless you explicitly make it so.
Logged

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

Code:
    if (dataArray[0] == 0)
      {
        //an error. no step has been recorded yet.
        //i need to get out of here and go to start. i can't use "break;" it's only for do, for, while, switch.. :(
        //What other options are possible???
      }
Refer to the recent reply.  How do you know that dataArray[0] will contain 0 if no step has been recorded ?

"What other options are possible???"
Change the logic of the if test.
Code:
if (step has been recorded)
{
  do stuff
}
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

@UKHB,

I have already figured out how to do it by: if else. Is it correct?

Quote
Not unless you explicitly make it so.

Then I have to use for loop to put 0's on each array index.
I cannot put it in the loop.
Is it ok to put it after the void setup() before the void loop()?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is it ok to put it after the void setup() before the void loop()?
No. Executable code needs to be inside a function. If the array you are referring to is global, it is, by default, initialized to 0. If it is rewritten regularly, with different amounts of data, you need to reinitialized it to all 0s before you start writing new data to it.
Logged

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

Going back to my question on reply # 31.
If only 200 of the 250 slots have data.
If I read the data on the 201st slot, what value would I be able to read?

Another question.
If executable codes need to be inside a function, are there other functions where I can put my code into?
Logged

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

Going back to my question on reply # 31.
If only 200 of the 250 slots have data.
If I read the data on the 201st slot, what value would I be able to read?

All of your slots have data.

If you have written to them previously, the value is whatever you last wrote there. If a slot has never been written to, its value will be whatever value it was initialised to. For global data this will be zero, unless you explicitly initialised it to a different value. For local or heap data, this will be whatever arbitrary value the memory location held previously. If you want unused slots to hold a specific value, you need to put it there.
Logged

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Another question.
If executable codes need to be inside a function, are there other functions where I can put my code into?
Any function(s) you create can contain executable code.
Logged

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

Can you give me some examples please.

Forgive me for asking so much. I'm really new in here. I'm trying my best to learn from the experts.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have examples. setup() and loop() are user functions. All that is needed is to specify a return type, a function name, any arguments (type and name) and a set up curly braces.

Defining a function for the Arduino is NO different from defining a function for a PC application.

It sounds like you really need a good (or even bad) C book and to learn some fundamentals.
Logged

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

An example for you
Code:
byte anArray[10];  //array of 10 bytes.  First byte is in anArray[0].  Last byte is in anArray[9]

void setup()
{
  initArray();    //call the function to initialise the array
}

void loop()
{
  //do stuff here that writes and reads the array
  //when you need to initialise it call initArray again
  initArray();
  //NOTE - in this example it is called every time loop() runs
  //in practice you will only call it when you need to start
  //storing values again from the beginning
}

void initArray()        //a function to fill the array with zeroes
{
  for (int index = 0;index <= 9;index++)  //10 entries to fill numbered 0 to 9 
  {
    anArray[index] = 0;
  }
}
As Paul has suggested, read up on and try out some of the fundamentals
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

Thanks for presenting the example.

I only have this as my reference.
file:///C:/Program%20Files/ARDUINO/arduino-1.0.3/reference/index.html
This is where I go back from time to time to check what the codes are for.
For some of my questions where answers could not be found here, I ask it in the forum.
Fortunately I get good replies
Logged

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

Have you tried and understood the example code in the IDE ?
When I got the Arduino I found the tutorials by Jeremy Blum on YouTube very helpful but don't just watch them, enter the code he uses, run it and modify it to see what happens. 

His tutorial code is downloadable but it is much better in my opinion to enter it yourself and sort out any problems that arise.  Often it is just a case of a missing semi-colon (or an extra one), brackets in the wrong place or wrong kind of bracket in the right place, but sorting out errors is very instructive and you will need to do it with your own code anyway.
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: 131
Like to build typing devices, and heard unicorns
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I followed this when I started.
http://sheepdogguides.com/arduino/FA1main.htm
Some may not like his "style" but these helped me understand functions among other things
Logged

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