LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-26-2014, 02:48 PM   #1
nascarbuff17
LQ Newbie
 
Registered: Oct 2014
Posts: 3

Rep: Reputation: Disabled
Help find the problem in my Program


Hello all, first post here.

I am trying to compile a program to take two reaction times and display the winner for an assignment. There are no compile errors, but it still does not work as it should...Find it below;

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // sleep function include
#include <wiringPi.h>

int main()
{
int readport, readportB, timeCounterA, timeCounterB;

if (wiringPiSetup() < 0)
{
fprintf(stderr, "Unable to initialise wiringPi\n");
return 1;
}

// Set GPIO 23 (WiringPi 4) for input
// Set GPIO 24 (WiringPi 7) for input
// Set GPIO 17 (WiringPi 0) for output
// Set GPIO 18 (WiringPi 1) for output

pinMode(0, OUTPUT);
digitalWrite(0, LOW);
pinMode(1, OUTPUT);
digitalWrite(1, LOW);
pinMode(4, INPUT);
pullUpDnControl(4, PUD_DOWN);
pinMode(6, INPUT);
pullUpDnControl(6, PUD_DOWN);

printf("Before the while loop\n");

while (1) {
do{
readport=!digitalRead(4);
printf("Pushbutton is %d\n", readport);
}while (readport == 1);

printf("Pushbutton is down. Hold until it turns RED\n");
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
usleep(1000000);
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
timeCounterA = 0;
do{
readport=!digitalRead(4);
usleep(1000);
timeCounterA = timeCounterA + 1;
} while (readport !=1);
digitalWrite(1, LOW);

printf("Your time was %d ms.\n", timeCounterA);
sleep(2);

do{
readportB=!digitalRead(6);
printf("Pushbutton is %d\n", readportB);
}while (readportB ==1);

printf("Pushbutton is down. Hold until it is RED\n");
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
usleep(1000000);
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
timeCounterB = 0;
do{
readportB=!digitalRead(6);
usleep(1000);
timeCounterB = timeCounterB + 1;
} while (readportB !=1);
digitalWrite (1, LOW);

printf("Your time was %d ms.\n", timeCounterB);


if (timeCounterA < timeCounterB);
{
printf("Player A is the winner!\n");
}
if (timeCounterB < timeCounterA);
{
printf("Player B is the winner!\n");
}
if (timeCounterA=timeCounterB);
{
printf("Its a tie!\n");
}

}

return 0;
}

Whats supposed to happen is this; when an LED turns colour, player 1 is suppose to press a button, then his reaction is timed and shown, followed by the player two. Player 1 works properly but...

When player two plays, the reaction time is not shown nor does my program go to my if statements to display the results of the game (i.e. winner, tie). This is suppose to happen, but after it times player 2 the program resets.

Help? Thanks in advance.
 
Old 10-26-2014, 06:15 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Welcome to LinuxQuestions.

It appears that the code is the same between player 1 and 2 except for the switch i.e. GPIO pin. I assume some of your code is for debugging purposes. What other things have you already tried?

In the program's present state once player 2 releases the switch the program will go back to the top of the loop so you should see the push button status continuously and the winner/tie messages will drop of the display rather quickly. In addition you did not indicate if the player B switch operated correctly.

So it is difficult to know if your problem is software or hardware at first glance. I would first comment out the while (1) loop. This will cause the program to stop after the first test making debugging a little easier.
 
1 members found this post helpful.
Old 10-26-2014, 06:41 PM   #3
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Some things you should do:
1) Use code tags so the source code is more readily visible.
2) Add more comments to the code, not everyone programs at such a low level.
3) Add debugging code, printfs, asserts, etc.
4) Your method of timing isn't very accurate, I would use clock_gettime in monotonic mode for your timer instead of using delays.

If you need an example of using clock_gettime, I wrote a timer program a while back:
https://www.linuxquestions.org/quest...7/#post5149129

Last edited by metaschima; 10-26-2014 at 06:42 PM.
 
Old 10-26-2014, 07:00 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Since this is the OP's first post I was going to let the code tags go but they are are available by selecting the go advanced button and clicking on # or you can surround your code with . You will have to repost it since the formatting is now gone.

I agree about the timer function but this is an assignment and since we do not know what has already been taught I am reluctant to suggest major changes.
 
Old 10-26-2014, 07:18 PM   #5
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Well, if the assignment doesn't require accurate timing then I guess just continue using delays.
 
Old 10-26-2014, 08:21 PM   #6
nascarbuff17
LQ Newbie
 
Registered: Oct 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for the response guys! after player 2 plays, it goes back to player 1 without showing his time. It just alternates between 1 and 2. I had a feeling the while loop wrong. I'll admit this is a low level of programming but it is part of my course that I am very new to so far.

I apologize for the lack of comments, An accurate description of the program;
Once executed, player 1 must press the first pushbutton. The LED will go green, and once it changes to red you must let go and it times your reaction and proceeds to player 2, who must do the same with the second push button. His time is shown, and then the winner is displayed. The commands I used are mostly all the ones I am familiar with. michaelk, once player two plays, his time must display and the if statement needs to display the winner.
 
Old 10-26-2014, 08:26 PM   #7
nascarbuff17
LQ Newbie
 
Registered: Oct 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Case closed, the program works properly now. Thanks for your suggestions guys, and next time I will use code tags to be more descriptive.

Last edited by nascarbuff17; 10-26-2014 at 08:45 PM.
 
Old 10-26-2014, 08:45 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Check the syntax for your if statements. The semicolons should not be there. The if statement for a tie is also wrong. Lets see if you can figure that one out...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How does a program "find" another file or program when it calls one? pottzie Programming 8 11-10-2011 10:07 PM
problem: can't find any program in synaptic Package manager davidbelo Ubuntu 2 12-15-2008 02:34 PM
can't find my program boncrs Linux - Desktop 10 04-21-2007 03:59 PM
cant find program Circuit Monkey Linux - Newbie 3 03-25-2005 04:36 AM
problem in : program to find which all data blocks are allocated udayan Programming 0 07-03-2002 08:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration