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 CCS Technical Support

SSD1306 OLED menu interfacing with button and LED using PIC
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sradhika1998



Joined: 21 Jun 2019
Posts: 12

View user's profile Send private message AIM Address

SSD1306 OLED menu interfacing with button and LED using PIC
PostPosted: Fri Jun 21, 2019 8:32 am     Reply with quote

Hello,
I have built a menu code, and this little code is part of big code. I am interfacing transistor in place of LED as shown in picture. But just for practical results i am using LED here.
Code:

void discharging_USB() {
       
        SSD1306_ClearDisplay();
        cursor(starting_row);
        SSD1306_GotoXY(starting_col, starting_row);
        SSD1306_PutC("1. 5V 1A");
        delay_ms(10);
        start_row = starting_row;
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("2. 5V 2A");
        delay_ms(10);
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("3. 5V 3A");
        delay_ms(10);
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("4. Back");
        delay_ms(100);
        if(input_state(PIN_B5)==0 && m2==2){
        if(  row_Ind ==1  && output_low(PIN_B0) && output_low(PIN_B1) && output_low(PIN_B2))
           {
            output_low(PIN_B0);
            output_low(PIN_B1);
            output_high(PIN_B2);
            button_select==1;
           }
       else if( row_Ind ==2 && output_low(PIN_B0) && output_low(PIN_B1) && output_low(PIN_B2) )
           {
            output_low(PIN_B0);
            output_high(PIN_B1);
            output_low(PIN_B2);
            button_select==1;
           }
           
        else if( row_Ind==3 && output_low(PIN_B0) && output_low(PIN_B1) && output_low(PIN_B2) )
           {
            output_low(PIN_B1);
            output_high(PIN_B0);
            output_low(PIN_B2); 
            button_select ==1;
           }
           else
           {
            output_low(PIN_B0);
            output_low(PIN_B1);
            output_high(PIN_B2);
            button_select==1;
           }}
        }


In this code, I only get the output from pin b2 but not the b0 or b1 when i press 5V 1A in the menu.. or 5V 3A in the menu. In this code only else statement is executing. Here, m2 ==2 is the menu 2 here.

[img]https://postimg.cc/68js4q64[/img]
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: SSD1306 OLED menu interfacing with button and LED using
PostPosted: Fri Jun 21, 2019 8:58 am     Reply with quote

sradhika1998 wrote:

if( row_Ind ==1 && output_low(PIN_B0) && output_low(PIN_B1) && output_low(PIN_B2))

else if( row_Ind ==2 && output_low(PIN_B0) && output_low(PIN_B1) && output_low(PIN_B2) )

else if( row_Ind==3 && output_low(PIN_B0) && output_low(PIN_B1) && output_low(PIN_B2) )

[img]https://postimg.cc/68js4q64[/img]

The output_low() function doesn't return a value. Maybe you really
want to use input_state() ? But output_low() in an if() statement will
not work.
sradhika1998



Joined: 21 Jun 2019
Posts: 12

View user's profile Send private message AIM Address

Re: SSD1306 OLED menu interfacing with button and LED using
PostPosted: Fri Jun 21, 2019 10:15 am     Reply with quote

PCM programmer wrote:

The output_low() function doesn't return a value. Maybe you really
want to use input_state() ? But output_low() in an if() statement will
not work.


hello I made some changes in it,
Code:


        if( row_Ind ==1  && input_state(PIN_B0)==0 && input_state(PIN_B1)==0  && input_state(PIN_B2)==0 && input_state(PIN_B5)==0 && m2==1)
           {
            input_state(PIN_B0)==0;
            input_state(PIN_B1)==0;
            input_state(PIN_B2)==1;
            button_select==1;
           }
           else if(row_Ind ==1  && input_state(PIN_B0)==0 && input_state(PIN_B1)==0  && input_state(PIN_B2)==0 && input_state(PIN_B5)==0 && m2==1)
           { input_state(PIN_B0)==1;
            input_state(PIN_B1)==0;
            input_state(PIN_B2)==0;
            button_select==1;}
            else if (row_Ind ==1  && input_state(PIN_B0)==0 && input_state(PIN_B1)==0  && input_state(PIN_B2)==0 && input_state(PIN_B5)==0 && m2==1)
            {input_state(PIN_B0)==1;
            input_state(PIN_B1)==0;
            input_state(PIN_B2)==0;
            button_select==1;
            }
           else
           {
           input_state(PIN_B0)==0;
            input_state(PIN_B1)==0;
            input_state(PIN_B2)==1;
            button_select==1;}
       
        }


But now no LED is glowing in the output. What to do?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 21, 2019 10:36 am     Reply with quote

Quote:
input_state(PIN_B0)==0;
input_state(PIN_B1)==0;
input_state(PIN_B2)==1;

This code won't do anything. Look at this:
Quote:
....................
.................... input_state(PIN_B2)==1;
....................
....................

It compiles to nothing.

You need to learn C.
Ttelmah



Joined: 11 Mar 2010
Posts: 19542

View user's profile Send private message

PostPosted: Fri Jun 21, 2019 10:44 am     Reply with quote

You really need to think/explain what you are actually trying to do?.

output_low(PIN_xx);

sets an output pin low.

If a pin is being used as an output, it can't at the same time be an input.

input_state returns true/false depending on the voltage present on a pin.

If you are using a pin as an input, you can hardly drive it low a moment
later (actually there are circumstances where you can mix I/O
operations, but only by using specific timings).

How are you 'pressing' things in the menu?. Keys?. If so which pins
are these connected to, and how are they connected. If these are B0, B1
& B2, then you can hardly use these same pins to drive LED's.
sradhika1998



Joined: 21 Jun 2019
Posts: 12

View user's profile Send private message AIM Address

PostPosted: Sat Jun 22, 2019 2:50 am     Reply with quote

Ttelmah wrote:
You really need to think/explain what you are actually trying to do?.

output_low(PIN_xx);

sets an output pin low.

If a pin is being used as an output, it can't at the same time be an input.

input_state returns true/false depending on the voltage present on a pin.

If you are using a pin as an input, you can hardly drive it low a moment
later (actually there are circumstances where you can mix I/O
operations, but only by using specific timings).

How are you 'pressing' things in the menu?. Keys?. If so which pins
are these connected to, and how are they connected. If these are B0, B1
& B2, then you can hardly use these same pins to drive LED's.

Hello, I am switching ON the LED by using push button which then select the desired menu on OLED SSD1306
There are three button, up down and select which is connected to b5, b6, b7 pin and I want output at bo, b1, b2. Please look to the image added in main text. Now how do I switch ON the led when I select desired menu on OLED and that time other 2 led is off.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jun 22, 2019 3:08 am     Reply with quote

Your buttons are connected incorrectly. Do it like this for each switch:
Code:

           +5v
            |
            <
            > 4.7K       
            <         ___ Push button switch 
To          |        _|_|_
PIC -----------------o   o------
pin                            |             
                              --- GND
                               -   

Then test for a logic '0' to see if a switch is pressed.
The 'off' state for each switch is a logic '1'.
sradhika1998



Joined: 21 Jun 2019
Posts: 12

View user's profile Send private message AIM Address

PostPosted: Sat Jun 22, 2019 4:07 am     Reply with quote

PCM programmer wrote:
Your buttons are connected incorrectly. Do it like this for each switch:
Code:

           +5v
            |
            <
            > 4.7K       
            <         ___ Push button switch 
To          |        _|_|_
PIC -----------------o   o------
pin                            |             
                              --- GND
                               -   

Then test for a logic '0' to see if a switch is pressed.
The 'off' state for each switch is a logic '1'.

Hello I have used this circuit, but the thing is it only activate b2, i guess it only works for else statement but not if. What changes I need to do so that if statement work too.
temtronic



Joined: 01 Jul 2010
Posts: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jun 22, 2019 4:16 am     Reply with quote

You should add a small cap across the switch. I use .68 mfd as I have a reel of them,probably 4500+ left on it. .1 mfd, .22 mfd, .05mfd make work as well.
The cap will reduce if not eliminate 'contact bounce' which is the term for multiple 'one-zero' transitions when you press and release the pushbutton.

Jay
sradhika1998



Joined: 21 Jun 2019
Posts: 12

View user's profile Send private message AIM Address

PostPosted: Sat Jun 22, 2019 8:09 am     Reply with quote

temtronic wrote:
You should add a small cap across the switch. I use .68 mfd as I have a reel of them,probably 4500+ left on it. .1 mfd, .22 mfd, .05mfd make work as well.
The cap will reduce if not eliminate 'contact bounce' which is the term for multiple 'one-zero' transitions when you press and release the pushbutton.

Jay

Hello, Now I have used case statement, its still not glowing any led. Please guide.
Code:

  void discharging_USB() {
       
        SSD1306_ClearDisplay();
        cursor(starting_row);
        SSD1306_GotoXY(starting_col, starting_row);
        SSD1306_PutC("1. 5V 1A");
        delay_ms(10);
        start_row = starting_row;
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("2. 5V 2A");
        delay_ms(10);
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("3. 5V 3A");
        delay_ms(10);
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("4. Back");
        delay_ms(100);
       if (m2==1 && row_Ind == 1 && button_select==0){
       
        if( m2==2)
           {
           switch (row_Ind)
           {
           case 1:
           
           
            output_low(PIN_B0)==0;
            output_low(PIN_B1)==0;
            output_high(PIN_B2)==1;
            button_select==1;
            break;
           case 2:
           
            output_high(PIN_B0)==0;
            output_low(PIN_B1)==1;
            output_low(PIN_B2)==0;
            button_select==1;
            break;
            case 3:
           
            output_high(PIN_B0)==1;
            output_low(PIN_B1)==0;
            output_low(PIN_B2)==0;
            button_select==1;
            break;
           default:
           
            output_low(PIN_B0)==0;
            output_low(PIN_B1)==0;
            output_high(PIN_B2)==1;
            button_select==1;
       break;
        }
  }
       }
  }
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Sat Jun 22, 2019 8:21 am     Reply with quote

How do you expect it will ever enter the case statement.
Quote:
if (m2==1 && row_Ind == 1 && button_select==0){

if( m2==2)
sradhika1998



Joined: 21 Jun 2019
Posts: 12

View user's profile Send private message AIM Address

PostPosted: Sat Jun 22, 2019 8:30 am     Reply with quote

alan wrote:
How do you expect it will ever enter the case statement.
Quote:
if (m2==1 && row_Ind == 1 && button_select==0){

if( m2==2)


I don't know how do I frame this loop.

m2==1 mean that there is menu which is considered as m2==1
for eg:
1. ABC
2. DEF
3. GHI
and when m2==2 mean that when we go to sub menu of 1. ABC 2. DEF 3. GHI
and I wanna select sub menu of ABC, which is
1. 5V 1A
2. 5V 2A
3. 5V 3A
4. Back

If I just go with m2==2 then program get confuse with which menu it will go with, either ABC, DEF or GHI

Guide me what syntax do I use in it..
Awaiting your response.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jun 22, 2019 12:25 pm     Reply with quote

Stop doing this:
Quote:

output_low(PIN_B0)==0;
output_low(PIN_B1)==0;
output_high(PIN_B2)==1;

There is no equality test on output_high() or output_low().

Do it like this:
Code:
output_low(PIN_B0);
output_low(PIN_B1);
output_high(PIN_B2);
sradhika1998



Joined: 21 Jun 2019
Posts: 12

View user's profile Send private message AIM Address

PostPosted: Sat Jun 22, 2019 10:29 pm     Reply with quote

PCM programmer wrote:
Stop doing this:
Quote:

output_low(PIN_B0)==0;
output_low(PIN_B1)==0;
output_high(PIN_B2)==1;

There is no equality test on output_high() or output_low().

Do it like this:
Code:
output_low(PIN_B0);
output_low(PIN_B1);
output_high(PIN_B2);

Okay, I did. And what logic should I use for switch statement?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Jun 23, 2019 11:00 am     Reply with quote

Quote:
If I just go with m2==2 then program get confuse with which menu it will go with, either ABC, DEF or GHI

Guide me what syntax do I use in it..
Awaiting your response.

You are the one confused, not the program.
I'm also confused, and can't figure out what you are trying to do or how you are trying to achieve it.

This is a help you forum NOT a do it for you one.

To help us:-

Explain clearly what you are trying to do.
Provide a current diagram or list of relevant connections.
Supply SHORT complete compilable code which shows your problem.
I.e. so that we can copy and paste to test it.

You appear to be struggling with a switch/LED menu loop.
That's all you need to include.
Leave out ALL other code, and I mean remove it, don't just comment it out.
It will then be much easier for us all to help you.

Mike
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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