For our second assignment we were to create a simple application of digital or analog input and digital output. I choose to work with analog input and digital output because I was interested in using a potentiometer.
Motorized Disc
The potentiometer supplied the PWM (pulse width modulation) analog input and the DC motor received the digital output. The potentiometer was the component that determined the varying speed of the rotating disc as seen in the demo below:
I used the resources and kit supplied in the Arduino Starter Kit and amped the Motorized Pinwheel (pg 95 in the Arduino Projects Book) that was originally controlled via a digital input (button) to a circuit that was controlled from an analog input (potentiometer).
The difficultly that I had in this project was how to slow the speed of the motor. I observed the
Serial.println(motorSpeed)
and tried to use the
map()
function to map the min and max numbers I observed in the Serial Monitor to reduce the speed, but I had no luck with that. I will ask the professor and report back once I have a better idea of how to tackle this problem.
Below is the code that I used to program this circuit:
const int POT_PIN = A0;
const int MOTOR_PIN = 2;
int motorSpeed = 0;
int potVal = 0;
void setup() {
pinMode(MOTOR_PIN, OUTPUT);
//Serial.begin(9600);
}
void loop()
{
potVal = analogRead(POT_PIN);
motorSpeed = map(potVal, 0, 1023, 127, 255);
analogWrite(MOTOR_PIN, motorSpeed);