CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PLEASE HELP: Water Level Program

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
zephyr2009



Joined: 06 Aug 2010
Posts: 15

View user's profile Send private message

PLEASE HELP: Water Level Program
PostPosted: Fri Sep 03, 2010 6:01 am     Reply with quote



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: 1931
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Sep 03, 2010 6:30 am     Reply with quote

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

View user's profile Send private message

Re: PLEASE HELP: Water Level Program
PostPosted: Fri Sep 03, 2010 7:06 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Sep 03, 2010 7:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Sep 03, 2010 9:14 am     Reply with quote

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 Smile
I hope that it would work.. Smile
zephyr2009



Joined: 06 Aug 2010
Posts: 15

View user's profile Send private message

PostPosted: Fri Sep 03, 2010 11:08 am     Reply with quote

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.. Smile
My water level works fine! Smile

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 Smile
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Sep 06, 2010 1:52 am     Reply with quote

Glad to help, that ! must have been a typo Embarassed
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group