Skip to content

Instantly share code, notes, and snippets.

@todocono
Created April 16, 2020 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todocono/6a71e27c214d19e046e75f7436cc7ba2 to your computer and use it in GitHub Desktop.
Save todocono/6a71e27c214d19e046e75f7436cc7ba2 to your computer and use it in GitHub Desktop.
/*
* NYU Shanghai - Interactive Media Arts
* 2020 Spring
* This code was prepared for Interaction Lab
* A simple drawing machine
*
* It is based on "Controlling a servo" by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
* the modifications by Scott Fitzgerald http://www.arduino.cc/en/Tutorial/Knob
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int anglePot = A0; // analog pin used to connect the potentiometer 1
int motorPot = A1; // analog pin used to connect the potentiometer 2
const int pinS = 9; //servo pin
const int pinM1 = 5; // motor pin1
const int pinM2 = 6; // motor pin2
void setup() {
myservo.attach(pinS); // attaches the servo on pin 9 to the servo object
pinMode(pinM1, OUTPUT);
pinMode(pinM2, OUTPUT);
}
void loop() {
int valAngle = analogRead(anglePot); // reads the desired servo position value of the potentiometer (value between 0 and 1023)
valAngle = map(valAngle, 0, 1023, 30, 150); // scale it to use it with the servo (value between 0 and 180)
myservo.write(valAngle); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
int valMotor = analogRead(motorPot); // reads the desired motor speed value of the potentiometer (value between 0 and 1023)
valMotor = map(valMotor, 0, 1023, -200, 200);
if (valMotor > 0) {
analogWrite(pinM1, valMotor);
digitalWrite(pinM2, LOW);
} else {
digitalWrite(pinM1, LOW);
analogWrite(pinM2, abs(valMotor)); //we use abs() to eliminate the negative sign of the value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment