View unanswered posts | View active topics It is currently Tue Sep 16, 2014 3:53 pm






Reply to topic  [ 6 posts ] 
Touch Sensors **SO CONFUSED** 
Author Message
Rookie

Joined: Thu Jul 19, 2012 11:59 am
Posts: 10
Post 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
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3221
Location: Rotterdam, The Netherlands
Post Re: Touch Sensors **SO CONFUSED**
try using it like
Code:
nxtDisplayCenteredTextLine(4,"Took: %d", time1[T1]);


:)

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Jul 20, 2012 1:29 pm
Profile WWW
Rookie

Joined: Thu Jul 19, 2012 11:59 am
Posts: 10
Post 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
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post 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 :P

_________________
Mike aka Spiked3
http://www.spiked3.com


Fri Jul 20, 2012 5:00 pm
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post 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
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Touch Sensors **SO CONFUSED**
An alternative way to test whether a number is odd or even.
Code:
if (rand1 & 0x01)
{
    // rand1 is odd.
}
else
{
    // rand1 is even.
}


Fri Jul 20, 2012 6:40 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 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

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.