Following the arduino starter kit, one of the projects uses a push button to on/off a motor.
In the end they suggest to try to use a potentiometer to control the speed of the motor. I already did it, but I am not sure how I should use the capacitors(I placed one with the pot).
In one of the previous examples they used 2 100uF capacitors(one for the pot. and another for a servo motor) and I was wondering if this would be the same case.
This is my circuit:
The code:
const int POT_PIN = A0;
const int MOTOR_PIN = 9;
int motorSpeed = 0;
int potVal = 0;
void setup()
{
pinMode(MOTOR_PIN, OUTPUT);
}
void loop()
{
potVal = analogRead(POT_PIN);
motorSpeed = map(potVal, 0, 1023, 0, 255);
analogWrite(MOTOR_PIN, motorSpeed);
}
Previous example with capacitors:
Code:
#include <Servo.h>
Servo myServo;
int const POT_PIN = A0;
int potVal;
int angle;
int alpha = 0.8;
int rawReading;
void setup()
{
myServo.attach(9);
Serial.begin(9600);
}
void loop()
{
rawReading = analogRead(POT_PIN);
potVal = alpha*potVal + (1-alpha)*rawReading;
Serial.print("potVal: ");
Serial.print(potVal);
angle = map(potVal, 0, 1023, 0, 179);
Serial.print(", angle: ");
Serial.println(angle);
myServo.write(angle);
delay(15);
}