ROBOTC.net forums
http://robotc.net/forums/

Touch Sensors **SO CONFUSED**
http://robotc.net/forums/viewtopic.php?f=1&t=4687
Page 1 of 1

Author:  Simon1 [ Fri Jul 20, 2012 1:17 pm ]
Post subject:  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.

Author:  mightor [ Fri Jul 20, 2012 1:29 pm ]
Post subject:  Re: Touch Sensors **SO CONFUSED**

try using it like
Code:
nxtDisplayCenteredTextLine(4,"Took: %d", time1[T1]);


:)

Author:  Simon1 [ Fri Jul 20, 2012 1:38 pm ]
Post subject:  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"

Author:  Spiked3 [ Fri Jul 20, 2012 5:00 pm ]
Post subject:  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

Author:  Spiked3 [ Fri Jul 20, 2012 6:31 pm ]
Post subject:  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.

Author:  MHTS [ Fri Jul 20, 2012 6:40 pm ]
Post subject:  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.
}

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/