Hi!
I need help programming my dc motor.
I need it to start when I press the pushbutton and to accelerate it with potentiometer.
And lastly I want it to shut down when I press the pushbutton again.
Here's my code
void button();
void potentiometer();
int buttonPin = 2;
int motorPin = 3;
int buttonState = 0;
int ledState = HIGH;
int lastButtonState = LOW;
int sensorValue = 0;
int analogInPin = A0;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup() {
pinMode(motorPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop()
{
button();
}
//******************BUTTON*******************************
void button()
{
int reading = digitalRead(buttonPin);
if (reading != lastButtonState)
{
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
lastButtonState = reading;
digitalWrite (motorPin, ledState);
Serial.println(buttonState);
}
//*********************POTENTIOMETER*********************
void potentiometer()
{
sensorValue = analogRead(analogInPin);
sensorValue=sensorValue/4;
analogWrite(motorPin, sensorValue);
Serial.println(sensorValue);
button();
}
What sort of motor is it and how is it connected to the Arduino ?
The Arduino pins have very limited ability to supply current without the chip being damaged, so it is normal to use an external power supply for motors and a means of controlling them with a small current input such as an FET of H Bridge circuit.
The only place that you read the value of the analogInPin in your code is in the potentiometer() function but that function is not called in your program so I don't understand how the potentiometer controls the speed of the motor.
Oh my bad. I had wrong program uploaded to my Arduino.
So, I can start and stop my motor from the button, but I can't control motor's speed with my potentiometer.
I'd remove the reference to "button" from "potentiometer", and the reference to "potentiometer" from "button", then call "button" and "potentiometer" from "loop".
I'm new at Arduino world.
That's no excuse for your earlier comments.
How are we supposed to know that?
When I upload the code to my Arduino, the motor starts running and I can adjust it's speed with my potentiometer, but the button does nothing when I press it.
I already said in the beginning of this topic that what I want the button to do.
The button does absolutely nothing.
Serial monitor says that the button is changing only "reading" value from "0" to "1".