I need to know how to reverse the current running through my motors

I’m trying to get my robot to turn right when a sensor activates. so far i got a single motor to stop turning for a set amount of time, which would turn the robot but the wheels stop moving forward when it runs into something. Now I’m trying to make a single motor spin the other way for a set amount of time. I need a command or a few lines of code the would do this. here is my code and below are pictures of my robot. I’m using an Arduino Uno.

int Motor1Speed = 10;
int Motor1Control11 = 9;
int Motor1Control12 = 8;
int Motor2Speed = 11;
int Motor2Control11 = 13;
int Motor2Control12 = 12;
int speed= 255;
int sensorPin = 7;
int pinState;

void setup () {
 
  
  analogWrite(Motor1Speed, speed);
  analogWrite(Motor2Speed, speed);
  pinMode(Motor1Speed, OUTPUT);
  pinMode(Motor1Control11, OUTPUT);
  pinMode(Motor1Control12, OUTPUT);
  pinMode(Motor2Speed, OUTPUT);
  pinMode(Motor2Control11, OUTPUT);
  pinMode(Motor2Control12, OUTPUT);
  pinMode(sensorPin, INPUT);

  Serial.begin(9600);
}

void loop(){
  digitalWrite(Motor1Control11, HIGH);
  digitalWrite(Motor1Control12, LOW);
  digitalWrite(Motor2Control11, HIGH);
  digitalWrite(Motor2Control12, LOW);
   pinState = digitalRead(sensorPin);
 
  if (pinState == true){

  Serial.println("pressed");
  
  

  analogWrite(Motor1Speed,-255);
  delay(1000);

  }else{
    Serial.println("not pressed");
    
}
}

when i run this the wheels on my robot turn and the monitor says if the switch is pressed or not.


it looks like that but with one sensor
top view:


the wire that plugs into the aruino is connected to a 9V battery. The pair of red and black ones that go around the side are connect to 4 AA batteries. the red white and black are the sensor, and finally the the red and black wires the come up through holes are motors that have wheels attached to the side.

What actually happens when you run the program? Give as much detail as possible.

...R

i have 2 motors on an mold. the mold has a breadboard and an arduino uno on it. i also have a sensor that i put on the front to tell if the robot has something blocking its path. When this is pressed i want my robot to turn 90 degress. i tried running the code turn but it says turn was not defined and dosent work. want to add a line of code in the if else area that stop 1 motor when the switch is pressed so the other motor turns the robot

Try to imagine that we don’t know anything about your robot.

Because we don’t

do u want me to show u a picture of the robot? it is fully assembled

Will that help us decide how to make it turn to the right?

maybe, it will show u the wires and what the switch looks like, also it will show u the circut board. (nvm the image wont load it keeps saying the attachment wont well attach)

do you guys know any way i can stop a motor from moving but only when the sensor is pressed?

Cut the current to the motor (s)

brotato: i have 2 motors on an mold. the mold has a breadboard and an arduino uno on it. i also have a sensor that i put on the front to tell if the robot has something blocking its path. When this is pressed i want my robot to turn 90 degress. i tried running the code turn but it says turn was not defined and dosent work. want to add a line of code in the if else area that stop 1 motor when the switch is pressed so the other motor turns the robot

The code you posted in your Original Post does not have "turn" in it so I have not idea what you tried or why it did not compile. In your next Reply post the code that does not compile and post the error message that you get.

Note also that you can't say a program "does not work" if you cannot upload it to the Arduino and try it. If it can't be uploaded the correct phrase is "does not compile".

...R

Hi! so i go my robot to turn but apon testing it turns out that i need it to back up first, got any tips on how to do that? again im using an arduino uno starter kit, with to big wheels an all the wires i have in the picture plus a few more

it turns out that i need it to back up first

Reverse the motors.

brotato: Hi! so i go my robot to turn but apon testing it turns out that i need it to back up first, got any tips on how to do that?

How are we supposed to help when you refuse to post the latest version of your program ?

...R

int Motor1Speed = 10;
int Motor1Control11 = 9;
int Motor1Control12 = 8;
int Motor2Speed = 11;
int Motor2Control11 = 13;
int Motor2Control12 = 12;
int speed= 255;
int sensorPin = 7;
int pinState;

void setup () {
 
  // setup code that only runs once
  // setup motar control pins as outlets
  pinMode(Motor1Speed, OUTPUT);
  pinMode(Motor1Control11, OUTPUT);
  pinMode(Motor1Control12, OUTPUT);
  pinMode(Motor2Speed, OUTPUT);
  pinMode(Motor2Control11, OUTPUT);
  pinMode(Motor2Control12, OUTPUT);

  

  analogWrite(Motor2Speed, speed);
  
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
}

void loop(){
  
  digitalWrite(Motor1Control11, HIGH);
  digitalWrite(Motor1Control12, LOW);
  digitalWrite(Motor2Control11, HIGH);
  digitalWrite(Motor2Control12, LOW);
   pinState = digitalRead(sensorPin);
 
  if (pinState == true){

  Serial.println("1");
  digitalWrite(Motor1Control11,LOW);
  digitalWrite(Motor1Control12,LOW);
  delay(2500);
  }else{
    Serial.println("2");

}
}

here ya go. i dont know how to reverse the motors

i dont know how to reverse the motors

Reverse the current through them

so instead of

pinMode(Motor1Speed, OUTPUT);
  pinMode(Motor1Control11, OUTPUT);
  pinMode(Motor1Control12, OUTPUT);
  pinMode(Motor2Speed, OUTPUT);
  pinMode(Motor2Control11, OUTPUT);
  pinMode(Motor2Control12, OUTPUT);

its

pinMode(Motor1Speed, INPUT);
  pinMode(Motor1Control11, INPUT);
  pinMode(Motor1Control12, INPUT);
  pinMode(Motor2Speed, INPUT);
  pinMode(Motor2Control11, INPUT);
  pinMode(Motor2Control12, INPUT);

How did that go for you?

I cant test because a jumper cable broke and the pin is stuck in the circut. ill let you know when i fix it or get a new 1.

Hint: reverse current does not mean “make an output into an input”

ok it didnt work, instead of backing up it stopped. what would reverse the current, here ill post the code right before i did output/input thing

int Motor1Speed = 10;
int Motor1Control11 = 9;
int Motor1Control12 = 8;
int Motor2Speed = 11;
int Motor2Control11 = 13;
int Motor2Control12 = 12;
int speed= 255;
int sensorPin = 7;
int pinState;

void setup () {

  // setup code that only runs once
  // setup motar control pins as outlets
  
  analogWrite(Motor2Speed, speed);
   pinMode(Motor1Speed, OUTPUT);
  pinMode(Motor1Control11, OUTPUT);
  pinMode(Motor1Control12, OUTPUT);
  pinMode(Motor2Speed, OUTPUT);
  pinMode(Motor2Control11, OUTPUT);
  pinMode(Motor2Control12, OUTPUT);
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
}

void loop(){
  
  digitalWrite(Motor1Control11, HIGH);
  digitalWrite(Motor1Control12, LOW);
  digitalWrite(Motor2Control11, HIGH);
  digitalWrite(Motor2Control12, LOW);
   pinState = digitalRead(sensorPin);

  if (pinState == true){

  Serial.println("1");

  digitalWrite(Motor1Control11,LOW);
  digitalWrite(Motor1Control12,LOW);
  delay(2500);
  }else{
    Serial.println("2");

}
}

so yea what can i add to the if else statement to reverse the current