Offline
Newbie
Karma: 0
Posts: 41
|
 |
« on: February 03, 2013, 12:09:13 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #1 on: February 03, 2013, 01:26:13 am » |
urm could you explain it in another way ?
|
|
|
|
|
Logged
|
|
|
|
|
California
Online
Edison Member
Karma: 51
Posts: 2175
|
 |
« Reply #2 on: February 03, 2013, 01:42:46 am » |
Post the code you have using the proper code tags.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1599
May all of your blinks be without delay
|
 |
« Reply #3 on: February 03, 2013, 02:50:58 am » |
Which bit are you having problems with, reading the switches, reading the sensor or storing the data ? In pseudo code 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 ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 41
|
 |
« Reply #4 on: February 03, 2013, 04:59:56 am » |
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. How many entries are there likely to be ? The size is not definite but I think it would not exceed 30. Is it ok? Will the run ever start over again with previous data in the array ? I don't understand this question sir. 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. 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?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #5 on: February 03, 2013, 08:45:49 am » |
Instead of telling us HOW you think you should solve a problem, tell us WHAT the problem is you are trying to solve.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1599
May all of your blinks be without delay
|
 |
« Reply #6 on: February 04, 2013, 12:37:35 pm » |
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. How many entries are there likely to be ? The size is not definite but I think it would not exceed 30. Is it ok? Will the run ever start over again with previous data in the array ? I don't understand this question sir. 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. 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? My pseudo code stored a value for the switch pressed. 0 for switch 1 and 1 for switch 2. The array holds data for 'number of switch pressed' and 'data read' in alternate bytes. I actually had them the in the opposite order than you asked but that is easy to correct. "Will the run ever start over again with previous data in the array ?" What I had in mind was that after a run the array will have data in it. If you run the data collection again without clearing the array it will already have data in it. How will you distinguish old data from new when reading it back if there are fewer entries in the second run ? An entry in the array to signify 'end of data' would be advisable. 30 entries, actually 60 because of collecting the switch numbers, will not be a problem unless the data is a huge string of text, which I take it is not the case. Have you got any code written, even if is not working ? Please post it so that we can help.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 41
|
 |
« Reply #7 on: February 04, 2013, 10:32:40 pm » |
Hi,
@PaulS: I believe I have stated the problem on my posts. That is the very reason why UKHB was able to post a pseudo code.
@UKHB: I will make it and post as soon as possible. Additional info: I'm using an arduino with ATMEGA168 microcontroller.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester NH
Offline
Jr. Member
Karma: 1
Posts: 93
|
 |
« Reply #8 on: February 04, 2013, 11:31:50 pm » |
I'm not really sure what your goals are, but this might give you an idea on how to approach EEPROM you might want to avoid arrays given the EEPROM is writen and read as individual bytes by the standard library included read up on it a bit first though, EEPROM writes are not unlimited #include <EEPROM.h>
byte sensorDataAddress=0; byte switch0Address=1; byte switch1Address=3;
void setup() { //you need to intilize your sensors here }
void loop() { if(sensor== expectedReading) { EEPROM.write(sensorDataAddress, sensordata); sensorDataAddress=sensorDataAddress+3 //there are three readings, so store in every third address spot } //put the switches in the same way if(sensorDataAddress>"3belowYourEEPROMsize" && yourSwitchDataLimitations) { return; //dont write outside the address space of your EEPROM, it will cause issues } } just something to think about
|
|
|
|
« Last Edit: February 04, 2013, 11:37:47 pm by Paul Beaudet »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 41
|
 |
« Reply #9 on: February 05, 2013, 06:32:41 am » |
#include <EEPROM.h>
int sensorVal = 14; int footsw = 12; int leftsw = 11; int rightsw = 10; int resetsw = 9; int returnsw = 8; int audio_out = 7; int addr = 0; int totalsensorVal; int lastsensorVal = 0;
void setup() { 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, 140); sensorVal = max(sensorVal, 400); } if (digitalRead(footsw) == LOW) { totalsensorVal = sensorVal + lastsensorVal; lastsensorVal = totalsensorVal; } if (digitalRead(leftsw) == HIGH) //this part { EEPROM.write(addr, totalsensorVal); addr = addr + 1; EEPROM.write(addr, 1); addr = addr + 1; delay (1000); } if (digitalRead(rightsw) == HIGH) { EEPROM.write(addr, totalsensorVal); addr = addr +1; EEPROM.write(addr, 2); addr = addr +1; delay (1000); } //until this part. if (digitalRead(resetsw) == HIGH) { for (addr = 0; addr < 512; addr ++) EEPROM.write(addr, 0); } if (digitalRead(returnsw) == HIGH) { //to be added } }
Please comment. I tried compiling the code and no errors appeared.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 41
|
 |
« Reply #10 on: February 05, 2013, 06:48:04 am » |
From the code I posted above, I have here several questions. 1. The "int sensorVal = 14;" designates pin 14 of my arduino to sensorVal. Is that right? while (digitalRead(footsw) == HIGH) { sensorVal = analogRead(A0); sensorVal = constrain(sensorVal, 330, 470); sensorVal = map(sensorVal, 330, 470, 0, 140); sensorVal = max(sensorVal, 400); } 2. My aim here is to read the analog voltage while footsw is high. My sensor is the y-axis of the accelerometer. It is connected to the thigh of a person such that when the person walks, the y-axis tilts giving several analog output. A footswitch is installed under the shoe such that when the person lifts his foot, the footswitch is at high logic. I used "while" since I want to get the readings when footsw is at logic high. Range of sensorVal is from 330 until 500. From those readings, I want to get the maximum. Is my code able to do that? 3. I have decided to use the EEPROM instead of array. Is the "this part ....until this part" correct?
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1599
May all of your blinks be without delay
|
 |
« Reply #11 on: February 05, 2013, 07:15:22 am » |
1. The "int sensorVal = 14;" designates pin 14 of my arduino to sensorVal. Is that right? No. Variable sensorVal is set to 14, but you still need to do pinMode(sensorVal, INPUT); and analog/digitalRead(sensorVal); to set up the pin and read values from it. Your method for getting the maximum value is flawed. max(v1,v2) returns the value that is largest. Why 400 in your code ? You need to do something like before the loop set a variable, maybe call it maxValue, to zero start the while loop read the sensor constrain it if necessary map it to a larger range set maxValue to max(maxValue, sensorValue) end of while loop maxValue now holds the maximum value read from the sensor Judging from the names of your variables I assume that there are left and right foot switches. Is that right ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 41
|
 |
« Reply #12 on: February 05, 2013, 07:53:53 am » |
Hi,
Thanks for the clarification on my question 1. I'll make necessary changes.
I just learned coding just after buying the arduino board after looking at the example programs last month. I am not so familiar to pseudo codes but I'm glad I could understand it a bit.
As for getting the max value, I really should map it in a larger range. I used 400 because I assumed 400 should be the lowest value for my sensorVal. (lowest value for a single step)
The project I am to accomplish is a navigation aid for the blind. The blind walks with his cane. If he encounters a "decision point", a point where he need to turn left or right, he presses either "leftsw" if he turns left or "rightsw" if he turns right. This is why I have to record the sensorVal. sensorVal is the sensor reading for every step. From my loop, the totalsensorVal is computed and will be recorded to the EEPROM when he presses leftsw or rightsw. Along with this, I also would want to record the switch being pressed. The footsw is to make sure that the blind took a step.
If the blind wants to go back to the original location, he presses returnsw. This part I will take care later. It's accessing the data I saved on the EEPROM.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester NH
Offline
Jr. Member
Karma: 1
Posts: 93
|
 |
« Reply #13 on: February 05, 2013, 09:38:38 am » |
the blind please, "the user" is less discriminatory, I know people with walking around with cell phones that are less situationally aware then Helen Keller. Maybe they could use your device too. your code: 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. If you write sensor and switch data in sequentially different spots in eeprom this might confuse as to what type of data it is. 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. So you will have to make rule about when to sample the data, which you are kinda doing with the switch. 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. 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.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1599
May all of your blinks be without delay
|
 |
« Reply #14 on: February 05, 2013, 10:16:10 am » |
I used 400 because I assumed 400 should be the lowest value for my sensorVal. (lowest value for a single step)
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. Did you get your initial question of saving the switch number and a value saved to an array sorted out ?
|
|
|
|
|
Logged
|
|
|
|
|
|