Arduino to Motors

by golan @ 2:53 pm October 17, 2011

Arduino code to control an analog DC motor, regulated by a potentiometer

// Control an analog DC motor, regulated by a potentiometer
int analogInput = 0;
int motorSpeed = 0;
 
void setup(){
  pinMode (3, OUTPUT);
}
 
void loop(){
  analogInput = analogRead(A0);
  motorSpeed =  analogInput/4;
  analogWrite (3, motorSpeed);
}
// Control a servo motor
#include 
 
Servo myServo;
 
int analogInput = 0;
int motorPosition = 0;
 
void setup(){
  myServo.attach(9);
}
 
void loop(){
  analogInput = analogRead(A0);
  motorPosition =  map (analogInput, 0,1023,  0,180);
  analogWrite (3, motorSpeed);
}

Breadboard diagram:

Make a servo motor turn back and forth:

#include 
 
Servo myServo;
 
int analogInput = 0;
int motorPosition = 0;
 
void setup(){
 myServo.attach(9);
}
 
void loop(){
 //analogInput = analogRead(A0);
 //motorPosition = map(analogInput, 0, 1023, 0, 180);
 //myServo.write(motorPosition);
 
 for(motorPosition = 0; motorPosition =1; motorPosition--){
  myServo.write(motorPosition);
  delay(10);
 }
}

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2017 CMU Electronic Media Studio II, Fall 2011, Section A | powered by WordPress with Barecity