|
Page 1 of 1
|
[ 6 posts ] |
|
Touch Sensors **SO CONFUSED**
| Author |
Message |
|
Simon1
Rookie
Joined: Thu Jul 19, 2012 11:59 am Posts: 10
|
 Touch Sensors **SO CONFUSED**
I am having a really annoying problem. To me, my code looks fine, but when I debug it everything goes wrong. I am trying to get a program to see what someones reaction time is. When the touch sensor is 1 and my random number is even then they should hit the right touch sensor and if the number is odd they should hit the left touch sensor. For some reason everything is going wrong. Code:  |  |  |  | Code: #pragma config(Sensor, S1, touchRight, sensorTouch) #pragma config(Sensor, S4, touchLeft, sensorTouch)
task main() { while(true) { int rand1 = random[10]; wait1Msec(1000); nxtDisplayCenteredBigTextLine(4,"Ready?%d"); wait1Msec(1500); nxtDisplayCenteredBigTextLine(4,"GO!%d"); wait1Msec(500); nxtDisplayCenteredBigTextLine(4,"%d"); nxtDisplayTextLine(4,"Number: %d", rand1); wait1Msec(1000); time1[T1] = 0;
SensorValue(touchRight) = 0; SensorValue(touchLeft) = 0; if(SensorValue(touchRight) == 1 && rand1 == 0 || 2 || 4 || 6 || 8 || 10) { wait1Msec(1); nxtDisplayCenteredTextLine(4,"Took: %d", T1); } if(SensorValue(touchRight) == 0) { wait1Msec(1); nxtDisplayBigTextLine(4,"You Lose %d"); } if(SensorValue(touchLeft) == 1 && rand1 == 1 || 3 || 5 || 7 || 9) { wait1Msec(1); nxtDisplayCenteredTextLine(4,"Took: %d", T1); } } } |  |  |  |  |
It seems everything has gone wrong and I have completely lost my patience. The program is also supposed to record the time.. That's why I have time1. Any help would be fantastic. I have never gotten so annoyed at a program.
|
| Fri Jul 20, 2012 1:17 pm |
|
 |
|
mightor
Moderator
Joined: Wed Mar 05, 2008 8:14 am Posts: 2860 Location: Rotterdam, The Netherlands
|
 Re: Touch Sensors **SO CONFUSED**
try using it like 
_________________| Some people, when confronted with a problem, think, "I know, I'll use threads," | and then two they hav erpoblesms. (@nedbat)| My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
| Fri Jul 20, 2012 1:29 pm |
|
 |
|
Simon1
Rookie
Joined: Thu Jul 19, 2012 11:59 am Posts: 10
|
 Re: Touch Sensors **SO CONFUSED**
Yes! That helped but I'm still having an issue with it.  |  |  |  | Code: #pragma config(Sensor, S1, touchRight, sensorTouch) #pragma config(Sensor, S4, touchLeft, sensorTouch)
task main() { while(true) { int rand1 = random[10]; wait1Msec(1000); nxtDisplayCenteredBigTextLine(4,"Ready?%d"); wait1Msec(1500); nxtDisplayCenteredBigTextLine(4,"GO!%d"); wait1Msec(500); nxtDisplayCenteredBigTextLine(4,"%d"); nxtDisplayTextLine(4,"Number: %d", rand1); wait1Msec(1000); time1[T1] = 0;
SensorValue(touchRight) = 0; SensorValue(touchLeft) = 0; if(SensorValue(touchRight) == 1 && rand1 == 0 || 2 || 4 || 6 || 8 || 10) { wait1Msec(1); nxtDisplayCenteredTextLine(4,"Took: %d", time1[T1]); wait1Msec(1500); } if(SensorValue(touchRight) == 0) { wait1Msec(1); nxtDisplayBigTextLine(4,"You Lose %d"); wait1Msec(1500); } if(SensorValue(touchLeft) == 1 && rand1 == 1 || 3 || 5 || 7 || 9) { wait1Msec(1); nxtDisplayCenteredTextLine(4,"Took: %d", time1[T1]); wait1Msec(1500); } if(SensorValue(touchLeft) == 1 && rand1 == 0 || 2 || 4 || 6 || 8 || 10) { wait1Msec(1); nxtDisplayBigTextLine(4,"You Lose %d"); wait1Msec(1500); } if(SensorValue(touchRight) == 1 && rand1 == 1 || 3 || 5 || 7 || 9) { wait1Msec(1); nxtDisplayBigTextLine(4,"You Lose %d"); wait1Msec(1500); } } } |  |  |  |  |
It seems to glitch out and show "You Lose" after I had clicked the correct touch sensor and then it resets and says "Took: 3003"
|
| Fri Jul 20, 2012 1:38 pm |
|
 |
|
Spiked3
Expert
Joined: Tue Feb 28, 2012 3:10 pm Posts: 195
|
 Re: Touch Sensors **SO CONFUSED**
"if(SensorValue(touchRight) == 1 && rand1 == 0 || 2 || 4 || 6 || 8 || 10)" sorry, 'if' doesn't work that way  I could make you look that up, but you'd find the answer pretty quick so I'll just tell you; if (rand1 == 0 || rand1 == 2 || rand1 == 4 .... etc ) woohoo, I'm now an expert 
_________________Mike aka Spiked3 http://www.spiked3.com
|
| Fri Jul 20, 2012 5:00 pm |
|
 |
|
Spiked3
Expert
Joined: Tue Feb 28, 2012 3:10 pm Posts: 195
|
 Re: Touch Sensors **SO CONFUSED**
just to take it a step further, it looks like you are checking for odd or even?
two things, you could limit your random number range to 0 and 1 and get the same results.
Or you code use the 'mod' operator, rand1 % 2 == 0 will be 0 for even numbers, and == 1 for odd.
try it both ways, add them to your bag of tricks.
To be honest I haven't tested this in RobotC - the help file seems to be missing documentation on rand, but it would work assuming rand[] does what you expect.
_________________Mike aka Spiked3 http://www.spiked3.com
|
| Fri Jul 20, 2012 6:31 pm |
|
 |
|
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1023
|
 Re: Touch Sensors **SO CONFUSED**
An alternative way to test whether a number is odd or even.
|
| Fri Jul 20, 2012 6:40 pm |
|
|
|
Page 1 of 1
|
[ 6 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 2 guests |
|
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 post attachments in this forum
|
|