Go Down

Topic: Servo language question (Read 6786 times) previous topic - next topic

bren21m

Jul 03, 2012, 06:57 am Last Edit: Jul 03, 2012, 08:40 am by Nick Gammon Reason: 1
hey, im new to the arduino world, i have a question on the arduino uno i bought at radio shack for 30 dollars a few weeks ago. i have it set to use the servo sweep as  a wiper, this is the code i have now.

// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.


#include <Servo.h>

Servo myservo;  // create servo object to control a servo
               // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

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


void loop()
{
 for(pos = 0; pos < 1; pos += 1)  // goes from 0 degrees to 180 degrees
 {                                  // in steps of 1 degree
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(8000);                       // waits 15ms for the servo to reach the position
 }
 for(pos = 180; pos>=179; pos-=1)     // goes from 180 degrees to 0 degrees
 {                                
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(8000);                       // waits 15ms for the servo to reach the position
 }
}




i would like to know if there would be any way to put a switch with 3 positions that i could put into the system that could change the delay time,
like in position one it would be 8000ms and in position 2 it would do a 15000ms delay and positon 3  a 20000ms delay, i there is a way or if anyone already asked the question i was wondering if anyone could point me in the right direction or help me write a code,

thank you,
brendan :smiley-mr-green:

JimboZA

#1
Jul 03, 2012, 07:29 am Last Edit: Jul 03, 2012, 07:36 am by JimboZA Reason: 1
The code below will help... it reads a few switches on one analog pin and determines which switch is pressed based on the resistance. So that would be your 3-way switch.

Then, where I have return 0, 1, 2 etc which are my switch numbers, you could return the delay you want instead.

Lower down when you call the routine that reads the buttons, instead of "thebutton" have "thedelay" and use that in your delay later on as delay(thedelay) instead of delay(8000).

Something like that anyway... thats' off the top of my head but I'm kinda busy at work this morning...

Code: [Select]
/*
 AnalogReadSerial
 Rig a divider with a switch on the side to be measured, take the
 junction between the two sides to A0
 Rig more such switches, each with a different resistor,
 all thru the common pull down
 Then depending which switch is pressed,
 a different reading will appear on the pin since a
 unique resistor is in play upstream

This example code is in the public domain.
*/
int theButton;  //buttons are 1, 2 and 3, or 0 for no button
// This routine reads the analog pin and returns a button number
// Thresholds were from calulations according to resistors
// in the divider, adjusted for a bit of leeway
int sensorValue;
int ReadTheButton()
{
  sensorValue = analogRead(A0);
 if (sensorValue < 100) return 0;  // no button
 if (sensorValue < 550) return 1;
 if (sensorValue < 750) return 2;
 if (sensorValue < 900) return 3;
 return 0;  // might be over 900
}


void setup() {
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
 // force pin13 low so led is off
 pinMode(13, OUTPUT);
 digitalWrite(13, LOW);
}


void loop() {
 delay(1000);
 theButton = ReadTheButton();
 Serial.print("Button number = " );
 Serial.println(theButton);
 
}


Have a look at post 6 and 7 here: http://forums.trossenrobotics.com/showthread.php?1284-Turning-analog-inputs-into-multi-switch-digital-inputs

Beware of making it go BOOM so look at what's wrong in post 6 as pointed out in post 7  8)
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

bren21m

do you know where i could get a switch for this application? or what kind i would need from radio shack

DuaneB

Hi,

Two suggestions

1) rather than multiple switches or a multi position switch, why not just use a potentiometer or if you want to get really fancy two pots, 1 for position and the other to control the speed at which the servo will seek the new positions - probably cheaper than one multi position switch.

2) This code doesn't do what it says -

Code: [Select]

  for(pos = 0; pos < 1; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(8000);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=179; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(8000);                       // waits 15ms for the servo to reach the position
  }


Easy to fix.

Duane B

rcarduino.blogspot.com
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

JimboZA

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

bren21m

i dont think i can use a potentiometer because the servo is being a wiper for a camera lense i want the servo to go as fast as possible, or if you mean using the potentiometer instead of a switch like from degree 1-60 (8000ms) and degree 61-120 (15000ms) and degree 121-180  (20000Ms)

i know a potentiometer dosent have 180 degreees i just dont know what exactly it has

PeterH


i dont think i can use a potentiometer because the servo is being a wiper for a camera lense i want the servo to go as fast as possible, or if you mean using the potentiometer instead of a switch like from degree 1-60 (8000ms) and degree 61-120 (15000ms) and degree 121-180  (20000Ms)

i know a potentiometer dosent have 180 degreees i just dont know what exactly it has


You need to be clearer about what you're trying to achieve. Are you trying to control how slowly the wiper scrapes across the lense, or how long it waits between wipes?

If you want the wiper to travel as fast as possible and just control the interval between wipes you can bin most of that sketch since you are not actually trying to control the speed of movement of the servo.

bren21m

i want to be able to change the amout of time inbetweeen wipes with a 3 point switch like i said in my original question

DuaneB

#8
Jul 04, 2012, 07:24 am Last Edit: Jul 04, 2012, 11:25 am by DuaneB Reason: 1
Hi,
   Just use a potentiometer, then you can have 1024 levels of adjustment, if thats still too many, still use a potentiometer but divide its value by 256 to get 4 levels of adjustment or 128 to get 8 different periods.

to get 1024 values -

Code: [Select]

int nPeriod = analogRead(POT_PIN); // nPeriod = 0 to 1024
nPeriod *= 10; // nPeriod is multiplied by 10 so now give a period from 0 to 10240 milliseconds (10.2 seconds)


if you just want to adjust between 4 fixed values -

Code: [Select]

int nSetting = analogRead(POT_PIN); // nSetting = 0 to 1024
nSetting >> 8; // divide by 256 nSetting = 0 to 3;

int nPeriod = 0;

switch(nSetting)
{
case 0:
    nPeriod = 3000; // 3 seconds
    break;
...
...
case 3:
    nPeriod = 25000; // 12 seconds
    break;
}



Duane B
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

PeterH


i want to be able to change the amout of time inbetweeen wipes with a 3 point switch like i said in my original question


In that case both those 'for' loops are a waste of time. You just need to send the servo its new position and then wait for the required interval before moving it back. But instead of a fixed delay (8000) you will delay by an amount held in a variable, which you set based on the value read from your switch. For three distinct positions (or perhaps four, if you want an 'off' position) I'd use a rotary switch with different resistors at each position set up to act as a voltage divider, use an analog input to read the voltage and do a range comparison to decide which position the switch is in, use that as an index into an array of delay values.

bren21m

would one like this be ok?

http://www.radioshack.com/product/index.jsp?productId=2062536#

bren21m

hi everyone,

im going to go with the potentiometer idea idea and i bought this one

http://www.radioshack.com/product/index.jsp?productId=2062536#

i really have no idea where to start with writing the program to use it so you could have told me already and i wouldnt know it.

i wanted to know if somone would be nice enough to write it and then explain how you did it. i would really appreciate it. i would also like to know which pin on the potentiometer goes to which pin on the arduino board and if there has to be another 5 volts going to the potentiometer like the servo.

thankss,

Brendan

bren21m

ok so i now know how the potentiometer works there are 2 pins in the middle on the bottom and 12 around the edge. the one pin controls the 6 on that side and the other pin controls the 6 pins on the other side. when you turn the knob it in snaps into  a position and what ever electricity is comming thruogh the pin goes out that outer pin on that side only and if there is any electricity is in the other pin it goes through to that sides pin. so now i need to know which pin hole on the adruino board i connect to the 6 pins on the one side on the potentiometer.. the other 6 will most likely be LEDs showing which position it is in. and i also need to figure out how to program the arduino (so that when pin #X on the arduino gets electricity then make the delay 5000) etc

im pretty sure thats what i have to do. correct me if im wrong. im only 15

i have the arduino UNO if that helps anything

zoomkat

Quote
im going to go with the potentiometer idea idea and i bought this one


That is not a pot, it is a switch.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

PaulS

Quote
That is not a pot, it is a switch.

After enough pot, it could be... 8)
The art of getting good answers lies in asking good questions.

Go Up