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

Code not working
http://www.robotc.net/forums/viewtopic.php?f=14&t=13929
Page 1 of 1

Author:  lazerblast8 [ Tue Apr 12, 2016 8:36 am ]
Post subject:  Code not working

I am trying to use a limit switch to count up to a set number and then run a motor for a set amount of time then stop after that I want it to keep counting until it reaches another amount and do the same thing as before.
Code:
#pragma config(Sensor, dgtl1,  limitSwitch,    sensorTouch)
#pragma config(Sensor, dgtl2,  limitSwitch2,   sensorTouch)
#pragma config(Motor,  port2,           motorOne,      tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port3,           motorTwo,      tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port4,           moterThree,    tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
   int w = 0;
   motor[port2] = 0;

   wait1Msec(2000);

   while (1==1)
   {
      waitUntil(SensorValue(limitSwitch) == 1);

      waitUntil (SensorValue(limitSwitch) == 0);

      w = w + 1;

      if (w == 5)

      motor[port2] = 100;

      wait1Msec(3000);
      motor[port2] = 0;

      if (w == 10)

      motor[port3] = 100;

      wait1Msec(3000);
      motor[port3] = 0;

      if (w == 15)

      motor[port4] = 100;

      wait1Msec(3000);
      motor[port4] = 0;
   }
}


Attachments:
File comment: This is the code
SourceFile002.c [931 Bytes]
Downloaded 138 times

Author:  CARBOT [ Tue Apr 12, 2016 11:43 am ]
Post subject:  Re: Code not working

The code that runs when the if statement is true should be in braces after the if statement.

For example:
if (w == 5)
motor[port2] = 100;
wait1Msec(3000);
motor[port2] = 0;
becomes
if (w == 5)
{
motor[port2] = 100;
wait1Msec(3000);
motor[port2] = 0;
}

Your next challenge may be to only have the if statement execute once.

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