digitalwrite(DCmotorPin, LOW); does not work

Hi, first of all I want to mention that I'm a newbie. Started with Arduino a month or 2 ago. I'm reading the starters kit, but I'm also building a project.

In this project I have a DC motor connected (with mosfet, diode, external supply) to pin 10. After a button press, I want the motor to start and the motor needs to stop again after some ms.

I have added the serial.prints for testing purposes, but even after the digitalWrite(dcPin, LOW); the serial monitor indicates that dcPin is 1, so HIGH.

Am I missing something?

Thx

[u]Code-extract:[/u] digitalWrite(dcPin, HIGH); //Start DC motor 1 delay(500); Serial.println(digitalRead(dcPin));

digitalWrite(dcPin, LOW); //Stop DC motor 1

Serial.println(digitalRead(dcPin)); delay(500);

Code-extract

Answer extract:

I can't be bothered to guess.

Post your code. Use code tags.

Hi,

Here is the full code. Some part are in dutch (just copy/paste)
What do you mean by code tags? (My apologies, I’m a newbie, and this is only my second post…)

#include <Servo.h>

Servo myServo;

int phase = 1;
//phase 1 = rotate ball - ServoMotor
//phase 2 = release ball to motors - DC motor 1
//phase 3 = shoot ball - DC motors 2 and 3

//init servomotor
const int servoPin = 9; //The servo is connected to pin 9
const int servoAngle0 = 135; //Servo Angle initial
const int servoAngle1 = 0; //Servo Angle after rotation
const int switchButton = 2; //The button is connected to pin 2

//init DC motor
const int dcPin = 10;

boolean lastButton = LOW; //Variable containing the previous button state
boolean currentButton = LOW; //Variable containing the current button state

void setup()
{
//Define switchButton (=pin 2, connected to drukknop voor servo) as inputpin
pinMode(switchButton,INPUT);

//Servo is connected to Arduino using pin 9
myServo.attach(servoPin);

//Define dcPin (=pin 10, connected to gate of mosfet)
pinMode(dcPin, INPUT);
digitalWrite(dcPin, LOW);

Serial.begin(9600);
}

/*
Debouncing function to get accuracy of the switch.

*/
boolean debounce(boolean last)
{
boolean current = digitalRead(switchButton);
if (last != current)
{
delay(5);
current = digitalRead(switchButton);

return current;
}
}
/================================/

/*
Servo motor

*/
void servoMove(int servoAngle)
{
Serial.print(“Angle Servo Init:\t”);
Serial.print(servoAngle);
Serial.print("\t\t");

myServo.write(servoAngle1);
delay(1000);

Serial.print(“Angle Servo Rotate TUWZ:\t”);
Serial.print(servoAngle1);
Serial.print("\t\t");

myServo.write(servoAngle);

Serial.print(“Angle Servo Rotate UWZ:\t”);
Serial.print(servoAngle);
Serial.print("\t\t");
Serial.println();
}
/================================/

void loop()
{
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH) //button is pressed
{
servoMove(servoAngle0);

digitalWrite(dcPin, HIGH); //Start DC motor 1
delay(500);
Serial.println(digitalRead(dcPin));

digitalWrite(dcPin, LOW); //Stop DC motor 1

Serial.println(digitalRead(dcPin));
delay(500);

}
lastButton = currentButton;
}

He means that when posting code, to put it in between [ code] [/code ] Take out the space after the left bracket [ and take out the space before the right bracket ]

Do you also have the circuit diagram? Do you have a link to the original article? Are you using the same parts as the author?

You have dcPin as an INPUT, make it OUTPUT

Could I also add that your debounce function is pointless. If a user presses the button there are then two half second delays to endure before the button will be looked at again. Therefore the extra delay of 5 microseconds inserted by your debounce function is redundant.

Thx,

changing dcPin to Output works!!

Only the Debounce function is from Jeremy Blum, the rest I wrote myself. I was planning to add him in my code as author. This code is still just a draft since I still need to add 2 extra motors and some other input sensors. I'm working on it functionality by functionality.

I have no circuit diagram, I wrote it down as I would connect it on my breadboard.

Kind regards. Feedback for posting here is also welcome. Willing to learn and (in future when more experience) help others. (Everybody was a beginner once.)

currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)    //button is pressed
  {
     servoMove(servoAngle0);   
     
     digitalWrite(dcPin, HIGH);  //Start DC motor 1
     delay(500);

If the switch has changed state, fart around for more than a second.

Unless you've got the crappiest switch in the known universe, it is not necessary to debounce the thing.

Unless you've got the crappiest switch in the known universe, it is not necessary to debounce the thing

This code is still just a draft

So once op has updated their loop function, that debounce could be useful. No point throwing it out now to just have to re-invent it later. It's not doing any harm where it is. I even feel a bit guilty for mentioning it in the first place.