Vex motor code help need

never never NEVER apply more than 5V to your arduino !!!!

If you want to connect anything that has more than 5V ask in the forum how to do it properly
Your knowledge-level about electronics is sooooo basic that you are in high danger of destroying your arduino if you do it on your own.

Of course you can drive higher voltages than 5V but you have to do it the right way.

If you want to go beyond asking for each and every detail how to wire things.
I highly recommend that you learn the basics of electronics.

The microcontroller-world is

not

super-standardised like USB-devices
In the microcontroller-world you have to take care of many more details than just
.
"does the plug fit into the socket?"
.
You can proceed in two ways

  1. needing time to order new components because you have destroyed them by not knowing what you are doing

2.needing time for learning electronics

best regards Stefan

So I'm trying to modify the code so it work with the vex motor. This is what I've done so far

#define unPressed HIGH
#define pressed   LOW
#include <Servo.h>

Servo servo;  // create servo object to control a servo 
const byte ToggleButtonPin = 8;
boolean isActive = false;

unsigned long myTestTimer;

void setup() {
  Serial.begin(115200); // change baudrate in the serial monitor to 115200
  Serial.println("Setup-Start");
  servo.attach(9);  // attaches the servo on pin 9 to the servo object
  
 
  // 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 (digitalRead(ToggleButtonPin) == HIGH) {
    servo.write(60);
    
  } else {
    servo.write(90);
  }
}

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
}



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) ) {
    
    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
}```

I haven't gotten it to work yet

I also tried this but it didn't work. The motor did not move when I pushed the button.'

#include <Servo.h>

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

int motorState; 
int buttonPin =8;
int buttonNew;
int buttonOld;


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

}

void loop() {
  buttonNew = digitalRead(buttonPin);
  if(buttonOld=0 && buttonNew== 1)
  {
    if (motorState==0)
    {
      servo.write(60);
      motorState=1;
    }
    else{
      servo.write(90);
      motorState=0;
    }
    
  }
  buttonOld=buttonNew;
}

That expression is never true.

Look carefully, perhaps after e viewing the difference between == and =.

That might not be all your problem but it's a show stopper for sure.

a7

You haven't yet understood something very very basic about arduino-programs:

function setup runs down a single time directly after power-on

after that function loop() is doing what its name says:

looping infinitely

This means any code that shall be executed once after pwer-on must be inside function
setup()

All code that shall be executed repeatedly must be inside function loop()

In your modification of my demo-code you left function loop() empty

you just left the comment inside. This comment is saying the same thing

For analysing what your code is really doing you should learn how to add serial printing to your code.

Adding serial printings is pretty easy. And having learned it will save you many many hours of time scratching your head why your code might not behave like you thought.

In general I recommend that you work through this tutorial
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

There are basically two ways of learning programming

1 way which I do not recommend
stumbling over a detail like if-conditions require always a double-equal-sign for compairing
which you missed

has to be

// wrong single-equalsign if(buttonOld=0 && buttonNew== 1)
// right double-equal-sign if(buttonOld==0 && buttonNew== 1)`
if(buttonOld == 0 && buttonNew == 1)

stumbling again and again and again and again over some small detail
always waiting for an detail-answer

or way 2 which I recommend
working through a basic tutorial to learn the most important details and after that beeing able to start through with coding always based on a working code which you modify in small steps
to keep the space where the bug might be small for fast finding the bug.
learning how to use serial printing for analysing and if you can't conclude from the serial printing asking in the forum.

best regards Stefan

So I'm trying something new that I've seen work. I have two buttons and when one is pressed the motor spins cw. When the second button is pressed the motor spins ccw. I wired everything up and at first this program started working. I then unplugged ground to make the motor stop. When I plugged ground back in the buttons stooped working. The motor just spins and it seems to not detect the buttons. I believe the wiring is correct so am assuming its a programming error. I also am still unfused on how to make is toggle and not have it to where I have to hold the button. All the examples I've found have LED's and I'm not sure how to make them work with my motor.

#include <Servo.h>

Servo servo;  // create servo object to control a servo
int buttonPin=8;
int buttonPin2= 7;
int buttonPin3=6;
int buttonState;
int buttonState2;


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

void loop() {
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
 
  
   if (buttonState == HIGH && buttonState2==LOW ) {
    servo.write(60);
   
  } 
    if (buttonState == LOW && buttonState2==HIGH  ){
      servo.write(120);
    }
 
}

As a reminder this is the vex program I have currently that works. I'm trying to make this in Arduino. For now I'm using buttons instead of limit switches

#pragma config(Sensor, dgtl1,  start,          sensorTouch)
#pragma config(Sensor, dgtl2,  top,            sensorTouch)
#pragma config(Sensor, dgtl3,  bottom,         sensorTouch)
#pragma config(Motor,  port1,           topmotor,      tmotorVex393_HBridge, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
	while (1==1)
	{
	untilTouch(start);
	motor(topmotor)=-50;
	untilTouch(bottom);
	motor(topmotor)=50;
	untilTouch(top);
	motor(topmotor)=0;
}

}

You said it. Not because of a code change, but because you were messing with the wires.

So it's now a wiring issue, or you messing around with the wires made something break.

You should never change the wiring whilst the power is on. Even ppl who know what they are doing will avoid that. It's a good way to break things.

a7

1 Like

motors are inductive loads. If you disconnect power from an inductive load this will create a voltage-spike with a much higher voltage than the normal supply-voltage.

Additionally if you connect the voltage-supply even just for 0,01 seconds to the wrong thing
you might create a shortcut and then even 0,01 seconds can be long enough to burn through a component.

Again: This is the reason why you should learn elementary basics of electronics like ohms law
You might think "It take too long to switch off power each time I want to change a single wire.

Well compare the switch-on / switch-off time with the time you need to order and wait for a new component. It's a nobrainer what takes less time.

If you want to be save you could use electronic simulator (which of course will take some time to learn how to use it)

best regards Stefan

So this program I'm using works but after a while it seems to break. After I ran it a couple times the motor started to just spin cw even when I press the other buttons. I have to hold the button to make the motor change which isn't what I want to happen. It should be just a tap. It worked like that at first but now it broke. I'm not sure if it is a code error or something else

#include <Servo.h>

Servo servo;  // create servo object to control a servo
int buttonPin=8;
int buttonPin2= 7;
int buttonPin3 =6;
int buttonState=0;
int buttonState2=0;
int buttonState3 =0;


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

void loop() {
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
 
  
   if (buttonState == HIGH && buttonState2==LOW && buttonState3 == LOW ) {
    servo.write(60);
   
  } 
     if (buttonState == LOW && buttonState2==HIGH && buttonState3 == LOW  ){
      servo.write(120);
    }

    if (buttonState == LOW && buttonState2==LOW && buttonState3 == HIGH )
    {
      servo.write(180);
    }


 
}

How are your switches wired?

It looks like you are using pull down resistors externally, and expect a pressed button to show HIGH.

Better: use no resistors externally, specify INPUT_PULLUP for your ponMode() calls and expect pressed buttons to read LOW.

If you didn't change the software, it must be a hardware issue.

a7

I'm currently using 10k ohm resistors for the buttons cause that what I saw other people use. Are you saying I should get rid of those resistors

You can use them with INPUT mode.

I was suggesting moving to the more common wiring of your buttons, the pull-up configuration where

the resistor goes between the input pin and Vcc and

the switch goes from the input pin to GND and

you look for the opposite condition in testing the switches.

I like using the resistors, but with pull-up mode, you can usually just use INPUT_ PULLUP mode and exploit the bike in pull-up capability of the pin.

But all those different ways work, and one of them worked for you before, so that isn't probably the matter.

How are you powering the servo motor?

a7

I'm powering everything using an external 9v battery

If you mean a PP3 smoke detector type 9 volt battery I suggest you find a better source of power.

You can run your Arduino board and use 6 volts form four AA batteries to power the servo.

Inadequate power is a common problem with servos. Perhaps your 9 volt battery was up to the challenge for awhile and is no longer.

a7

only in case you use pinMode(myPin, INPUT _PULLUP)

whenever you use pinMode(myPin, INPUT)
an external pull-down or pull-up-resistor is required for proper function

If I remember right I already gave you the advice of learning the basics of electronics.
The microcontroller-world is not super-standardised like USB-devices
You have to take care of more details than just
"does the plug fit into the socket?"

about buttons and switches you should read this tutorial

best regards Stefan

I'm powering the board using the computer. Everything else is supplied by the 9v.

I checked the voltage of my battery and it is at 7.5v. Would that be too low to power the servo and the three buttons

I have changed the power supply, the motor, and all the buttons and it still doesn't work. For some reason it seems to always reads the first button as high. When I start the program the motor instantly start to move CW and I have to hold the other buttons to make it change or stop. I'm not sure why this is happening or how to fix it. I tried the INPUT_PULLUP and that didn't change anything.

the must substantial
most important thing for helping is
posting always new your actual sketch.

I have no glassphere to look into
to see your actual code.

post your complete sketch even in case you it is the exact sketch post it new as a code-section

best regards Stefan