Society of Robots - Robot Forum
Software => Software => Topic started by: schiz0id on June 03, 2010, 05:42:14 PM
-
Hey all! I used to be on this forum as "geek1", then took a hiatus from robotics, and uh, here I am again! I have a question. My brother has issues sleepwalking. He has an alarm to alert him when he gets up--but it relies on a pressure sensor which isn't comfortable. I am attempting to build an alarm which uses an IR range sensor to sense when he gets up, then sounds a beeper. I am currently doing my source code experiments using an LED. I can't get it to work--the code follows.
int irReader = 1; // the analog input pin for the ir reader
int ledPin = 13;
int irVal = 0; // stores value from Ir reader
int irvalarr[23] = {286, 275, 263, 248, 222, 213, 154, 131, 150, 140, 138, 134,
138, 140, 150, 131, 154, 213, 222, 248, 263, 275, 286};
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // begins serial communication with the computer
}
void loop(int servtime) {
irVal = analogRead(irReader); // read the value from the ir sensor
if (irVal <= irvalarr) {
digitalWrite (ledPin, HIGH);
}
Serial.println(irVal); // sends ir val to computer
}
-
It's because you initialize irvalarr as an array, and then compare it as if it where a number.
Change
int irvalarr[23] = {286, 275, 263, 248, 222, 213, 154, 131, 150, 140, 138, 134,
138, 140, 150, 131, 154, 213, 222, 248, 263, 275, 286};
to
int irvalarr = 512; //or some single number representing the alarm point
-
Thank you, however I have a new problem. Updated code:
int irReader = 1; // the analog input pin for the ir reader
int ledPin = 12;
int irVal = 0; // stores value from Ir reader
int irvalarr = 512;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // begins serial communication with the computer
}
void loop() {
irVal = analogRead(irReader); // read the value from the ir sensor
if (irVal <= irvalarr) {
digitalWrite (ledPin, HIGH);
}
else (irVal >= irvalarr); {
digitalWrite (ledPin, LOW);
}
Serial.println (irVal);
}
I have also tried it as
int irReader = 1; // the analog input pin for the ir reader
int ledPin = 12;
int irVal = 0; // stores value from Ir reader
int irvalarr = 512;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // begins serial communication with the computer
}
void loop() {
irVal = analogRead(irReader); // read the value from the ir sensor
if (irVal <= irvalarr) {
digitalWrite (ledPin, HIGH);
}
Serial.println (irVal);
}
As the first one, it won't turn on. As the second, it won't turn off. Why is this? I've tried moving my hand back and forth dozens of times in front of the sensor and still no response from the LED.
-
Try this:
int irReader = 1; // the analog input pin for the ir reader
int ledPin = 12;
int irVal = 0; // stores value from Ir reader
int irvalarr = 512;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // begins serial communication with the computer
}
void loop() {
irVal = analogRead(irReader); // read the value from the ir sensor
if (irVal <= irvalarr)
digitalWrite (ledPin, HIGH);
else
digitalWrite (ledPin, LOW);
Serial.println (irVal);
delay(100);
}
Remember, you might have to adjust the irvalarr number to get it to work.
-
Try this:
int irReader = 1; // the analog input pin for the ir reader
int ledPin = 12;
int irVal = 0; // stores value from Ir reader
int irvalarr = 512;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // begins serial communication with the computer
}
void loop() {
irVal = analogRead(irReader); // read the value from the ir sensor
if (irVal <= irvalarr)
digitalWrite (ledPin, HIGH);
else
digitalWrite (ledPin, LOW);
Serial.println (irVal);
delay(100);
}
Remember, you might have to adjust the irvalarr number to get it to work.
That solved it--although an interesting side note is that it actually needed to be if (irVal >= irvalarr)
-
Actually, one last question. Do you happen to know the maximum input value for a Sharp IR rangefinder? I realized it would work better if the alarm could be tripped by stepping anywhere in the beam at all--and thus wanted to set the threshold as high as possible.
-
I'm sorry--LAST question. I'm having a new problem. The alarm is periodically going off--with nothing tripping it. Any ideas?
Thanks, maybe I'll move this to another thread.
-
The Sharp IR isn't perfect, there may be the occasional spike that triggers it. You could figure out a kind of moving-average system to reduce the effect of random spikes like that.
To help you with the "maximum value" we need to know what model Sharp IR you are using, since there are several.
-
Hi,
I'm sorry--LAST question. I'm having a new problem. The alarm is periodically going off--with nothing tripping it. Any ideas?
Did you consider other (simpler) sensors, like whether there's weight (his) on his bed?
Whichever sensor, you'd do well in averaging over time and only sound the alarm (light the LED) when you have had an alarm condition for perhaps 1 or 2 seconds (wouldn't stop it from working, as he's sleepwalking, not sleeprunning after all).
A capacitive sensors would be another option that won't be a nuisance to your brother, it can be made for next to nothing and your brother won't feel the sensor parts at all - and it only needs a controller if you absolutely want to include it, as simple CMOS gates will do nicely with no programming to go wrong.
-
You seem to overestimeate me. As I said in my initial post, about a year or two ago I would have understood all of this, but I sort of didn't do anything hardware-related at all during that time, and as such am insanely rusty.
1) How would I program averaging over time? and
2) Define "capacitave sensor"?
Also, I don't know how easy it would be to attach a weight sensor to the bed.
Hi,
I'm sorry--LAST question. I'm having a new problem. The alarm is periodically going off--with nothing tripping it. Any ideas?
Did you consider other (simpler) sensors, like whether there's weight (his) on his bed?
Whichever sensor, you'd do well in averaging over time and only sound the alarm (light the LED) when you have had an alarm condition for perhaps 1 or 2 seconds (wouldn't stop it from working, as he's sleepwalking, not sleeprunning after all).
A capacitive sensors would be another option that won't be a nuisance to your brother, it can be made for next to nothing and your brother won't feel the sensor parts at all - and it only needs a controller if you absolutely want to include it, as simple CMOS gates will do nicely with no programming to go wrong.
-
Hi,
You seem to overestimeate me. As I said in my initial post, about a year or two ago I would have understood all of this, but I sort of didn't do anything hardware-related at all during that time, and as such am insanely rusty.
It's like riding a bike, a few trips to the ER and you're back ;)
1) How would I program averaging over time? and
It can be done several ways, but here's the rough outline:
Each time you take a sample, you store it in an array.
Then you add each element of the buffer and divide with the number of elements it holds.
Finally, you compare the result with a constant that defines something around the middle value of the full on and full off values. (and then either shift all elements of the array, dropping the "oldest" value, OR use a ring buffer and just update the "put in" and "take out" pointers).
2) Define "capacitave sensor"?
A sensor that senses changes in capacity ;D
One way to produce it would be:
An oscillator running at eg. 1..2 MHz connected to a wire or a piece of foil under the sheets around upper torso height and a wire/foil around the height of the pelvic area going to a receiver.
That way, the signal will travel through your brother when he's there and will appear at the receiver.
One possible disadvantage is if he sweats a lot, it may seem like he's still there when he isn't
'Then oscillator and receiver can be connected through two small caps and a wire from their common node going under the wire (adjusted to positively detect when your brother grounds the weak signal capacitively.
I can redo a circuit for it, if you need it
Also, I don't know how easy it would be to attach a weight sensor to the bed.
It doesn't have to be anything fancy, just he's there/he's gone, so a microswitch under a bedspring or board can be used, just mount the microswitch so it is off when there's no weight on the bed (and the mount should be able to give of course)
Both methods work. I have tried both of them as part of a sleep pattern study. Simple does it.
-
The Sharp IR isn't perfect, there may be the occasional spike that triggers it. You could figure out a kind of moving-average system to reduce the effect of random spikes like that.
To help you with the "maximum value" we need to know what model Sharp IR you are using, since there are several.
2Y0A02
-
Hi,
You seem to overestimeate me. As I said in my initial post, about a year or two ago I would have understood all of this, but I sort of didn't do anything hardware-related at all during that time, and as such am insanely rusty.
It's like riding a bike, a few trips to the ER and you're back ;)
1) How would I program averaging over time? and
It can be done several ways, but here's the rough outline:
Each time you take a sample, you store it in an array.
Then you add each element of the buffer and divide with the number of elements it holds.
Finally, you compare the result with a constant that defines something around the middle value of the full on and full off values. (and then either shift all elements of the array, dropping the "oldest" value, OR use a ring buffer and just update the "put in" and "take out" pointers).
2) Define "capacitave sensor"?
A sensor that senses changes in capacity ;D
One way to produce it would be:
An oscillator running at eg. 1..2 MHz connected to a wire or a piece of foil under the sheets around upper torso height and a wire/foil around the height of the pelvic area going to a receiver.
That way, the signal will travel through your brother when he's there and will appear at the receiver.
One possible disadvantage is if he sweats a lot, it may seem like he's still there when he isn't
'Then oscillator and receiver can be connected through two small caps and a wire from their common node going under the wire (adjusted to positively detect when your brother grounds the weak signal capacitively.
I can redo a circuit for it, if you need it
Also, I don't know how easy it would be to attach a weight sensor to the bed.
It doesn't have to be anything fancy, just he's there/he's gone, so a microswitch under a bedspring or board can be used, just mount the microswitch so it is off when there's no weight on the bed (and the mount should be able to give of course)
Both methods work. I have tried both of them as part of a sleep pattern study. Simple does it.
A circuit would help.