Newbie Help - Servo & LED

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Newbie Help - Servo & LED

Postby cwackduck » Thu Aug 09, 2012 11:58 pm

Hello folks! I've tried to avoid annoying others with my tinkering problems but I've come up against something I can't understand at all.
Before I begin, my skill level is pretty novice right now. I've gotten the Experimentation Kit and I'm starting to attempt to adjust code to make new things work, not just follow tutorials.
That said, I came up with a simple goal tonight: Use a pot to control both a servo and an LED at the same time. Pot = 0, the Servo and LED = 0. Pot = 1023, Servo = 179 and LED = 254.
This seemed like an easy enough goal and I felt like I could figure it out on my own by editing the "knob" example code.

Here's the code I'm presently working with:
Code: Select all
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

#include <Servo.h>

int ledPin = 10;    // Setting my LED to pin 10

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

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
int LEDval;    // Empty value to control LED brightness
int brightness = 0;  //LED brightness value 0 - 254


void setup()
{
  pinMode(ledPin, OUTPUT);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
  analogWrite(ledPin, brightness);      // lights up the LED based on brightness value
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  LEDval = map(val, 0, 179, 0, 254);    // rescale the input over to LED brightness values
  brightness = LEDval;                  // Set the brightness value based on the Pot input
  myservo.write(val);  // sets the servo position according to the scaled value
  delay(30);                           // waits for the servo to get there
}


The pot is correctly controlling the servo, but there are no signs of life to the LED.
I set up a "println" command in one variation to make sure the LEDval was getting written correctly and it was.
So I started tearing the code down bit by bit and found that if I remove the "myservo.attach" string from setup, the LED works as expected. But if I put the "myservo.attach" string back in, the servo works but the LED won't anymore.

So my question, after all this rambling: Why won't the LED and the Servo work at the same time? If someone could break it down a little, or lead me down the correct path to figuring it out myself I'd greatly appreciate it! I'm really trying my best to start understanding how to code on my own but, since this involves invoking a servo library and things stop working, I feel like it's already over my head.

Thanks!
cwackduck
 
Posts: 2
Joined: Tue Jun 08, 2010 10:10 pm
Location: Springfield, MO

Re: Newbie Help - Servo & LED

Postby adafruit_support_bill » Fri Aug 10, 2012 5:13 am

The servo library is taking over Timer-1 that controls PWM on pinns 9-10. Try connecting your led to one of the other PWM pins: http://arduino.cc/playground/Main/TimerPWMCheatsheet
User avatar
adafruit_support_bill
 
Posts: 16022
Joined: Sat Feb 07, 2009 9:11 am

Re: Newbie Help - Servo & LED

Postby cwackduck » Fri Aug 10, 2012 10:31 am

That makes complete sense and I kind of suspected something like that was going on. I just didn't know enough to figure out which pins were out of bounds for that particular module. Thank you so much! Now I can start working on getting frustrated at something else! :D
cwackduck
 
Posts: 2
Joined: Tue Jun 08, 2010 10:10 pm
Location: Springfield, MO


Return to Arduino

Who is online

Users browsing this forum: No registered users and 1 guest

Stuff to buy from the Adafruit store and links to product documentation!


New Products [103]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[61]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]