ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
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.
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.
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.