View previous topic :: View next topic |
Author |
Message |
zephyr2009
Joined: 06 Aug 2010 Posts: 15
|
PLEASE HELP: Water Level Program |
Posted: Fri Sep 03, 2010 6:01 am |
|
|
Quote: | ////////////////////////////SENSOR FUNCTION////////////////////////////////////
void check_sensor()
{
if (!(input(SENSOR_LEVEL_4))&& (!input(SENSOR_LEVEL_3))&& (!input(SENSOR_LEVEL_2))&& (!input(SENSOR_LEVEL_1))){
water_level = 4;
}
else if ((input(SENSOR_LEVEL_4))&& (!input(SENSOR_LEVEL_3))&& (!input(SENSOR_LEVEL_2))&& (!input(SENSOR_LEVEL_1))){
water_level = 3;
}
else if((input(SENSOR_LEVEL_4))&& (input(SENSOR_LEVEL_3))&& (!input(SENSOR_LEVEL_2))&& (!input(SENSOR_LEVEL_1))){
water_level = 2;
}
else if((input(SENSOR_LEVEL_4))&& (input(SENSOR_LEVEL_3))&& (input(SENSOR_LEVEL_2))&& (!input(SENSOR_LEVEL_1))){
water_level = 1;
}
else{
water_level = 0;
}
} |
is my program correct?
all levels are active high
and all levels have pull down resistor to ground
LEVEL 1 = PORTA1
LEVEL 2 = PORTA2
LEVEL 3 = PORTA3
LEVEL 4 = PORTA4
when there are no switches that are "shorted", it means the desired output is level 0
when level 1 float switch is shorted = level 1
when level 1 and 2 are shorted = level 2
level 1,2, and 3 are shorted = level 3
when level 1,2,3, and 4 are shorted = level 4
are the codes above correct? because my problem is when all switches are "open" the output is LEVEL 4 which is supposedly to output LEVEL 0...
PLEASE HELP ME!! Thanks in advance |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Fri Sep 03, 2010 6:30 am |
|
|
What processor?
Check the data sheet. I think you will find pin A4 is special....open collector. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
Re: PLEASE HELP: Water Level Program |
Posted: Fri Sep 03, 2010 7:06 am |
|
|
zephyr2009 wrote: |
is my program correct?
all levels are active high
and all levels have pull down resistor to ground
when there are no switches that are "shorted", it means the desired output is level 0
when level 1 float switch is shorted = level 1
when level 1 and 2 are shorted = level 2
level 1,2, and 3 are shorted = level 3
when level 1,2,3, and 4 are shorted = level 4
|
I think your logic is worng n your code. You have pull downs on the pins so the other side of the switch must be VDD.
So your truth table is:-
Code: |
water L1 L2 L3 L4
0 0 0 0 0
1 1 0 0 0
2 1 1 0 0
3 1 1 1 0
4 1 1 1 1
|
Your code is opposite to this:-
Code: |
void check_sensor()
{
if (!(input(SENSOR_LEVEL_4))&& (!input(SENSOR_LEVEL_3))&& (!input(SENSOR_LEVEL_2))&& (!input(SENSOR_LEVEL_1))){
water_level = 4;
}
else if ((input(SENSOR_LEVEL_4))&& (!input(SENSOR_LEVEL_3))&& (!input(SENSOR_LEVEL_2))&& (!input(SENSOR_LEVEL_1))){
water_level = 3;
}
else if((input(SENSOR_LEVEL_4))&& (input(SENSOR_LEVEL_3))&& (!input(SENSOR_LEVEL_2))&& (!input(SENSOR_LEVEL_1))){
water_level = 2;
}
else if((input(SENSOR_LEVEL_4))&& (input(SENSOR_LEVEL_3))&& (input(SENSOR_LEVEL_2))&& (!input(SENSOR_LEVEL_1))){
water_level = 1;
}
else{
water_level = 0;
}
}
|
The ! means not or false so if L4 and L3 and L2 and L1 are all false (0) then you set water_level = 4.
This does not match your description.
Change code to
Code: |
void check_sensor()
{
if (input(SENSOR_LEVEL_4)&& input(SENSOR_LEVEL_3) && input(SENSOR_LEVEL_2) && input(SENSOR_LEVEL_1)) {
water_level = 4;
}
else if (input(SENSOR_LEVEL_3) && !input(SENSOR_LEVEL_2) && input(SENSOR_LEVEL_1)) {
water_level = 3;
}
else if (input(SENSOR_LEVEL_2) && input(SENSOR_LEVEL_1)) {
water_level = 2;
}
else if (input(SENSOR_LEVEL_1)) {
water_level = 1;
}
else {
water_level = 0;
}
}
|
You may notice you don't have to check each one as the previous condition rules out the logic. Unless there is the possibility of incorrect readings. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Sep 03, 2010 7:14 am |
|
|
you can simplify this because you are using pins A1, A2, A3 and A4
Code: |
int8 val;
val = (input_a() >> 1) & 0x0F; // Get the input and adjust for A1 to A4
if (val & 0x0f) // L4 = 1, L3 = 1, L2 = 1, L1 = 1
water_level = 4;
else if (val & 0x07) // L3 = 1, L2 = 1, L1 = 1
water_level = 3;
else if (val & 0x03) // L2 = 1, L1 = 1
water_level = 2;
else if (val & 0x01) // L1 = 1
water_level = 1;
else
water_level = 0;
|
I think. |
|
|
zephyr2009
Joined: 06 Aug 2010 Posts: 15
|
|
Posted: Fri Sep 03, 2010 9:14 am |
|
|
Code: | water L1 L2 L3 L4
0 0 0 0 0
1 1 0 0 0
2 1 1 0 0
3 1 1 1 0
4 1 1 1 1 |
Thank you sir wayne for replying...
Your right sir
my truth table is just like that...
I will try your codes with respect to my codes
I hope that it would work.. |
|
|
zephyr2009
Joined: 06 Aug 2010 Posts: 15
|
|
Posted: Fri Sep 03, 2010 11:08 am |
|
|
Code: | else if (input(SENSOR_LEVEL_3) && !input(SENSOR_LEVEL_2) && input(SENSOR_LEVEL_1)) {
water_level = 3;
} |
Good day sir..
It is a great code..
My water level works fine!
I noticed that your code && !input(SENSOR_LEVEL_3) has a ! (not) and i deleted it to make my level 3 = 1 1 1 0 as the truth table
Code: | else if (input(SENSOR_LEVEL_3) && input(SENSOR_LEVEL_2) && input(SENSOR_LEVEL_1)) {
water_level = 3;
} |
Thank you very much sir wayne..
Hope that you could still help me if i found another problem |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Sep 06, 2010 1:52 am |
|
|
Glad to help, that ! must have been a typo |
|
|
|