Thread: Water level sensor

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    33

    Water level sensor

    Hi, I am doing water level sensor. I want to display 3 different values in my 7 segment. For example when the water level reaches 10cm which is the alert(A) the 7 segment also need to show 10,when the water level reaches 20cm which is the warning(W) the 7 segment also need to show 20 and when the water level reaches 30cm which is the emergency(E)the 7 segment also need to show 30. But right now with the below code it can only shows the emergency level(E) where the water level reaches 30cm the 7 segment also show 30 but cannot display for other 2 levels. The code uses C progremming. What are the changes need to be made in the code. Please respond to me as soon as possible. Thank You.

    Appendix C—Water Level Sensor Alarming

    Code:
     
    
    #include "msp430x22x4.h"
    
    #include "mrfi.h"
    
    #define MRFI_RADIO_FAMILY1
    
    #define P3_set_out P2DIR |=0x01;//set Pin 3 and pin 4 to output
    
    #define P4_set_out P2DIR |=0x02;
    
     
    
    #define a_set_out P2DIR |=0x04;//set P2.2 output direction 
    
    #define b_set_out P2DIR |=0x08;//set P2.3 output direction 
    
    #define c_set_out P2DIR |=0x10;//set P2.4 output direction 
    
    #define d_set_out P4DIR |=0x08;//set P4.3 output direction 
    
    #define e_set_out P4DIR |=0x10;//set P4.4 output direction 
    
    #define f_set_out P4DIR |=0x20;//set P4.5 output direction 
    
    #define g_set_out P4DIR |=0x40;//set P4.6 output direction 
    
     
    
    #define a_on P2OUT |=0x04;
    
    #define a_off P2OUT &=~0x04;
    
    #define b_on P2OUT |=0x08;
    
    #define b_off P2OUT &=~0x08;
    
    #define c_on P2OUT |=0x10;
    
    #define c_off P2OUT &=~0x10;
    
    #define d_on P4OUT |=0x08;
    
    #define d_off P4OUT &=~0x08;
    
    #define e_on P4OUT |=0x10;
    
    #define e_off P4OUT &=~0x10;
    
    #define f_on P4OUT |=0x20;
    
    #define f_off P4OUT &=~0x20;
    
    #define g_on P4OUT |=0x40;
    
    #define g_off P4OUT &=~0x40;
    
    int num,num_1,num_2,r_1,count=0;
    
    void main(void)
    
    {
    
      
    
      WDTCTL = WDTPW + WDTHOLD;
    
      BSP_Init();
    
      P1REN|=0x04;
    
      P1IE|=0x04;
    
      MRFI_Init();//initalize 6 pin wire connection between cc2500 & msp430
    
      MRFI_WakeUp();//wakes radio up
    
      MRFI_RxOn();//turn radio into receiving mode
    
      
    
      TBCTL=TBSSEL_2+MC_1+ID_3;
    
      TBCCTL0=CCIE;
    
      TBCCR0=150;
    
      __bis_SR_register(LPM0_bits+GIE);
    
      
    
      
    
      
    
      P1DIR |= 0x03; // P1DIR = P1DIR | 0x03 Set P1.0, P1.1 to output direction
    
      P1OUT &= ~0x03;
    
     
    
     
    
      /*TBCTL=TBSSEL_2+MC_1+ID_3;
    
      TBCCTL0=CCIE;
    
      TBCCR0=150;*/
    
      __bis_SR_register(GIE);
    
      
    
      for(;;)
    
      {
    
       
    
        
    
      }
    
    }
    
     
    
     
    
     
    
     
    
    void MRFI_RxCompleteISR()
    
    {
    
      mrfiPacket_t packet;
    
      MRFI_Receive(&packet);
    
      if(packet.frame[9]=='A')
    
      {
    
        
    
        P1OUT |= 0x01;
    
        P1OUT &=~0x02;
    
        P2OUT &=~0x03;
    
        P4DIR |=0x20;
    
        P4OUT |=0x20;
    
        packet.frame[9]=' ';
    
      }
    
      else if(packet.frame[9]=='W')
    
      {
    
        
    
        P1OUT &= ~0x01;
    
        P1OUT |= 0x02;
    
        P2OUT &=~0x03;
    
        P4DIR |=0x20;
    
        P4DIR |=0x40;
    
        P4OUT |=0x20;
    
        P4OUT |=0x40;
    
       
    
        packet.frame[9]=' ';
    
        
    
      }
    
      else if(packet.frame[9]=='E')
    
      {
    
        P1OUT |= 0x01;
    
        P1OUT |= 0x02;
    
        P4DIR |=0x20;
    
        P4DIR |=0x40;
    
        P4OUT |=0x20;
    
        P4OUT |=0x40;
    
        
    
        
    
        P3_set_out;
    
        P4_set_out;
    
      
    
      
    
        a_set_out;//set P2.2->segment A as output pin
    
        b_set_out;//set P2.3->segment B as output pin
    
        c_set_out;//set P2.4->segment C as output pin
    
        d_set_out;//set P4.3->segment D as output pin
    
        e_set_out;//set P4.4->segment E as output pin
    
        f_set_out;//set P4.5->segment F as output pin
    
        g_set_out;//set P4.6->segment G as output pin
    
      
    
        num_1=(30/10);
    
        r_1=30%10;
    
        num_2=r_1/1;
    
        if(count>=0&&count<=4)
    
        {
    
          P2OUT &=~0x03;
    
          P2OUT |=0x01;
    
          num=num_1;
    
        }
    
        if(count>=5&&count<=10)
    
        {
    
          P2OUT &=~0x03;
    
          P2OUT |=0x02;
    
          num=num_2;
    
        }
    
        
    
        packet.frame[9]=' ';
    
         switch(num)
    
        {
    
        case 0:
    
          a_on;
    
          b_on;
    
          c_on;
    
          d_on;
    
          e_on;
    
          f_on;
    
          g_off;
    
          break;
    
        
    
        case 1:
    
          a_off;
    
          b_on;
    
          c_on;
    
          d_off;
    
          e_off;
    
          f_off;
    
          g_off;
    
          break;
    
        
    
        case 2:
    
          a_on;
    
          b_on;
    
          c_off;
    
          d_on;
    
          e_on;
    
          f_off;
    
          g_on;
    
          break;
    
        
    
        case 3:
    
          a_on;
    
          b_on;
    
          c_on;
    
          d_on;
    
          e_off;
    
          f_off;
    
          g_on;
    
          break;
    
        
    
        case 4:
    
          a_off;
    
          b_on;
    
          c_on;
    
          d_off;
    
          e_off;
    
          f_on;
    
          g_on;
    
          break;
    
        
    
        case 5:
    
          a_on;
    
          b_off;
    
          c_on;
    
          d_on;
    
          e_off;
    
          f_on;
    
          g_on;
    
          break;
    
        
    
        case 6:
    
          a_on;
    
          b_off;
    
          c_on;
    
          d_on;
    
          e_on;
    
          f_on;
    
          g_on;
    
          break;
    
        
    
        case 7:
    
          a_on;
    
          b_on;
    
          c_on;
    
          d_off;
    
          e_off;
    
          f_off;
    
          g_off;
    
          break;
    
        
    
        case 8:
    
          a_on;
    
          b_on;
    
          c_on;
    
          d_on;
    
          e_on;
    
          f_on;
    
          g_on;
    
          break;
    
        
    
        case 9:
    
          a_on;
    
          b_on;
    
          c_on;
    
          d_off;
    
          e_off;
    
          f_on;
    
          g_on;
    
          break;
    
        }
    
      }
    
      else if(packet.frame[9]=='X')
    
      {
    
        //P1OUT |= 0x02;
    
        P1OUT &=~0x01;
    
        P4OUT &=~0x20;
    
        P4OUT &=~0x40;
    
        packet.frame[9]=' ';
    
        
    
      }
    
      else if(packet.frame[9]=='Y')
    
      {
    
        //P1OUT |= 0x02;
    
        P1OUT &=~0x01;
    
        P4OUT &=~0x20;
    
        P4OUT &=~0x40;
    
        packet.frame[9]=' ';
    
      }
    
      else if(packet.frame[9]=='Z')
    
      {
    
        //P1OUT |= 0x02;
    
        P1OUT &=~0x01;
    
        P4OUT &=~0x20;
    
        P4OUT &=~0x40;
    
        packet.frame[9]=' ';
    
      }
    
    }
    
    #pragma vector =PORT1_VECTOR 
    
    __interrupt void Port_1(void)
    
    {
    
      P1IFG&=~0x04;
    
      
    
      //BUZZER
    
    }
    
    #pragma vector=TIMERB0_VECTOR 
    
    __interrupt void TB0_ISR(void)
    
    {  
    
     
    
     if (count == 10)
    
      {    
    
        count = 0;
    
      }
    
      else
    
      {
    
        count++;
    
      }
    
    }

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    While I have limited micro controller experience and my C is rusty (just getting back into the swing) - I think you'll find you need to analyse the code within the following block to find the answer:

    Code:
    else if(packet.frame[9]=='E')
    {
            //Your answer is in here.
    }
    I can already pick where the number is calculated and the logic used to apply it to the 7-seg. It's been way to long since I've bit-banged so I'm not going to try to work it out, but I also feel that I would be doing your homework for you if I did. That said, I can see that this could do with some significant refactoring once you're done.


    [EDIT]

    After looking at this for a few more minutes, I could write the code to do this - it is elementary. Copy/Paste and change 2 lines of code each time and you're done. I assume this example will increase in complexity in future, but if you can't work out this first step - you need to do a lot more studying/practice.
    Last edited by bivhitscar; 06-21-2011 at 11:06 PM. Reason: Enlightenment
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Are you sure you don't need brackets in your #defines?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > MRFI_WakeUp();//wakes radio up
    > MRFI_RxOn();//turn radio into receiving mode
    I wonder what any of this has to do with "water level sensing".
    In fact, the whole code reads like it's controlling some kind of simple radio, and is nothing at all to do with the problem.

    Mostly, it just looks like another dumb "here's my assignment, and here's some random code I found along the way".

    > Appendix C—Water Level Sensor Alarming
    So I'm guessing you just copy/pasted it from the "book", along with it's description of what it apparently "does already", without bothering to even read it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Salem, you really should be more formal until you know what people are posting. Hahahahaha, sorry I couldn't resist.

    Anyway, what book is this? Seems interesting.

  6. #6
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by Salem View Post
    > MRFI_WakeUp();//wakes radio up
    > MRFI_RxOn();//turn radio into receiving mode
    I wonder what any of this has to do with "water level sensing".
    In fact, the whole code reads like it's controlling some kind of simple radio, and is nothing at all to do with the problem.
    Nah, looks to me like it's just initialising a receiver then reading off whatever it gets sent:

    Code:
    void MRFI_RxCompleteISR()
    {
      mrfiPacket_t packet;
      MRFI_Receive(&packet);
      if(packet.frame[9]=='A')
      ...
    }


    Quote Originally Posted by Salem View Post
    Mostly, it just looks like another dumb "here's my assignment..."
    This, this I agree with.


    [EDIT]

    Wow, that was hard to find (seriously) - http://focus.ti.com/mcu/docs/mcuorph...ntentId=120687
    Last edited by bivhitscar; 06-22-2011 at 12:35 AM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    33
    I have done the code for my projectn and i have not copy from any books. Pls don hurt other peoples hardwork by just saying anyhow. I am just asking a help to solve my problem. If you can help on my project pls help i really don know how to solve the problem.

  8. #8
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    If you wrote the code that is in your original post, then you shouldn't have any trouble making the two 7-segs display 20 for W and 10 for A.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    33
    I have tried out but the 10 and 20 only shows when the water level reaches 30cm, but i want it to show for every level individually. Pls bivhitscar help me.

  10. #10
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Could you paste the code where you try to make 10 and 20 work?
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    33
    Code:
    void MRFI_RxCompleteISR()
    {
      mrfiPacket_t packet;
      MRFI_Receive(&packet);
      if(packet.frame[9]=='A')
      {
        P1OUT |= 0x01;
        P1OUT &=~0x02;
        P2OUT &=~0x03;
        P4DIR |=0x20;
        P4OUT |=0x20;
        packet.frame[9]=' ';
        
        a_set_out;//set P2.2->segment A as output pin
        b_set_out;//set P2.3->segment B as output pin
        c_set_out;//set P2.4->segment C as output pin
        d_set_out;//set P4.3->segment D as output pin
        e_set_out;//set P4.4->segment E as output pin
        f_set_out;//set P4.5->segment F as output pin
        g_set_out;//set P4.6->segment G as output pin
        
        num_1=(20/10);
        r_1=20%10;
        num_2=r_1/1;
        if(count>=0&&count<=4)
        {
          P2OUT &=~0x03;
          P2OUT |=0x01;
          num=num_1;
        }
        if(count>=5&&count<=10)
        {
          P2OUT &=~0x03;
          P2OUT |=0x02;
          num=num_2;
        }
        packet.frame[9]=' ';
        switch(num)
        {
        case 0:
          a_on;
          b_on;
          c_on;
          d_on;
          e_on;
          f_on;
          g_off;
          break;
        case 1:
          a_off;
          b_on;
          c_on;
          d_off;
          e_off;
          f_off;
          g_off;
          break;
        case 2:
          a_on;
          b_on;
          c_off;
          d_on;
          e_on;
          f_off;
          g_on;
          break;
        case 3:
          a_on;
          b_on;
          c_on;
          d_on;
          e_off;
          f_off;
          g_on;
          break;
        case 4:
          a_off;
          b_on;
          c_on;
          d_off;
          e_off;
          f_on;
          g_on;
          break;
        case 5:
          a_on;
          b_off;
          c_on;
          d_on;
          e_off;
          f_on;
          g_on;
          break;
    
        case 6:
          a_on;
          b_off;
          c_on;
          d_on;
          e_on;
          f_on;
          g_on;
          break;
        case 7:
          a_on;
          b_on;
          c_on;
          d_off;
          e_off;
          f_off;
          g_off;
          break;
        case 8:
          a_on;
          b_on;
          c_on;
          d_on;
          e_on;
          f_on;
          g_on;
          break;
        case 9:
          a_on;
          b_on;
          c_on;
          d_off;
          e_off;
          f_on;
          g_on;
          break;
        }
      }
      else if(packet.frame[9]=='W')
      {
        P1OUT &= ~0x01;
        P1OUT |= 0x02;
        P2OUT &=~0x03;
        P4DIR |=0x20;
        P4DIR |=0x40;
        P4OUT |=0x20;
        P4OUT |=0x40;
        packet.frame[9]=' ';
        
        a_set_out;//set P2.2->segment A as output pin
        b_set_out;//set P2.3->segment B as output pin
        c_set_out;//set P2.4->segment C as output pin
        d_set_out;//set P4.3->segment D as output pin
        e_set_out;//set P4.4->segment E as output pin
        f_set_out;//set P4.5->segment F as output pin
        g_set_out;//set P4.6->segment G as output pin
        
        num_1=(10/10);
        r_1=10%10;
        num_2=r_1/1;
        if(count>=0&&count<=4)
        {
          P2OUT &=~0x03;
          P2OUT |=0x01;
          num=num_1;
        }
        if(count>=5&&count<=10)
        {
          P2OUT &=~0x03;
          P2OUT |=0x02;
          num=num_2;
        }
        packet.frame[9]=' ';
        switch(num)
        {
        case 0:
          a_on;
          b_on;
          c_on;
          d_on;
          e_on;
          f_on;
          g_off;
          break;
        case 1:
          a_off;
          b_on;
          c_on;
          d_off;
          e_off;
          f_off;
          g_off;
          break;
        case 2:
          a_on;
          b_on;
          c_off;
          d_on;
          e_on;
          f_off;
          g_on;
          break;
        case 3:
          a_on;
          b_on;
          c_on;
          d_on;
          e_off;
          f_off;
          g_on;
          break;
        case 4:
          a_off;
          b_on;
          c_on;
          d_off;
          e_off;
          f_on;
          g_on;
          break;
        case 5:
          a_on;
          b_off;
          c_on;
          d_on;
          e_off;
          f_on;
          g_on;
          break;
    
        case 6:
          a_on;
          b_off;
          c_on;
          d_on;
          e_on;
          f_on;
          g_on;
          break;
        case 7:
          a_on;
          b_on;
          c_on;
          d_off;
          e_off;
          f_off;
          g_off;
          break;
        case 8:
          a_on;
          b_on;
          c_on;
          d_on;
          e_on;
          f_on;
          g_on;
          break;
        case 9:
          a_on;
          b_on;
          c_on;
          d_off;
          e_off;
          f_on;
          g_on;
          break;
        }
      }
    
      else if(packet.frame[9]=='E')
      {
        P1OUT |= 0x01;
        P1OUT |= 0x02;
        P4DIR |=0x20;
        P4DIR |=0x40;
        P4OUT |=0x20;
        P4OUT |=0x40;
        P3_set_out;
        P4_set_out;
        
        a_set_out;//set P2.2->segment A as output pin
        b_set_out;//set P2.3->segment B as output pin
        c_set_out;//set P2.4->segment C as output pin
        d_set_out;//set P4.3->segment D as output pin
        e_set_out;//set P4.4->segment E as output pin
        f_set_out;//set P4.5->segment F as output pin
        g_set_out;//set P4.6->segment G as output pin
        
        num_1=(30/10);
        r_1=30%10;
        num_2=r_1/1;
        if(count>=0&&count<=4)
        {
          P2OUT &=~0x03;
          P2OUT |=0x01;
          num=num_1;
        }
        if(count>=5&&count<=10)
        {
          P2OUT &=~0x03;
          P2OUT |=0x02;
          num=num_2;
        }
        packet.frame[9]=' ';
        switch(num)
        {
        case 0:
          a_on;
          b_on;
          c_on;
          d_on;
          e_on;
          f_on;
          g_off;
          break;
        case 1:
          a_off;
          b_on;
          c_on;
          d_off;
          e_off;
          f_off;
          g_off;
          break;
        case 2:
          a_on;
          b_on;
          c_off;
          d_on;
          e_on;
          f_off;
          g_on;
          break;
        case 3:
          a_on;
          b_on;
          c_on;
          d_on;
          e_off;
          f_off;
          g_on;
          break;
        case 4:
          a_off;
          b_on;
          c_on;
          d_off;
          e_off;
          f_on;
          g_on;
          break;
        case 5:
          a_on;
          b_off;
          c_on;
          d_on;
          e_off;
          f_on;
          g_on;
          break;
     
        case 6:
          a_on;
          b_off;
          c_on;
          d_on;
          e_on;
          f_on;
          g_on;
          break;
        case 7:
          a_on;
          b_on;
          c_on;
          d_off;
          e_off;
          f_off;
          g_off;
          break;
        case 8:
          a_on;
          b_on;
          c_on;
          d_on;
          e_on;
          f_on;
          g_on;
          break;
        case 9:
          a_on;
          b_on;
          c_on;
          d_off;
          e_off;
          f_on;
          g_on;
          break;
        }
      }

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have some very odd things going on here:
    Code:
        num_1=(20/10);
        r_1=20%10;
        num_2=r_1/1;
    Why don't you just do:
    Code:
    num_1 = 2;
    r_1 = num_2 = 0;
    I don't know why you have 10 case statements, because the only two values num will ever have are 0 and 2. Actually...
    Code:
        num_1=(30/10);
        r_1=30%10;
        num_2=r_1/1;
    ...
        num_1=(10/10);
        r_1=10%10;
        num_2=r_1/1;
    I should say will only ever be 0, 1, 2, or 3, depending on what your packet.frame[x] equals. In any case, you don't need 10 cases three times, you only need 2 cases three times.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    33
    Code:
     
    num_1=(20/10);
    r_1=20%10;
    num_2=r_1/1;
    This part is assigning a maths expression to a variable. Those are just arithmetic operation of way to display in 7 segment.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Aneesha View Post
    Code:
     
    num_1=(20/10);
    r_1=20%10;
    num_2=r_1/1;
    This part is assigning a maths expression to a variable.
    No it's not.

    edit - Or I am missing something in your translation here. 20/10 is always 2, and 20%10 is always 0, and 0/1 is also always 0.


    Quzah.
    Last edited by quzah; 06-22-2011 at 02:04 AM.
    Hope is the first step on the road to disappointment.

  15. #15
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    I agree with everything quzah is saying - though this is what I was referring to earlier. I was assuming that 20%10 would become 20%var. I was also assuming that you might think to create a function to manage your 7-seg that accepts an integer and sets the correct pins.

    That said, if the code above works for packet.frame[9]=='E', then I cant see why it wouldn't work for the other two. However, if your original description of 10 = Alarm, 20 = Warning is correct, you actually have them around the wrong way.

    Assuming the code works, then there might be something wrong with whatever you are using as a transmitter. Though that would be pretty hard for us to troubleshoot. You would need to confirm that a water level of 10 is actually setting packet.frame[9]=='A' and a level of 20 is setting packet.frame[9]=='W'
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SRF02 ultrasonic sensor
    By quachtinh in forum C Programming
    Replies: 1
    Last Post: 11-13-2009, 06:51 AM
  2. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  3. Creating a array sensor
    By mikeprogram in forum C++ Programming
    Replies: 1
    Last Post: 12-01-2005, 05:44 PM
  4. OS sensor
    By Queatrix in forum Windows Programming
    Replies: 1
    Last Post: 07-11-2005, 07:00 PM