Sunday, April 15, 2012

Week 14 - Arduino Potentiometer

This week I connected a Potentiometer to the Arduino. After following the kit and controlling an LED to fade on and off, I decided to connect it to a Servo. I had some basic info to hook it up sine I already connected a Servo in a previous project. Once I had it working, I then used the button project from last week to activate and deactivate the potentiometer and servo from working.

This worked as I hoped it would. I already had the buttons still connected on the breadboard from last week so all I had to do was add its code to this weeks code and add an on/off state again to handle whether the servo and knob should respond or not.

It was also cool to see many elements working together as well.



Also this week I've been really wanting to make it to the Albright Knox Gallery to see the works of Joan Miro, which was ending this Sunday. I was able to make it the last day with friends and really enjoyed it. there wasn't as much as I had hoped, but was happy to fit this in with my busy schedule.

Doh! sorry here's the code for the Arduino if anyone is interested...


// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott
// modfied by naiJenn
/* servo vars*/

#include

Servo myservo; // create servo object to control a servo

int potpin = A0; // analog pin used to connect the potentiometer
int knobval; // variable to read the value from the analog pin

/*set button info*/
const int buttonPin1 = 2; // the number of the ON pushbutton pin
const int buttonPin2 = 3; // the number of the OFF pushbutton pin
const int ledPin = 9; // the number of the LED pin
boolean onOff = false; // button state

void setup()
{
myservo.attach(11); // attaches the servo on pin 9 to the servo object
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}

void loop()
{
knobval = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
knobval = map(knobval, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)

if (digitalRead(buttonPin1) == LOW) { // button presed state = LOW
digitalWrite(ledPin, HIGH);
onOff = true;
} else if (digitalRead(buttonPin2) == LOW) { // button presed state = LOW
digitalWrite(ledPin, LOW);
onOff = false;
}
if (onOff) {
myservo.write(knobval); // sets the servo position according to the scaled value
delay(15);
}
}

No comments:

Post a Comment