View unanswered posts | View active topics It is currently Thu Jul 31, 2014 3:04 am






Reply to topic  [ 3 posts ] 
Latching output on Digital I/O ports 
Author Message
Rookie

Joined: Tue Oct 23, 2012 2:31 pm
Posts: 1
Post Latching output on Digital I/O ports
I need to be able to raise a digital output port to the high condition and keep it high until I shut it off. The processor needs to execute other code (eg. steering the robot) while the output port is latched high. Please tell me what commands do this. Thank you.


Tue Oct 23, 2012 2:37 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Latching output on Digital I/O ports
SensorValue[port] = 0 and SensorValue[port] = 1 can be used to set a digital port high or low.

_________________
sudo rm -rf /


Tue Oct 23, 2012 2:43 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 558
Post Re: Latching output on Digital I/O ports
If you wish to have the lights continually blinking, this would actually be a good time for multitasking.

We can modify the example from the multitasking thread (below) to suite our needs:

Code:
task e_stop()
{
   while(true)
   {
      if(SensorValue(e_stopBtn) == 1)
      {
         StopAllTasks(); // this ends the program
      }
      wait1Msec(10);  // this prevents the current task from hogging the CPU
   }
}


task main()
{
   StartTask(e_stop);
   while(true)
   {
      if(SensorValue(sonar) <= 10)
      {
         motor[rightMotor] = 0;
         motor[leftMotor] = 0;
         motor[vacuumMotor] = 0;
      }
      else
      {
         // code for driving around room here
      }
      wait1Msec(10);  // this prevents the current task from hogging the CPU
   }
}


We are going to focus on just the light blinking part for now. Since it is a 'secondary' part of the code (the driving being the 'main' part), we will give the blinking its own task

Code:
#pragma config(Sensor, dgtl1,  digitalPort1,   sensorDigitalOut)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task blink()
{
   while(true)
   {
     
    ClearTimer(T1);
    while(time1[T1]<2000)
       {
        SensorValue[digitalPort1]=1;
      }
     
    ClearTimer(T1);
    while(time1[T1]<2000)
       {
        SensorValue[digitalPort1]=0;
      }
     
   }
}


The first line, the #pragma statement, is created by ROBOTC automatically when we set digital port 1 as a digital output (through the Motors and Sensors Setup). Next, we have the actual task; just like task main, task blink gets a name (in this case, 'blink') and a set of parentheses (). We want the lights to continually blink, so we create an infinite loop 'while(true)' that continually runs the code contained inside of it.

Code:
 ClearTimer(T1);
    while(time1[T1]<2000)
       {
        SensorValue[digitalPort1]=1;
      }
     
    ClearTimer(T1);
    while(time1[T1]<2000)
       {
        SensorValue[digitalPort1]=0;
      }


The ClearTimer(T1) command clears the value of the T1 timer and resets it back to 0, which then allows us to use it in another while loop to control how long the light is on 'while(time1[T1] < 2000)' (since the time1 command counts in milliseconds, we wait for 2000 milliseconds, which is 2 seconds). Once the light is on for the appropriate amount of time, we clear the T1 timer and use another while loop to control how long the light is turned off (it is almost exactly the same as the previous loop; the only difference is that digitalPort1 (the alias we gave digital port 1 in the Motors and Sensor setup window) is turned off instead of on.

Code:
task main()
{
   StartTask(blink);
   while(true)
   {
      //Other code (steering, etc) goes here
      wait1Msec(10);  // this prevents the current task from hogging the CPU
   }
}


The final pieces of the code (and by far the most important) is the task main. The first thing task main does is starts the 'blink' task using the StartTask() command. We make one final infinite loop and place all other appropriate code inside of it, and place a 10 millisecond wait command at the end of it to prevent the task from taking over the CPU.

When the code is run, both tasks will run *at the same time* and flash the LED (or in your particular case, drive the switch) while at the same time enabling the steering and control code.

For more information on multitasking, definitely take a look at the multitasking thread and the multitasking tips section of our wiki (special thanks to community member jpearman for his contributions to multitasking in ROBOTC.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Tue Oct 23, 2012 4:00 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 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.