Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Please help with this getch() code !!!

Status
Not open for further replies.

motofreek

Junior Member level 2
Joined
Dec 12, 2009
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,450
Hi, I'm trying to write a code so that when 5 numbers (like 12345) are recieved in sequence an LED comes on. I'm using the code below and IT IS WORKING. However, if too many numbers are typed AFTER the numbers are entered from the keyboard, the program freezes up.

For eg. if I type 12345 the LED blinks once and I can repeat the action again.
But if I type 123456789, the LED blinks but I cannot do it again. The
program freezes.



while(true){
ch=getch();
switch(ch)
{
case '1':
if(getch()=='2'&&getch()=='3'&&getch()=='4'&&getch()=='5'){
output_high(PIN_B7);
delay_ms(2000);
output_low(PIN_B7);
goto jump;
}
break;
jump:
puts("prog restarting now");





Any ideas on how I can fix this ?
(I think it recieves too many characters in the buffer and gets confused)
 

hi
plz put your program completely. where your while(true) ends? where is your break for switch case options? as I see your switch is not correct you should put all digits option and put a counter and when you receive correct sequence increment it and when incorrect digit reset your counter. when your counter reaches 5 on your led and reset counter and start again.
 

Hi guys. Thanks for the reply. Here's my complete code.

I'm not exactly sure how to use the COUNTER to track the amount of digits can you give me an example ?

void main() {
char ch;
printf("To make the red LED flash...\n\n\n\r");
delay_ms(2000);
printf("Enter 12345\n\n\r");
printf("To make the green LED flash...\n\n\n\r");
delay_ms(2000);
printf("Enter 54321\n\n\r");

while(true){
ch=getch();
switch(ch)
{
case '5':
if(getch()=='4'&&getch()=='3'&&getch()=='2'&&getch()=='1')
{output_high(PIN_B6);
delay_ms(2000);
output_low(PIN_B6);
break;
}
case '1':
if(getch()=='2'&&getch()=='3'&&getch()=='4'&&getch()=='5')
{output_high(PIN_B6);
delay_ms(2000);
output_low(PIN_B6);
break;
}
default:
printf("That's not valid");
}
}
}

:|I'm not an expert at C programming
 

motofreek said:
Hi guys. Thanks for the reply. Here's my complete code.

if(getch()=='4'&&getch()=='3'&&getch()=='2'&&getch()=='1')


i don't think this code will work getch() will have only one value and the result will be always 0
you better go for scanf and compare with your string
if you want it with getch you have to get it separately and compare each time with the digit or get the value to a string ( accounting error like backspace etc) and compare

hope this will help you
 

What I really need is to turn on an output if a string is read from a long stream of characters. Maybe I can use something like strcmp but I'm not sure how to use it.

Example: if the stream is 11332465464313123455655222,

I want to be able to activate an output when "12345" passes through and ignore all other characters.

By the way, I'm using a PIC16F876 with hardware UART.

Please help.
 

you can use strstr function
 

OK. I'm using this but it doesn't work as I want. why?


void main() {
char s1[] = "qwertykeyboard";
char s2[] = "keyb";
char ptr;
ptr = strstr(s1, s2);
while(1){
output_low(pin_B6);
delay_ms(2000);
output_high(pin_B7);
gets(s1);
if(ptr==NULL){
printf("Waiting for string. Press ENT after sending string.");
}

else
output_high(pin_B6);
}
}
 

Hope this will help you

#include <16F876.h>
#include<string.h>
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void main() {
char s1[] = "qwertykeyboard";
char s2[] = "keyb";
char ptr;

while(1){
output_low(pin_B6);
output_low(pin_B7);
delay_ms(2000);
gets(s1);
ptr = strstr(s1, s2);
if(ptr)
{output_high(pin_B7);
break;
}

if(ptr==NULL){
printf("Waiting for string. Press ENT after sending string.");
}
else
{
output_low(pin_B7);
output_high(pin_B6);
}
}

// do other code

}
 

give a feed back, is this code helped you?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top