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

help in digital dice, same sequence

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



Joined: 09 Dec 2012
Posts: 3

View user's profile Send private message Send e-mail

help in digital dice, same sequence
PostPosted: Mon Dec 10, 2012 8:43 am     Reply with quote

code:
Code:
#include <16F877a.h>
#Fuses HS,NOWDT,NOLVP,Protect
#include <stdlib.h>
#include <stdlibm.h>
#include <stdio.h>
#use delay (clock=20M)
#byte out=6

void main()
{
int a;
int seg[6]={0b0000110,0b11011011,0b01001111,0b01100110,0b01101101,0b01111101};

srand(1);
set_tris_b(0);

while(1)
{
a=rand()% 6 + 1;
{
switch(a)
{
case 1:out=seg[a];
delay_ms(100);
case 2:out=seg[a];
delay_ms(100);
case 3:out=seg[a];
delay_ms(100);
case 4:out=seg[a];
delay_ms(100);
case 5:out=seg[a];
delay_ms(100);
case 6:out=seg[a];
delay_ms(100);
}}
}}



plz help me, my code is having two problems.
1) i am getting the same sequence of random number in seven segment display, means 4,5,4,6,3 etc every time...how can i have a new sequence every time i start my program?

I am an engineering student. I have just started learning pic micro controller and c language, so plz make me understand in easy term.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Mon Dec 10, 2012 8:50 am     Reply with quote

Hmm... You are the second person to post with this problem today Same PIC, same problem. Is this a school assignment?

Look at the thread your collegue posted.

RF Developer
ahsansag93



Joined: 09 Dec 2012
Posts: 3

View user's profile Send private message Send e-mail

PostPosted: Mon Dec 10, 2012 8:55 am     Reply with quote

RF_Developer wrote:
Hmm... You are the second person to post with this problem today Same PIC, same problem. Is this a school assignment?

Look at the thread your collegue posted.

RF Developer

yup brother i tried :( but as much i understood from that post , i made...
i have been trying to made it for 5 days :( but still not successful .i don't know what to do know now.
how to generate new sequence every time
she is my colleague.
its our assignment.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Dec 10, 2012 9:31 am     Reply with quote

First error is that your switch-case statements are missing the 'break' command. What now happens is that all commands 'fall through' until the end of the switch-statement and end with the last statement, 'case 6'. Look into your C study book for the correct implementation.

second error:
Code:
a=rand()% 6 + 1;
{
switch(a)
{
case 1:out=seg[a];
You nicely add 1 to 'a' so it counts from 1 to 6, but then the offset into an array always starts counting from 0. So here you should subtract 1 again.

Once you have that fixed you should be getting semi random values. A problem here is that you only have 100ms delay, for the human eye that will bee too fast and you only see rapid blinking LEDs.

Then, every time you start your dice you will get the same sequence of values. That is because computers use a 'pseudo random' generator. To make the sequence different every time you will have to give srand() a real random number just once, for how to do this a few hints were given in the other thread.

Hint: your program can be made a lot shorter when you look at how all the lines in the switch statement are the same. In fact, you can remove the whole switch statement and replace by 2 lines.
ezflyr



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

View user's profile Send private message

PostPosted: Mon Dec 10, 2012 10:28 pm     Reply with quote

Hi,

Microchip developed a "roll the dice" project a few years ago that was covered in Nuts and Volts magazine. You can download the code here:

http://www.nutsvolts.com/index.php?/magazine/downloads//2008

The code can be found under Jan. 2008

John
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