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