Vex motor code help need

did you do a basic check if your motor-controller is working at all?

You should post your actual sketch as a code-section.

How should anybody give advice if

  • it is unclear if your code still has a bug?
  • it is unclear that your motor-controller is damaged through overvoltage?

technical things like microcontroller require

detailed and precise

information
if you don't like to give detailed and precise information
go search another hobby like throwing paint bubbles on a big wall to create randomly colored pictures

if you are too lazy to write more than three sentences go snap-chattering

So here is the update. I got the motor to kind of move but I think it was by mistake. I'm using a different power source that has a changeable voltage. I set it to 7.5V and uploaded my code. At first nothing happened but then I got rid of the GND wire that goes to the Arduino and then the motor started to move. The problem is it kind of moves randomly back in forth in intervals and I don't think it is reading my code because when I deleted the part that was supposed to control the motors movement it still moved. If I plug the GND wire back in then the motor stops moving. I have replaced both the motor and the motor controller and this happens no matter what. I even tried with 6V and it still does that.

here is the code I'm currently using


#include <Servo.h>

int MotorPin = 4;
Servo motor;

void setup() {

  motor.attach(MotorPin);
}

void loop() {

int motor(180);

}

The code you are using is creating a servo-pulse with which the servo

standing still

the function call

makes the code start creating a servo-pulse but the pulse of mid-position
On a continious rotation-servo mid-possition is the middle between fullspeed forward and full-speed backwards and this middle-position is speed zero

as far as I know the servo-library a function-call

int motor(180);

does nothing at all

Without a proper connection between ground of your extra power-supply and the arduino
the control-signal is floating uncontrolled which make the servo move somehow but completely uncontrolled.
The connection between ground of extra-power-supply and ground of arduino is a must for proper operation!

You still seem to just tinker around randomly instead of learning and knowing what you are doing.

Why don't you use a well proven example-code for testing the servo?
This is a demo-code delivered with the arduino IDE

/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
}

The code above should make your motor start rotating and the speed will increase
and at maximum-speed decrease until running full speed backwards
etc. etc. etc.

Can your digital voltage meter measure frequency?
If yes you could do a check if the IO-pin that shall work as the servo-control if there is a frequency of 50 Hz

If your digital multimeter can measure duty-cycle you could do a measuring if the duty-cycle is between 1% and 2%

and here the situation is: where a digital multimeter that can measure frequency and dutycycle would be very useful.

best regards Stefan

Thanks for all of this help. The vex motor actually moved. It does two spins to the right and then does two spins to the left which I believe is correct. However in my project, the motor has a gear with a belt on it. This belt has a wood rod sticking out. The program is started with a button which moves the belt down until the rod hits a limit switch. The motor switches directions making the belt with the rod go up. It then stops when it hits another limit switch. I'm not sure if this design will work with the servos since they are position based. I was thinking that I could program it to spins a certain amount of times before it switches directions since it would be going the same distance every time. I'm not sure if this is possible or the best approach.

Here are some pics of my mockup



Position based servos can only turn 90 to maximum 270 degree. which is a 3/4 rotation.

If your servo can rotate one full rotation or multiple rotations it is

not

position based.

non continious = position based servos:

  servo.write(90);: turn to middle-position and hold middle-position
  servo.write(0);: turn to   0 degree position and hold 0-degree position
servo.write(180);: turn to 180 degree-position and hold 180 degree-position

continous rotation servo = rotates on and on and on and on

 servo.write(90); : STOP rotation
 servo.write( 0);:full speed for clockwise rotation
 servo.write( 180);: degrees full speed for counter-clockwise rotation

 servo.write( 10); degrees: slow clockwise rotation
 servo.write( 100); degrees slow counterclockwise rotation

as a general advice:
unmount your servo from your device.
you should examine in small steps:

  • what happens if my code uses
servo.write(90);
  • what happens if my code uses
servo.write(95);
  • what happens if my code uses
servo.write(100);

etc. etc.

  • what happens if my code uses
servo.write(85);
  • what happens if my code uses
servo.write(80);
  • what happens if my code uses
servo.write(75);

to collect experience what does your servo do in dependancy of the angle given inside the parentheses of the function-call

servo.write();

If you want good advice how to get a certain way of functioning the first step is to describe this functionality in

normal words

make a new attempt which shall be more precise to describe your wanted functionality
in normal words

best regards Stefan

So as I'm running the program and just using the vex motor I've noticed a few things and I just wanted to ask you about them. First off when I properly wire and plug in the motor it just starts spinning continuously. Is this normal for the vex motor since it acts like a servo. Also when I run the example program you showed me I noticed that it spins clockwise twice and then spins counter clockwise twice. Unless I'm reading the code wrong, I though that the motor was only supposed to spin once in each direction.

Here is the program I'm using currently

#include <Servo.h>

Servo servo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position

void setup() {
  servo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // rotate from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo.write(pos);                   // tell servo to go to position in variable 'pos'
    delay(10);                          // waits 10ms for the servo to reach the position
  }

  for (pos = 180; pos >= 0; pos -= 1) { // rotate from 180 degrees to 0 degrees
    servo.write(pos);                   // tell servo to go to position in variable 'pos'
    delay(10);                          // waits 10ms for the servo to reach the position
  }
}

Tuning in late here, sry.

Do you have servo that goes to position, and stays where you told it to go, perhaps over a range of 0 to 180 degrees of sweep?

Or do,you have a continuous rotation servo, that goes at a speed in either direction depending on what you last told it?

a7

You seem to have forgotten what I have explained above in this thread about continiuous-rotation-servos

start variable with value zero
create servo-signal for "angle" zero.
On continious-rotation servos angle 0 means full speed clockwise
then the value of pos is increased
1
2
3
4
....
89 on continious-rotation servos 89 means rotate at slow speed clockwise
90
angle 90 on continious-rotation servos means rotate NOT

91 on continious-rotation servos means rotate at slow speed counterclockwise
92 on continious-rotation servos means rotate at little bit faster counterclockwise
etc.

second loop goes back from full speed countercockwise to zero and rising to full speed clockwise

You should observe what the motor is doing more carefully to discover such details as which rotational direction is the motor moving?
Maybe you should something on the motor-axle to make it easier to see

best regards Stefan

So the vex motor is a continuous servo

OK, so…

...some number near 90 should keep it from moving, or make it move slowly.

Try just feeding one number to the servo and see what value makes it move slowest. 90 +/-.

As @StefanL38 has explained more than one time I think, larger numbers will make it go one way, the larger the faster, and so forth.

I do not think VEX motors like that have any means for reporting how many revolutions they have made, so cannot be of any help in going only so far.

Unless the motor has a build-in encoder of some kind.

a7

So I did some testing and it looks like the motor does not move at 0,90, and, 180. It starts moving properly at 55

OK, so what do you get with, say 110? And 70?

Please try to see that tinkering with this can be dignified by calling it experimentation.

Design and perform some experiments. Make guesses (hypotheses) and test your growing understanding of what is in front of you.

Dare to be wrong. When wrong things happen, adjust your understanding and allow that to inform the design for new experiments.

If you aren't too clumsy, there's little danger of damaging much.

It's waht passes for fun for some of us.

a7

So it seems like for clockwise rotation 50-60 is the sweet spot for the fastest rpm. 120-130 seems like the spot with the best rpm for counter clockwise.

Motor moving. CW and CCW.

So we all done here, or what?

a7

When it comes to making the motor move properly yes. But I'm still working on wring and programing a start button and limit switches. I'll try some stuff and if I get stuck I'll ask . Thanks for all your help

So I have been try to make a start button for my prgram but it is not working. I think it is my wiring but it could be my code.

Here is my program

#include <Servo.h>

Servo servo;  // create servo object to control a servo
int buttonPin =8;

void setup() {
  servo.attach(9);  // attaches the servo on pin 9 to the servo object
   pinMode(buttonPin, INPUT);
}

void loop() {
  digitalRead(buttonPin);
  
   if (digitalRead(buttonPin) == HIGH) {
    servo.write(60);
    
  } else {
    servo.write(90);
  }
}

And here is an image of the wiring I'm using for the button
image

I'm not drawing power from the Arduino for the button. I'm using the same 9v that I'm using for the motor. I'm not sure if this is causing a voltage issue.

So I changed the button and it now started to work so I guess that was the issue. Now I just need help to figure out how to make the button toggle on with a push. In my final design I need the motor to start when it is pressed and stay on I'm not sure how to do that exactly.

can you read the signs?
image

No? me not either! Even if it is zoomed 500%
This is the reason why Fritzy pictures are unliked to the max

Now take a look at this super sketchy sketch:

One look and everything is clear.
Button wired with pull-down-resistor and connected to IO-pin 3
60 seconds hand drawn.

Just showing the relevant information. Leaving away all non-relevant information.

Here is a code that demonstrates using a button to start / stop a part of the code

#define unPressed HIGH
#define pressed   LOW

const byte ToggleButtonPin = 3;
boolean isActive = false;

unsigned long myTestTimer;

void setup() {
  Serial.begin(115200); // change baudrate in the serial monitor to 115200
  Serial.println("Setup-Start");
  pinMode (LED_BUILTIN, OUTPUT);  // used for indicating logging active or not
  digitalWrite(LED_BUILTIN, LOW);
  // wire button between IO-pin and GND
  // Pull-up-resistor inverts the logic
  // unpressed: IO-pin detects HIGH
  // pressed:   IO-Pin detects LOW
  pinMode(ToggleButtonPin, INPUT_PULLUP);
}

void myCodeToRun() {
  // check if 249 milliseconds time have passed by
  if ( TimePeriodIsOver(myTestTimer, 249) ) {
    // if really 249 milliseconds have passed by
    Serial.println("running");
  }
}

void wait() {
  // check if 498 milliseconds time have passed by
  if ( TimePeriodIsOver(myTestTimer, 498) ) {
    // if really 498 milliseconds have passed by
    Serial.println("waiting");
  }
}


void loop() {
  // code that shall be executed UN-conditional all the time
  
  // store toggle-switch-state into variable named "isActive"
  isActive = GetToggleSwitchState();

  if (isActive == true) { // only in case variable isActive is true
    digitalWrite(LED_BUILTIN, HIGH);
    myCodeToRun();
  }
  else { // variable isActive holds value false
    digitalWrite(LED_BUILTIN, LOW);
    wait();
  }
  
  // code that shall be executed UN-conditional all the time
}


// this function cntains ALL the details to use a momentary push-button as a toggle-switch
// push-button = contact only closed as long as the button is pressed down

// first press  => contact closed 
// second press => conact opened
// third press  => contact closed 
// fourth press => conact opened
// and so on and off and on and off....
bool GetToggleSwitchState() {
  // "static" makes variables persistant over function calls
  static bool toggleState     = false;
  static bool lastToggleState = false;

  static byte buttonStateOld = unPressed;
  static unsigned long buttonScanStarted  =  0;
  unsigned long buttonDebounceTime = 50;
  static unsigned long buttonDebounceTimer;

  byte buttonStateNew;

  if ( TimePeriodIsOver(buttonDebounceTimer, buttonDebounceTime) ) {
    // if more time than buttonDebounceTime has passed by
    // this means let pass by some time until
    // bouncing of the button is over
    buttonStateNew = digitalRead(ToggleButtonPin);

    if (buttonStateNew != buttonStateOld) {
      // if button-state has changed
      buttonStateOld = buttonStateNew;
      if (buttonStateNew == unPressed) {
        // if button is released
        toggleState = !toggleState; // toggle state-variable
      } // the attention-mark is the NOT operator
    }   // which simply inverts the boolean state
  }     // !true  = false   NOT true  is false
  //       !false = true    NOT false is true
  return toggleState;
}


// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

best regards Stefan