ROV control

Hi, I'm building an ROV. This is just part of my programming?

// constants won't change. They're used here to // set pin numbers: int buttonPin1 = 11; int buttonPin2 = 12; int motora_pwm = 10; int motora_dir = 6;

// variables will change: int buttonState1 = 0; int buttonState2 = 0;

void setup() { // initialize the motor pin as an output: pinMode(motora_pwm, OUTPUT); pinMode(motora_dir, OUTPUT);

// initialize the pushbutton pin as an input: pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT);

}

void loop(){ // read the state of the pushbutton value: buttonState1= digitalRead(buttonPin1); buttonState2= digitalRead(buttonPin2);

// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState1== LOW) {

analogWrite(motora_pwm,191); digitalWrite(motora_dir,HIGH);

} if (buttonState2== HIGH) {

analogWrite(motora_pwm,191); digitalWrite(motora_dir,LOW); } else { // turn motor off: analogWrite(motora_pwm, 0); digitalWrite(motora_dir, LOW); } }

I use a motor driver to drive the motor. When button 1 is being pushed, PWM pin is 191 meanwhile DIR pin is low.This will make the motor to rotate clockwise.When the button 2 is being pushed, PWM is 191 and DIR is HIGH. This will make the motor rotate counter clockwise. My problem now is that the PWM and DIR always give an output even though there is no is input. Can anyone help me to rewrite or fix this programming? Thanks.

You could help yourself by adding some debugging prints to get an idea of what your sketch is actually doing.

You are not enabling the pullup resistors on the switch pins. Therefore, you must have external pullup or pulldown resistors. Do you? How ARE your switches wired?

My problem now is that the PWM and DIR always give an output even though there is no is input.

What do you mean by "when there is no input"? There is always input. It just may not be what you are expecting.

What i mean by there is no input is that the input is not being connected to any switch.
There would be an output even though I didn’t connect the input to any switch.

What i mean by there is no input is that the input is not being connected to any switch.

I don't know where you came up with that idea. There will be input, whether there is anything connected to the pin, or not.

What I'm trying to say here is actually once I connect a jumper on the board (the other end being left unconnected) which is the input, the motor start to spin. I measured the voltage at the PWM pin and i get 3.2V once i connect a jumper on the input side. If i take out the jumper, the PWM pin will give 0V.

What I'm trying to say here is actually once I connect a jumper on the board (the other end being left unconnected) which is the input, the motor start to spin. I measured the voltage at the PWM pin and i get 3.2V once i connect a jumper on the input side. If i take out the jumper, the PWM pin will give 0V.

What we have here is a fundamental misunderstanding on your part.

You are assuming that with nothing connected to a pin, it will read LOW. That is not the case. Without pullup or pulldown resistors, a pin with nothing connected to it is floating. It's value is indeterminate.

You are not using the internal pullup resistors, and have made no mention of external pullup or pulldown resistors, so I will assume that you don't have them, since you say "with nothing connected".

If there is nothing connected to an INPUT pin, you shouldn't be reading it. If you are reading an INPUT pin, you MUST connect something to it (or use the internal pullup resistors to do that).