Programing Help

I need help writing a program because everytime I try it doesn’t want to work for me. This is what I want it to do;

Whem Limit Switch 1 is actuated, Motor One will rotate clockwise 22.5 clicks (i’m using shaft encoders in the program as well) or 90 degrees. Then Motor Two will rotate clockwise 55 clicks, or 100 degrees, clockwise. Then Motor One will will rotate counterclockwise 22.5 clicks, or 90 degrees. Then Motor Two will rotate counterclockwise 55 clicks, or 100 degrees.

I also want this to work if Limit Switch 1 is only momentarily actuated, as in, the sequence will still run if Limit Switch 1 is unactuated. Any help would be…well…helpful.

Easy C or MPLAB???

When Limit Switch 1 is Pressed and Released,
    a. Motor One will rotate clockwise 22.5 clicks of shaft encoder One.
    b. Then Motor Two will rotate clockwise 55 clicks of shaft encoder Two.
    c. Then Motor One will will rotate counterclockwise 22.5 clicks of shaft encoder One.
    d. Then Motor Two will rotate counterclockwise 55 clicks of shaft encoder Two.

Questions:
Can Limit Switch 1 be “held”, or will it always be Pressed and then Released?? Do you want the Motors Process to Start right after Limit Switch 1 is depressed, or wait until Limit Switch 1 is released?

Can Motor One and Motor Two run Simultaneously , (Steps a. & b.) or does Motor 1 need to finish its rotation before Motor 2 Starts. Can Steps c. & d. be performed the same way as Steps a. & b. ??

Is the Limit Switch 1 check performed Once, or Every time through the main While Loop, is there any other conditions that must be met before the Limit Switch 1 check??

  1. EasyC
  2. It will always be pressed and then released.
  3. I would like it to start as soon as the switch is depressed.
  4. They have to run in a sequence. a then b then c then d
  5. Limit switch one will only be activated that one time to signal the activation of the entire sequence. Then, after step d when both motors are back to their original position, nothing will happen until the switch is pressed again.

Thank’s for taking an interest in my dilema.

I like to Problem Solve!!! :wink:

I need to note that I don’t have the Optical Shaft Encoders. I also don’t have any of my Vex Controllers, there all on loan to the local Robotics Club…

The Optical Shaft Encoders Documentation is contradictory in that the documentation shows that the Encoder is plugged into the Analog/Digital I/O Block (the Vex Controller block with 16 plus 2 connectors), but EasyC only allows Interrupts 1-6, which seems to correspond with the Interrupt Block that is in line with the Motor Block. You will need to determine where the Optical Shaft Encoders accually plug in.

Here is the EasyC 2 Code, see Attached ZIP file for the EasyC Project…

#include "Main.h"

void main ( void )
{
      unsigned char limit_switch_1; 
      int optical_encoder_1; 
      int optical_encoder_2; 

      // Motor #1 is connected to Optical Encoder #1 
      // Motor #1 is connected to Motor Port #1 
      // Optical Encoder #1 is connected to Interrupt #1 
      // Motor #2 is connected to Optical Encoder #2 
      // Motor #2 is connected to Motor Port #2 
      // Optical Encoder #2 is connected to Interrupt #2 
      StartEncoder ( 1 ) ; // Start Reading Encoder #1
      StartEncoder ( 2 ) ; // Start Reading Encoder #2
      while ( 1 ) // Loop forever
      {
            if ( limit_switch_1 == TRUE  ) // Limit Switch #1 Pressed.
            {
                  // Start Part a. 
                  PresetEncoder ( 1 , 0 ) ; // Initial Value Encoder #1
                  optical_encoder_1 = 0 ; // Set Inital Value Encoder #1
                  SetMotor ( 1 , 255 ) ; // Start Motor #1 (Clockwise)
                  while ( optical_encoder_1 < 22 )
                  {
                        optical_encoder_1 = GetEncoder ( 1 ) ; // Read Value Encoder #1
                  }
                  SetMotor ( 1 , 127 ) ; // Stop Motor #1
                  // Start Part b. 
                  PresetEncoder ( 2 , 0 ) ; // Initial Value Encoder #2
                  optical_encoder_2 = 0 ; // Set Inital Value Encoder #2
                  SetMotor ( 2 , 255 ) ; // Start Motor #2 (Clockwise)
                  while ( optical_encoder_2 < 55 )
                  {
                        optical_encoder_2 = GetEncoder ( 2 ) ; // Read Value Encoder #2
                  }
                  SetMotor ( 2 , 127 ) ; // Stop Motor #2
                  // Start Part c. 
                  PresetEncoder ( 1 , 0 ) ; // Initial Value Encoder #1
                  optical_encoder_1 = 0 ; // Set Inital Value Encoder #1
                  SetMotor ( 1 , 0 ) ; // Start Motor #1 (Counter-Clockwise)
                  while ( optical_encoder_1 < 22 )
                  {
                        optical_encoder_1 = GetEncoder ( 1 ) ; // Read Value Encoder #1
                  }
                  SetMotor ( 1 , 127 ) ; // Stop Motor #1
                  // Start Part d. 
                  PresetEncoder ( 2 , 0 ) ; // Initial Value Encoder #2
                  optical_encoder_2 = 0 ; // Set Inital Value Encoder #2
                  SetMotor ( 2 , 0 ) ; // Start Motor #2 (Counter-Clockwise)
                  while ( optical_encoder_2 < 55 )
                  {
                        optical_encoder_2 = GetEncoder ( 2 ) ; // Read Value Encoder #2
                  }
                  SetMotor ( 2 , 127 ) ; // Stop Motor #2
            }
      }
}

I see a BUG… I should have initialized “limit_switch_1” to a known value, from reading the Limit Switch. The loop will most likely never happen, because the value of “limit_switch_1” never will change throughout the program…

Revised Code, with Check for Limit Switch.
Limit Switch #1 is connected to I/O Port #5 because the Default Configuration of the Vex Controller has I/O Ports #1-#4 set for Analog Input. If you know how to re-configure the Vex Controller, you can change the Port that you want to Digital Input…

#include "Main.h"

void main ( void )
{
      unsigned char limit_switch_1; 
      int optical_encoder_1; 
      int optical_encoder_2; 

      // Revision 001a, forgot to read Limit Switch #1 
      // Limit Switch #1 is connected to Digital Input #5 
      // Motor #1 is connected to Optical Encoder #1 
      // Motor #1 is connected to Motor Port #1 
      // Optical Encoder #1 is connected to Interrupt #1 
      // Motor #2 is connected to Optical Encoder #2 
      // Motor #2 is connected to Motor Port #2 
      // Optical Encoder #2 is connected to Interrupt #2 
      StartEncoder ( 1 ) ; // Start Reading Encoder #1
      StartEncoder ( 2 ) ; // Start Reading Encoder #2
      while ( 1 ) // Loop forever
      {
            limit_switch_1 = GetDigitalInput ( 5 ) ;
            if ( limit_switch_1 == TRUE  ) // Limit Switch #1 Pressed.
            {
                  // Start Part a. 
                  PresetEncoder ( 1 , 0 ) ; // Initial Value Encoder #1
                  optical_encoder_1 = 0 ; // Set Inital Value Encoder #1
                  SetMotor ( 1 , 255 ) ; // Start Motor #1 (Clockwise)
                  while ( optical_encoder_1 < 22 )
                  {
                        optical_encoder_1 = GetEncoder ( 1 ) ; // Read Value Encoder #1
                  }
                  SetMotor ( 1 , 127 ) ; // Stop Motor #1
                  // Start Part b. 
                  PresetEncoder ( 2 , 0 ) ; // Initial Value Encoder #2
                  optical_encoder_2 = 0 ; // Set Inital Value Encoder #2
                  SetMotor ( 2 , 255 ) ; // Start Motor #2 (Clockwise)
                  while ( optical_encoder_2 < 55 )
                  {
                        optical_encoder_2 = GetEncoder ( 2 ) ; // Read Value Encoder #2
                  }
                  SetMotor ( 2 , 127 ) ; // Stop Motor #2
                  // Start Part c. 
                  PresetEncoder ( 1 , 0 ) ; // Initial Value Encoder #1
                  optical_encoder_1 = 0 ; // Set Inital Value Encoder #1
                  SetMotor ( 1 , 0 ) ; // Start Motor #1 (Counter-Clockwise)
                  while ( optical_encoder_1 < 22 )
                  {
                        optical_encoder_1 = GetEncoder ( 1 ) ; // Read Value Encoder #1
                  }
                  SetMotor ( 1 , 127 ) ; // Stop Motor #1
                  // Start Part d. 
                  PresetEncoder ( 2 , 0 ) ; // Initial Value Encoder #2
                  optical_encoder_2 = 0 ; // Set Inital Value Encoder #2
                  SetMotor ( 2 , 0 ) ; // Start Motor #2 (Counter-Clockwise)
                  while ( optical_encoder_2 < 55 )
                  {
                        optical_encoder_2 = GetEncoder ( 2 ) ; // Read Value Encoder #2
                  }
                  SetMotor ( 2 , 127 ) ; // Stop Motor #2
            }
      }
}



bctc_rats-001a.zip (2 KB)