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

Problem with if-else condition...

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



Joined: 18 Dec 2014
Posts: 12

View user's profile Send private message

Problem with if-else condition...
PostPosted: Fri May 29, 2015 8:42 am     Reply with quote

Code:
#include <keycode test.h>

int key[4]={3,7,0,1};
int pressed[4]={0,0,0,0};
int column=0;
int row=10;
int num=0;

void out()
{
  if (column==0) output_a(254);
  if (column==1) output_a(253);
  if (column==2) output_a(251);
}

void press(void)
{
   int pr=0;
  if(num<4)
  {
    pr = (column+1) + 3*row;
    if(pr == 11) pressed[num] = 0;
    else pressed[num] = pr;
    printf("%d ", pressed[num]);
    row = 10;
    num++;
  }


 
}

void read()
{
 int i=0;
 int in;
  for(i=0;i<4;i++)
  {
    in = PIN_B0 + i;
   if(input(in) == 0)
   {
    delay_ms(32);

      while(input(in)==0)
      {
        row = i;

      }

  }
 }
 if(row!=10) press();
}



void main() {
  int num=0;
  int i;
  int sum=0;
 

 
 

 

  while (TRUE)
  {
     if(num==4)
     {
     
      for(i=0;i<4;i++)
       {
        sum = sum + (key[i] - pressed[i]);
     
       }
      if(sum==0) output_d(4);
      num = 0;
     }

 
         
     if(num<4)
     {
      for(column=0;column<3;column++)
      {
       out();
       read();
       
      }

     }
   

  }
}

I am using PIC18F45K22 with external oscillator at 8MHz.
The problem is that it never executes the if(num==4) condition.
Originally i tried with if(num<4) ........ (where it outputs columns and reads rows) else verify() (function that substracts key[i] and press[i]). the problem is that it never checks the two arrays, it never goes to verify function...
humbertokramm



Joined: 29 May 2015
Posts: 7

View user's profile Send private message

PostPosted: Fri May 29, 2015 8:58 am     Reply with quote

I think the bug is because of you declare twice the same variable "num".
It is declared as Global and Local (on the main function)
_________________
Humberto Kramm
looking for questions to answer 42
jeremiah



Joined: 20 Jul 2010
Posts: 1319

View user's profile Send private message

PostPosted: Fri May 29, 2015 10:18 am     Reply with quote

Yes.

In C, if variables of two different scopes (global and local) are available in the current scope, then the local one takes precedence and the global one cannot be used in that scope.

Also, as a side advice, it is a really good idea to avoid using int, short, long, etc and instead use the defined: int32, int16, and int8. The size of int can change between parts, but int32, int16, and int8 are always the same size.
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