Vex motor code help need

Here is my new code. Since I think its keeps reading buttonState as high I tried to declare it as LOW when button 2 is pressed but that didn't seem to do anything

#include <Servo.h>

Servo servo;  // create servo object to control a servo
int buttonPin=7;
int buttonPin2= 5;
int buttonPin3 =4;
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);
      buttonState==LOW;
    }
    
 

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

How are the buttons connected to the arduino uno?

The most important thing in debugging code is to make visible what is going on the code
The easiest to use tool for this is printing to the serial monitor.

Here is exactly your code. But with added serial printing to the serial monitor.
For the printing macros are used.
Macros are some kind of a "type on the keyboard" right before starting to compile.

open the serial monitor
adjust baudrate to 115200
activate timestamp
upload code to the arduino uno
then post the result you get in the serial monitor

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *

// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  } while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
// print only once when value has changed
#define dbgc(myFixedText, variableName) \
  do { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  } while (false);
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


#include <Servo.h>

Servo servo;  // create servo object to control a servo
const byte buttonPin = 7;
const byte buttonPin2 = 5;
const byte buttonPin3 = 4;
byte buttonState = 0;
byte buttonState2 = 0;
byte buttonState3 = 0;

byte actualServoPos = 0;

void setup() {
  Serial.begin(115200);
  Serial.println ("Setup-Start");
  servo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  Serial.println ("Setup done");
}

void loop() {
  buttonState  = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

  /* // add "//" on the LEFT to de-activate the block-comment
  // dbgi prints once every 1000 milliseconds
  dbgi("A:",buttonState,1000);
  dbgi("B:",buttonState2,1000);
  dbgi("C:",buttonState3,1000);
  */   // add "//" on the LEFT to de-activate the block-comment
  
  // if the value has changed
  // /*   // remove the LEFT "//" to activate block-comment
  // dbgc prints only one time if the value changed
  dbgc("A:",buttonState);
  dbgc("B:",buttonState);
  dbgc("C:",buttonState);
  // */   // remove the LEFT "//" to activate block-comment

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

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

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

  actualServoPos = servo.read(); //store actual position of servo into variable
  
  // each time the value of variable named "actualServoPos" changes 
  // the value will be printed to the serial monitor
  dbgc("S:",actualServoPos);
}

best regards Stefan
upload this code

Here is a pic of the wiring if that's what you are asking about

So when I start the program it seems fine. Buttonstate changes to 1 when I press it. Then when I press 2 it for some reason messes up. From the data I collect it it looks like the first button is the issue. Even when I hold the second button down , the first button seems to fluctuate randomly between 1 and 0. I'm not sure why this is happening. It seems to always want to be high which make it keep spin clockwise. When I press the second button it does change for a bit and but then button 1 goes back to being high and fluctuating randomly.

Can you please give hand-drawing a schematic of your project a try?

Show all the parts, how they are connected and always show where power is supplied and from where it comes.

Just pen and paper and shoot a picture of that.

TIA

a7

I forgot to label it but the rectangle at the top with the two lines coming out is the 9v battery

then post the result you get in the serial monitor

best regards Stefan

This indicates that the physical / electrical connections between the wires and the components of your setup are not reliable or that pulling-down is not working at all.

The wire comming from the IO-pin acts as an antenna that catches up electromagnetic noise which is present everywhere. And this electromagnetic noise is enough energy to make the IO-pin change randomly from LOW = 0 to HIGH = 1

This is the reason why you need the pull-down-resistor
You can read about this in this tutorial

how to connect button / switches to work reliably

Do you have a digital multimeter?
If you don't have a digital multimeter you really really should invest $20 to buy one.
If you tell me the country you live in I can lookup some online-shops to re-commend a digitalmutlimeter.

If you have a digital multimeter you should check conductance of the wiring.

possible reasons why this happens:

  • the pins of these tiny push-buttons are too short for a reliable connection to the breadboard

image

  • you plugged in the push-button so that closing the contact is not between left/right but instead between lower-side / upper-side

I think this is the second time that I write
learn the basics of electronics

They are important and if you don't want to stumble over some small things again and again and again you should really learn it

best regards Stefan

So, you're trying to drive a servo with a 9V battery?

Yes. It powers everything except the Arduino

A quick search for 9v battery and servos might be a good idea.

Here's one result:

Here is another that might look familiar:

I looked on the vex website and it says that the 393 motor draws somewhere between 20mA to 1.5A which is a pretty big range. I think it can handle (v but that seems to be on the high side of its voltage capability.

So i just change a few of the buttons again and it now seems to work properly. I'm not sure if it will continue to work or why that fixed it but it seems fine for now. I'm now gonna try to make vex limit switches work with my code Here is the code I used

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// Take it for granted at the moment scroll down to void setup
// start of macros dbg and dbgi
#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  } while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
// print only once when value has changed
#define dbgc(myFixedText, variableName) \
  do { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  } while (false);
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


#include <Servo.h>

Servo servo;  // create servo object to control a servo
const byte buttonPin = 7;
const byte buttonPin2 = 5;
const byte buttonPin3 = 4;
byte buttonState = 0;
byte buttonState2 = 0;
byte buttonState3 = 0;

byte actualServoPos = 0;

void setup() {
  Serial.begin(115200);
  Serial.println ("Setup-Start");
  servo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  Serial.println ("Setup done");
}

void loop() {
  buttonState  = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

  /* // add "//" on the LEFT to de-activate the block-comment
  // dbgi prints once every 1000 milliseconds
  dbgi("A:",buttonState,1000);
  dbgi("B:",buttonState2,1000);
  dbgi("C:",buttonState3,1000);
  */   // add "//" on the LEFT to de-activate the block-comment
  
  // if the value has changed
  // /*   // remove the LEFT "//" to activate block-comment
  // dbgc prints only one time if the value changed
  dbgc("A:",buttonState);
  dbgc("B:",buttonState2);
  dbgc("C:",buttonState3);
  // */   // remove the LEFT "//" to activate block-comment

  
  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);
  }

  actualServoPos = servo.read(); //store actual position of servo into variable
  
  // each time the value of variable named "actualServoPos" changes 
  // the value will be printed to the serial monitor
  dbgc("S:",actualServoPos);
}```

this is a very unprecise description of what you have done.
The only information that can be retrieved from your post is
you changed something
but it stays unclear what you have changed
You should post a very precise description of what you have changed

If you continue to be that unprecise nobody will be able to be of great help.
You will just annoy users that they have to ask back what you have done.

So please give a very detailed and very precise description of what you have changed.

best regards Stefan

Sorry for the unprecise information. The only things I changed were the start button which makes the motor spin counter clockwise and the stop button. That seemed to fix the issue. I am currently replacing the button that switches the direction the motor spins and the stop button with vex limit switches. I tried to just plug them in but that didn't work which makes sense. I'm not sure if I have to modify my code or wire them differently.

I also need to use a transmitter since the start button will be separate from the rest of the electronics. I did a bit of research and found that I could get either a 315mHz or 433mHz transmitter. I'm not too sure which one would be the best option or if it matters at all.

depending on what these limit-switches are it might require some kind of interface electronic.

As a general advice:
start writing a new posting. Do it like you do it.
Then if you want to click the "post" button
go through the following checklist

  • did I use a new hardware? (the vex-limit switch)
    have I added the datasheet of this hardware to my posting?
  • did I modify a single character in my code?
    if yes post the new code-version as a code-section
  • did I observe a behaviour of the code that is different from the behaviour that I expected
    if yes describe both behaviours the observed behaviour and the behaviour you want

This "sorry" is absolutely non-worthy if you don't add the precise information

if you continue to be unprecise like in your posting above
I will mute you as a user and concentrate on other users that are able to post precise information without beggin for it five times

If you try to be quick all that happens is slowing down
because you will just receive a question for the details you thought you would leave out.

To comment with the same unprecision like you:
if your vex-limit switches do not work connect them in the proper way?
If your code does not what you want correct your code

does the two sentences above help?
No not at all

What more information do you need exactly. I'm not asking in a rude manner I'm genuinely curious on what I'm leaving out. I am currently just trying to figure out how to make vex limit switches work with Arduino. I need the motor to first change direction when the first limit switch is hit. The motor then needs to stop when the second switch it hit. It is exactly the same as what the buttons do. Now the problem is I tried to just plug in the limit switches by adjusting the current wring I have. This didn't work and the motor did not move when I pushed the start button or either of the limit switches. My question is whether the limit switches require different code or different wiring. I read the vex article online and it says the black wire is ground and the white wire is the control signal which I understand. The red wire says no connect and I'm not sure what that means exactly.

Here is a picture of my current wiring

Here is a link to the limit switches I'm using

If you need anymore information just let me know I and I will try my best to provide it for you.

The description says
Switch Type: SPDT microswitch, configured for SPST Normally Open behavior. Behavior: When the limit switch is not being pushed in, the sensor maintains a digital HIGH signal on its sensor port. This High signal is coming from the Microcontroller.

This means the IO-pin should be configured as INPUT_PULLUP
which it is.

the code in post # 101 prints to the serial monitor each time one of the buttons is pressed
what do you see in the serial monitor if you push the limit switch?
Do you see that the value changes by pushing/releasing the limit-switch

SPDT microswitch, configured for SPST Normally Open behavior

So… it may mean that your switch action is upside down, that is to say that the logic is expecting to work with normally closed contacts.

Usually limit switches are NC normally closed, this affords the strongest protection as it would also stop the machine or whatever if a wire broke…

Where you digitally read switches is the place to correct for the sense (NC or NO) of the real switch.

For normally open contacts wired between INPUT_PULLUP mode input pins and ground:

// first
# define LIMIT_REACHED LOW
.
.
.

// everywhere

    bool limitTriggered = digitalRead(switchPin) == LIMIT_REACHED;

If you have normally closed contacts, just change LOW to HIGH in the # definition of LIMIT_REACHED.

We can't see your switches, or see what kind they already are, and I can't easily see you code on the tiny window under the umbrella here, so you'll just have to get out a c an if common sense and work this out with some tests and experiments.

a7