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

verifying an array?

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



Joined: 26 Aug 2011
Posts: 6

View user's profile Send private message

verifying an array?
PostPosted: Mon Oct 03, 2011 1:02 pm     Reply with quote

Is the code correct in line 2?
Code:

int8 password_array[8]; 

if (password_array=="12311111")
{
output_high(pin_c3);
delay_ms(30000);
}
else
output_high(pin_c4);
}

This is part from a project that ask the user to enter the code through a keypad (i used flex_kbd). If the code correct the pin c3 goes high.
_________________
AYMAN
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Mon Oct 03, 2011 1:18 pm     Reply with quote

Hi,

Give it a try, and I think you'll find it doesn't work. It may not even compile....

You'll need something like this:

Code:

if (password_array[0] == '1' && password_array[1] == '2' && password_array[2] == '3') && ............)



John
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Oct 03, 2011 1:27 pm     Reply with quote

You can use strcmp(), but than the array must include a terminating null and have the length increased by one character.
aymanoo



Joined: 26 Aug 2011
Posts: 6

View user's profile Send private message

thx it worked
PostPosted: Tue Oct 04, 2011 10:13 am     Reply with quote

ezflyr wrote:
Hi,

Give it a try, and I think you'll find it doesn't work. It may not even compile....

You'll need something like this:

Code:

if (password_array[0] == '1' && password_array[1] == '2' && password_array[2] == '3') && ............)



John

_________________
AYMAN
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Oct 05, 2011 3:40 am     Reply with quote

There are other approaches to solve this problem, and problems like it.

This sort of thing is OK if its all the PIC is doing, i.e. in an exercise or a very simple widget. I say that as comparing an entered password against a single constant password hardwired into the code is very simple indeed, and almost useless in practical systems... except just possibly as a developer's backdoor. In most real systems you generally can't afford - space and money - to allocate one PIC to one function, so its going to have to do more than one thing, such as operate a lock, scan a keypad, multiplex a display AND communicate to a remote access control system all at the same time. In these sorts of systems a state machine approach is probably more useful.

I use this approach for all sorts of things including system state control and sequencing, numeric decoding and verification, button press detection/debouncing (including single/double click detection etc), comms protocol implementation and packet reading/decoding/verification.

In this instance consider your code running on a PIC that has a main loop running round and round continuously doing stuff is sequence - the standard main loop thing. You can't lock up other tasks while one bit delays, or waits for a button press, or a character to arrive on a serial port, or a message from CAN or whatever. All these things must run all the time without interfering with each other. State machines, one for each function that needs sequencing, all get called from the main loop. Other stuff runs too, like ADC measurements or checking input pins. The password input machine looks for a keypress, or serial input or whatever its input is, if it gets one it compares it to the relevant entry in the array, if good then it changes its state to look for the next keypress/character/whatever. If bad, it goes back to the idle state. Only once the machine has worked its way through all the entires one by one, can it enter the "release authorised" state and enable the lock. It starts a timer, either hardware or firmware derived from an interrupt routine, which will raise a flag in due course. When the flag is seen the lock is closed and the state machine goes back to idle to await the next time....

State machines are often introduced with the classic traffic light assignment, but there are many, many uses for them. They are a powerful tool in embedded systems.

RF Developer
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