Trouble in Arduino when trying to Code for Motor to Move 'Until'

Hi, I understand the following is not how one is supposed to connect a motor to an Arduino, but I needed the Arduino to be able to move the DC motor left and right, by putting both DC motor pins into Digital Outputs and in the code assigning HIGHs and LOWs for the pins they were connected to. It seemed to work at first, but I understand that the Arduino is doing so by sending power to each pin for a duration. Is it possible to say, rotateRightFull continuously until the potpin value changes in my IF statement?

int motorPin1 = 5;
int motorPin2 = 6;

int potpin = 0;
int val;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  
}

void loop() {
  int potReading = analogRead(potpin);
  val = map(potReading, 0, 1023, 0, 3960);
  
  if (potReading < 511) {
    rotateLeftFull(1000000000000000000000000);
  } else if (potReading > 511) {
      rotateRightFull(50);
  }

}


void rotateLeftFull(int length){
  digitalWrite(motorPin1, HIGH); //rotates motor
  digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW
  delay(length); //waits
  digitalWrite(motorPin1, LOW);    // set the Pin motorPin1 LOW
}

void rotateRightFull(int length){
  digitalWrite(motorPin2, HIGH); //rotates motor
  digitalWrite(motorPin1, LOW);    // set the Pin motorPin1 LOW
  delay(length); //waits
  digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW
}

Thank you

Before tackling your question here is one for you

rotateLeftFull(1000000000000000000000000);

Is this a joke ?

val = map(potReading, 0, 1023, 0, 3960);
  
  if (potReading < 511) {

Why bother with the map() function when you don’t use its output.

…R

Ramzeez100:
Hi, I understand the following is not how one is supposed to connect a motor to an Arduino, but I needed the Arduino to be able to move the DC motor left and right, by putting both DC motor pins into Digital Outputs and in the code assigning HIGHs and LOWs for the pins they were connected to. It seemed to work at first, but I understand that the Arduino is doing so by sending power to each pin for a duration. Is it possible to say, rotateRightFull continuously until the potpin value changes in my IF statement?

int motorPin1 = 5;

int motorPin2 = 6;

int potpin = 0;
int val;

void setup() {
 pinMode(motorPin1, OUTPUT);
 pinMode(motorPin2, OUTPUT);
 
}

void loop() {
 int potReading = analogRead(potpin);
 val = map(potReading, 0, 1023, 0, 3960);
 
 if (potReading < 511) {
   rotateLeftFull(1000000000000000000000000);
 } else if (potReading > 511) {
     rotateRightFull(50);
 }

}

void rotateLeftFull(int length){
 digitalWrite(motorPin1, HIGH); //rotates motor
 digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW
 delay(length); //waits
 digitalWrite(motorPin1, LOW);    // set the Pin motorPin1 LOW
}

void rotateRightFull(int length){
 digitalWrite(motorPin2, HIGH); //rotates motor
 digitalWrite(motorPin1, LOW);    // set the Pin motorPin1 LOW
 delay(length); //waits
 digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW
}




Thank you

Are your motors connected DIRECTLY to the processor?

UKHeliBob, Oh god I'm sorry. I forgot this was the version of the code I copied onto my clipboard, the multiple 0s was just a random experiment to see what would happen.

Robin2, what would be a more effective way then?

Vaclav, the motor is connected to the digital IO pins if that is what you're asking.

Ramzeez100: Robin2, what would be a more effective way then?

At the moment I have no better advice than to leave out the map() altogether. But that's because I don't know what you were thinking of using it for.

the motor is connected to the digital IO pins if that is what you're asking.

That will quickly destroy your Arduino. The I/O pins cannot provide enough power for a motor. Their max is 40mA and 20mA would be much safer. You need a motor driver board between the Arduino and a motor. And the motor should NOT draw its power from the Arduino 5v pin or from the Vin pin.

...R

Ramzeez100: UKHeliBob, Oh god I'm sorry. I forgot this was the version of the code I copied onto my clipboard, the multiple 0s was just a random experiment to see what would happen.

Robin2, what would be a more effective way then?

Vaclav, the motor is connected to the digital IO pins if that is what you're asking.

was just a random experiment to see what would happen

Doing random experiments is fine if you are willing to suffer the consequences. Connecting motor directly to I/O does not quality as "random experiment ". It is pretty much same as checking your car gas tank level with lighted match. Results are predictable.

( To MAKE SURE YOU UNDERSTAND MY ANALOGY DON'T DO THAT!) .

Without looking at the output circuit of the I/O I cannot tell if you destroyed it ( probably not) but it will not turn the motor DIRECTLY.

There are numerous ways to control motor indirectly, but at this point it looks as you are over your head and you should seek some hands on help. ( And I don't mean UTube videos) This group can help you with software , but you need to tackle the hardware first.