View unanswered posts | View active topics It is currently Tue Jul 22, 2014 4:12 am






Reply to topic  [ 11 posts ] 
How do I activate a lightSensor with a joystick button 
Author Message
Rookie

Joined: Sun Mar 25, 2012 7:59 pm
Posts: 7
Post How do I activate a lightSensor with a joystick button
How do I combine my lightSensor program and my joystick button program as one, so that when I press a button on the joystick, the sensor would come on and do what it is program to do, and when I press the button agian it will shut off. Moreoever, I would want to be able to drive the robot around as well and be able to use the other motors attached to it. Iam a rookie programmer and this is my first time working with the sensors. Thank you if you can help me. :-( :-( :-( :-( :-( :downloading:


Sun Mar 25, 2012 8:11 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: How do I activate a lightSensor with a joystick button
Can you provide a little more detail on exactly what you want the code to do? Also, have you already tried any code? If so, post it and we can work on it from there.

_________________
sudo rm -rf /


Sun Mar 25, 2012 9:25 pm
Profile
Rookie

Joined: Sun Mar 25, 2012 7:59 pm
Posts: 7
Post Re: How do I activate a lightSensor with a joystick button
#pragma config(Hubs, S1, HTMotor, HTMotor, HTMotor, none)
#pragma config(Sensor, S1, , sensorI2CMuxController)
#pragma config(Motor, motorA, CLAW, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, mtr_S1_C1_1, motorD, tmotorNormal, openLoop, reversed)
#pragma config(Motor, mtr_S1_C1_2, motorE, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_1, LegoArm, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_2, motorG, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C3_1, BowlingBall, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C3_2, Crate, tmotorNormal, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

#include "Joystickdriver.c"

task main()
{
float throttle;
throttle = 2;


while (1 == 1)
{
getJoystickSettings(joystick);

if (joy1Btn(5) == 1)
{
throttle = (0.5);
}
if(joy1Btn(6) == 1)
{
throttle = (3);
}

if(joy1Btn(4) == 1)
{
throttle = (1);
}

if(joy1Btn(2) == 1)
{
throttle = (4);
}

if (joy1Btn(9) == 1)
{
PlaySoundFile("Woops.rso");
wait1Msec(1040);
}

if(joystick.joy1_y2 < 5 && joystick.joy1_y2 > -5)
{
motor[motorE] = 0;
}
else
{
motor[motorE] = joystick.joy1_y2/(throttle);

}

if(joystick.joy1_y1 < 5 && joystick.joy1_y1 > -5)
{
motor[motorD] = 0;
}
else
{
motor[motorD] = joystick.joy1_y1/(-throttle);

}
if(joy1Btn(7)) // If Button 7 is pressed:
{
motor[LegoArm] = -100/2; // Motor G will go in reverse at full power
}
else

{
motor[LegoArm] = 0;

if(joy1Btn(8)) // If Button 7 is pressed:
{
motor[LegoArm] = 100/2; // Motor G will go in reverse at full power
}
else

{
motor[LegoArm] = 0;

}


if(joy1Btn(1)) // If Button 7 is pressed:
{
motor[CLAW] = 100; // Motor G will go in reverse at full power
}
else

{
motor[CLAW] = 0;

if(joy1Btn(3)) // If Button 7 is pressed:
{
motor[CLAW] = -100; // Motor G will go in reverse at full power
}
else

{
motor[CLAW] = 0;

}
if(joy2Btn(5)) // If Button 7 is pressed:
{
motor[BowlingBall] = 100/4; // Motor G will go in reverse at full power
}
else

{
motor[BowlingBall] = 0;

if(joy2Btn(6)) // If Button 7 is pressed:
{
motor[BowlingBall] = -100/4; // Motor G will go in reverse at full power
}
else

{
motor[BowlingBall] = 0;
}

if(joystick.joy2_y1/2 < 5 && joystick.joy2_y1/2 > -5)
{
motor[Crate] = 0;
}
else
{
motor[Crate] = joystick.joy2_y1;

}
}
}
}
}
}


Mon Mar 26, 2012 2:48 pm
Profile
Rookie

Joined: Sun Mar 25, 2012 7:59 pm
Posts: 7
Post Re: How do I activate a lightSensor with a joystick button
I meant to say colorSensor sorry I want the RGB color sensor to be activated and run its assigned program when I press a button on the joystick :?


Mon Mar 26, 2012 2:49 pm
Profile
Rookie

Joined: Sun Mar 25, 2012 7:59 pm
Posts: 7
Post Re: How do I activate a lightSensor with a joystick button
magicode wrote:
Can you provide a little more detail on exactly what you want the code to do? Also, have you already tried any code? If so, post it and we can work on it from there.

hello


Tue Mar 27, 2012 1:30 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: How do I activate a lightSensor with a joystick button
So if I'm understanding you correctly, you want to be able to keep the functionality that you have with your program now, but also have the light sensor code execute at the same time. I think that multitasking would be the easiest solution here, though there are other ways to do it.
Code:
task colorSensorTask(){
  //do something
  //if there is a while loop without any wait1Msec commands, use EndTimeSlice(); somewhere in the loop.
  //The reasoning for this is to let the other tasks have time to run
}

task main(){
  bool buttonWasPressed = false;
  while(1 == 1){
    getJoystickSettings(joystick);
    if(joy1Btn(some button)){
      if(!buttonWasPressed){
        StartTask(colorSensorTask);
        buttonWasPressed = true;
      }
    }else{
      buttonWasPressed = false;
    }
  }
}

_________________
sudo rm -rf /


Last edited by magicode on Wed Mar 28, 2012 12:57 am, edited 1 time in total.



Tue Mar 27, 2012 3:02 am
Profile
Rookie

Joined: Sun Mar 25, 2012 7:59 pm
Posts: 7
Post Re: How do I activate a lightSensor with a joystick button
Iam really need of help because I need to get this done before a deadline we have a competition and im like the only commited programmer. So that program you wrote on the forum that would tell the robot to start the color sensor when a button is pressed and turn it off when the button is pressed agian, can I just add that to my already written joystick program or do I have to do more complicated procedure. I will put this message on the forums agian.


Tue Mar 27, 2012 11:57 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: How do I activate a lightSensor with a joystick button
I didn't know that you wanted the task to stop once you pressed the button again. That changes the program a little:
Code:
task colorSensorTask(){
  //do something
  //if there is a while loop without any wait1Msec commands, use EndTimeSlice(); somewhere in the loop.
  //The reasoning for this is to let the other tasks have time to run
}

task main(){
  bool buttonWasPressed = false;
  bool taskWasStarted = false;
  while(1 == 1){
    getJoystickSettings(joystick);
    if(joy1Btn(some button)){
      if(!buttonWasPressed){
        if(taskWasStarted){
          StopTask(colorSensorTask);
          taskWasStarted = false;
        }else{
          StartTask(colorSensorTask);
          taskWasStarted = true;
        }
        buttonWasPressed = true;
      }
    }else{
      buttonWasPressed = false;
    }
  }
}


I have not tested this or the previous code as I do not have access ROBOTC or a robot to test it on at the moment. The logic should work, but I can't guarantee anything. I just used this to demonstrate how to go about solving the problem.

_________________
sudo rm -rf /


Wed Mar 28, 2012 1:03 am
Profile
Rookie

Joined: Sun Mar 25, 2012 7:59 pm
Posts: 7
Post Re: How do I activate a lightSensor with a joystick button
I see what you mean just alittle, but I need to know where do I add my joystick program to? and another question you could answer if you want, but I cant find the ColorSensor on the try me on the NXT or under the view option, Iam using a RGB colorSensor and the only two colors I need to distinguish is red and blue. I need a program, that will allow a motor to go, when the colorSensor senses a Blue color or value, and for the motor to turn of when there is a Red color or value. Also I need it where I press a button the program, which I wrote for the ColorSensor will activate, but at the same time Iam able to drive my robot and use the other controls on the joystick. Thank you


Wed Mar 28, 2012 1:10 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: How do I activate a lightSensor with a joystick button
You would put your joystick program in the main while loop, and the code for the color sensor inside the colorSensorTask. As I said, I do not have ROBOTC in front of me, so I cannot help you with the sample programs.

_________________
sudo rm -rf /


Wed Mar 28, 2012 1:30 am
Profile
Rookie

Joined: Sun Mar 25, 2012 7:59 pm
Posts: 7
Post Re: How do I activate a lightSensor with a joystick button
Lol I understand its ok, and I need a simple colorSensor program? The sample on on RobotC is hard to understand, can you decode it for me.


Wed Mar 28, 2012 2:50 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 11 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


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.