C programming

[hidden=20]
I am using C to program MicroStamp on my circuit as well as have the output show up in PuTTY. It really should be simple but for some reason it doesn't want to work the way I thought it would. (I am new when it comes to programming with C).

The program is supposed to count how many times a button is hit within a 10 second interval. Every time the button is pressed the 7-segment display increments 1 (from 0 to 9 then repeats) and it displays the true number in PuTTY (for example on the 42nd press of the button it will display 42).

I was able to easily get the program to count the button presses (at pin 6) and display the number in PuTTY, however when I make the program only take in input for 10 seconds it doesn't want to work quite right.

This program below is an example of one that does work perfectly. Once the button is pressed it counts starting at 0 in increments of 1 every 1 second:
Code:
//Filename: Lab4a.c


#include "kernel.c"
#include "vector.c"

void display_digit (int data) {
    int i;
	for (i=1;i<8;i++)set_pin(i); //
switch(data){
    case 0:       //light segments a,b,c,d,e,f
	             clear_pin(1);clear_pin(2);clear_pin(3);
				 clear_pin(4);clear_pin(5);clear_pin(6);
				 break;
	case 1: 		 //light segements b,c
				 clear_pin(2);clear_pin(3);
				 break;
	case 2:       //light segments a,b,d,e,g
	             clear_pin(1);clear_pin(2);clear_pin(4);
				 clear_pin(5);clear_pin(7);
				 break;
	case 3: 		 //light segements a,b,c,d,g
				 clear_pin(1);clear_pin(2);clear_pin(3);
				 clear_pin(4);clear_pin(7);
				 break;
	case 4:       //light segments b,c,f,g
	             clear_pin(2);clear_pin(3);clear_pin(6);
				 clear_pin(7);
				 break;
	case 5: 		 //light segements a,c,d,f,g
				 clear_pin(1);clear_pin(3);clear_pin(4);
				 clear_pin(6);clear_pin(7);
				 break;
	case 6:       //light segments a,c,d,e,f,g
	             clear_pin(1);clear_pin(3);clear_pin(4);
				 clear_pin(5);clear_pin(6);clear_pin(7);
				 break;
	case 7: 		 //light segements a,b,c
				 clear_pin(1);clear_pin(2);clear_pin(3);
				 break;
	case 8:       //light segments a,b,c,d,e,f,g
	             clear_pin(1);clear_pin(2);clear_pin(3);
				 clear_pin(4);clear_pin(5);clear_pin(6);
				 clear_pin(7);
				 break;
	case 9: 		 //light segements a,b,c,f,g
				 clear_pin(1);clear_pin(2);clear_pin(3);
				 clear_pin(6);clear_pin(7);
				
}
}

void main (void){

int count=0; 

init();
OutChar(CR); OutChar(LF);  //line control for Putty
OutString("Please push the button.");//Prompt user to push button
display_digit(count);
count=1;
wait_pin(6);//waits for button pushed on pin 6 (hardware pin 8)
while(1){            //infinite loop
   pause(3590);//wait 1 second
   OutChar(CR); OutChar(LF);
   display_digit(count); //call function display_digit to light segments
   if (count==0)OutString("0"); //print number displayed on 7-segment display to putty
   else if (count==1)OutString("1");
   else if (count==2)OutString("2");
   else if (count==3)OutString("3");
   else if (count==4)OutString("4");
   else if (count==5)OutString("5");
   else if (count==6)OutString("6");
   else if (count==7)OutString("7");
   else if (count==8)OutString("8");
   else OutString("9");
   count++;   //increment counter
   if(count==10) count-=10;   //go back to count=0
  }
  }


This is the program I am working on that so far simply counts the number of presses of the button (limited to 40 presses) and has no 10 second limit:
Code:
//Filename: Lab4a.c


#include "kernel.c"
#include "vector.c"

void display_digit (int data) {
    int i;
	for (i=1;i<8;i++)set_pin(i); //
switch(data){
    case 0:       //light segments a,b,c,d,e,f
	             clear_pin(1);clear_pin(2);clear_pin(3);
				 clear_pin(4);clear_pin(5);clear_pin(6);
				 break;
	case 1: 		 //light segements b,c
				 clear_pin(2);clear_pin(3);
				 break;
	case 2:       //light segments a,b,d,e,g
	             clear_pin(1);clear_pin(2);clear_pin(4);
				 clear_pin(5);clear_pin(7);
				 break;
	case 3: 		 //light segements a,b,c,d,g
				 clear_pin(1);clear_pin(2);clear_pin(3);
				 clear_pin(4);clear_pin(7);
				 break;
	case 4:       //light segments b,c,f,g
	             clear_pin(2);clear_pin(3);clear_pin(6);
				 clear_pin(7);
				 break;
	case 5: 		 //light segements a,c,d,f,g
				 clear_pin(1);clear_pin(3);clear_pin(4);
				 clear_pin(6);clear_pin(7);
				 break;
	case 6:       //light segments a,c,d,e,f,g
	             clear_pin(1);clear_pin(3);clear_pin(4);
				 clear_pin(5);clear_pin(6);clear_pin(7);
				 break;
	case 7: 		 //light segements a,b,c
				 clear_pin(1);clear_pin(2);clear_pin(3);
				 break;
	case 8:       //light segments a,b,c,d,e,f,g
	             clear_pin(1);clear_pin(2);clear_pin(3);
				 clear_pin(4);clear_pin(5);clear_pin(6);
				 clear_pin(7);
				 break;
	case 9: 		 //light segements a,b,c,f,g
				 clear_pin(1);clear_pin(2);clear_pin(3);
				 clear_pin(6);clear_pin(7);
				
}
}

void main (void){

int Count=0; 
int CountPutty=0;
int icount=0;

init();
OutChar(CR); OutChar(LF);  //line control for Putty
display_digit(Count);
OutString("Please push the button.");//Prompt user to push button
wait_pin(6);//waits for button pushed on pin 6 (hardware pin 8)
while(1){            //infinite loop
   OutChar(CR); OutChar(LF);
   button(6);
   display_digit(Count); //call function display_digit to light segments
   if (CountPutty==0)OutString("0"); //print number displayed on 7-segment display to putty
   else if (CountPutty==1)OutString("1");
   else if (CountPutty==2)OutString("2");
   else if (CountPutty==3)OutString("3");
   else if (CountPutty==4)OutString("4");
   else if (CountPutty==5)OutString("5");
   else if (CountPutty==6)OutString("6");
   else if (CountPutty==7)OutString("7");
   else if (CountPutty==8)OutString("8");
   else if (CountPutty==9)OutString("9");
   else if (CountPutty==10)OutString("10");
   else if (CountPutty==11)OutString("11");
   else if (CountPutty==12)OutString("12");
   else if (CountPutty==13)OutString("13");
   else if (CountPutty==14)OutString("14");
   else if (CountPutty==15)OutString("15");
   else if (CountPutty==16)OutString("16");
   else if (CountPutty==17)OutString("17");
   else if (CountPutty==18)OutString("18");
   else if (CountPutty==19)OutString("19");
   else if (CountPutty==20)OutString("20");
   else if (CountPutty==21)OutString("21");
   else if (CountPutty==22)OutString("22");
   else if (CountPutty==23)OutString("23");
   else if (CountPutty==24)OutString("24");
   else if (CountPutty==25)OutString("25");
   else if (CountPutty==26)OutString("26");
   else if (CountPutty==27)OutString("27");
   else if (CountPutty==28)OutString("28");
   else if (CountPutty==29)OutString("29");
   else if (CountPutty==30)OutString("30");
   else if (CountPutty==31)OutString("31");
   else if (CountPutty==32)OutString("32");
   else if (CountPutty==33)OutString("33");
   else if (CountPutty==34)OutString("34");
   else if (CountPutty==35)OutString("35");
   else if (CountPutty==36)OutString("36");
   else if (CountPutty==37)OutString("37");
   else if (CountPutty==38)OutString("38");
   else if (CountPutty==39)OutString("39");
   else if (CountPutty==40)OutString("40");
   Count++;   //increment counter
   CountPutty++;
   if(Count==10) Count-=10;   //go back to count=0
  }
  }

I tried having something similar to below in order to get my program to only take input over a 10 second interval:
Code:
pause(3590); //1 second pause for turbo mode
button(6);
icount=count(6,35900);  //10 second input interval
display_digit(icount);

What occurs when I add that is it doesn't take input for a few seconds and then when it starts counting the number of presses, it doesn't count every single press if the presses are to close together (within certain time frame).


This is a "partial" listing for the program directly from my lab book:
Code:
void main(void){
  init():
  while(1){
          paused(1000);  //1 second pause for standard mode - use 3250 or higher (varies) for Turbo mode
          button(6);
          icount=count(6,10000);    //replace 10000 with 32500 or higher (varies) for Turbo mode
          display_digit(icount);
}}
[/hidden]
 
Oh. This was my c++ program.
Code:
int main(int argc, char *argv[])
{

    string datstring;
    datstring = "abcdefghijklmnopqrstuvwxyz";
    for(int count=0; count<26; count++)
            {
            cout << "Hello World " << datstring[count] << endl;

            }
    system("PAUSE");
    return EXIT_SUCCESS;
}
Dem strings and for loops, man.
 
^
useless



Anyways, you seem to have a problem with the sleep where it is freezing the board input? You could always just use a more dynamic way of sleeping with a for loop.
 
Use the time() function?

Code:
time_t startTime = time(); // return current time in seconds
while (time() - startTime < 10)
{
//do stuff here
}
 
Storm said:
Use the time() function?

Code:
time_t startTime = time(); // return current time in seconds
while (time() - startTime < 10)
{
//do stuff here
}

Thanks, I will test this out tomorrow and let you know what happens. I may need to make some slight changes to it though.
 
@"Storm" and @"Chewbaka", I was just over-thinking it. I ended up doing my own thing and after a few tweaks I got it working perfectly.

Here is the code:
Code:
void main (void){

int Count=0; 
int CountPutty=0;
int icount;
int sig;

init();
OutChar(CR); OutChar(LF);  //line control for Putty
display_digit(Count);
OutString("Please push the button.");//Prompt user to push button
while(1){            //infinite loop
   OutChar(CR); OutChar(LF);
   button(6);
   display_digit(Count);
   icount=count(6,35900);
   OutString("The total number of button presses is: ");
   OutChar(CR); OutChar(LF);
   OutUDec(icount); //print total number to putty
   sig = icount%10; //Find 2nd digit
   icount = icount/10; //Find 1st digit 
   display_digit(icount);
   OutChar(CR); OutChar(LF);
   pause(3590);
   OutString("Please push the button to see the next significant digit.");
   wait_pin(6);
   display_digit(sig); //call function display_digit to light segments
   OutChar(CR); OutChar(LF);
   OutString("Push the button to start again.");
   }
   }

And if you want to know the code for display_digit:
Code:
void display_digit (int data) {
    int i;
	for (i=1;i<8;i++)set_pin(i); //
switch(data){
    case 0:       //light segments a,b,c,d,e,f
	             clear_pin(1);clear_pin(2);clear_pin(3);
				 clear_pin(4);clear_pin(5);clear_pin(6);
				 break;
	case 1: 		 //light segements b,c
				 clear_pin(2);clear_pin(3);
				 break;
	case 2:       //light segments a,b,d,e,g
	             clear_pin(1);clear_pin(2);clear_pin(4);
				 clear_pin(5);clear_pin(7);
				 break;
	case 3: 		 //light segements a,b,c,d,g
				 clear_pin(1);clear_pin(2);clear_pin(3);
				 clear_pin(4);clear_pin(7);
				 break;
	case 4:       //light segments b,c,f,g
	             clear_pin(2);clear_pin(3);clear_pin(6);
				 clear_pin(7);
				 break;
	case 5: 		 //light segements a,c,d,f,g
				 clear_pin(1);clear_pin(3);clear_pin(4);
				 clear_pin(6);clear_pin(7);
				 break;
	case 6:       //light segments a,c,d,e,f,g
	             clear_pin(1);clear_pin(3);clear_pin(4);
				 clear_pin(5);clear_pin(6);clear_pin(7);
				 break;
	case 7: 		 //light segements a,b,c
				 clear_pin(1);clear_pin(2);clear_pin(3);
				 break;
	case 8:       //light segments a,b,c,d,e,f,g
	             clear_pin(1);clear_pin(2);clear_pin(3);
				 clear_pin(4);clear_pin(5);clear_pin(6);
				 clear_pin(7);
				 break;
	case 9: 		 //light segements a,b,c,f,g
				 clear_pin(1);clear_pin(2);clear_pin(3);
				 clear_pin(6);clear_pin(7);
				
}
}

I use two libraries:
#include "kernel.c"
#include "vector.c"


The final result when I run it is:
-User presses the button a random amount of times during a 10 second interval.
-The total number of button presses is displayed to PuTTY.
-The most significant digit is displayed on the 7-segment display. (Example: total number is 52 it will display 5.)
-Tells user to press button once more to display the 2nd digit. (If 52 the display will show 2 after the button is pressed.)
-Press button once more to run program again and show a "0" on the 7-segment display.

I'll post a picture of the PuTTY output later and maybe a picture of what the circuit looks like.
 


The circuit looks the same as it did with my last lab. Only the software needed to be changed. I posted the picture on another thread but I will post it again using the same old picture:
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…