store sensor values into an array

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.

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.

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.

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.

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.

meowth08:

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.

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 
}

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

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. XD

meowth08:
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.

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

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"?

//********************************************//
//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.

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.

    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.

if (step has been recorded)
{
  do stuff
}

@UKHB,

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

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()?

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.

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?

meowth08:
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.

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.

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.

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.