Arduino Forum

Using Arduino => Project Guidance => Topic started by: bren21m on Jul 03, 2012, 06:57 am

Title: Servo language question
Post by: bren21m on Jul 03, 2012, 06:57 am
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:
Title: Re: SERVO LANGUAGE QUESTION
Post by: JimboZA on Jul 03, 2012, 07:29 am
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)
Title: Re: SERVO LANGUAGE QUESTION
Post by: bren21m on Jul 03, 2012, 07:55 am
do you know where i could get a switch for this application? or what kind i would need from radio shack
Title: Re: SERVO LANGUAGE QUESTION
Post by: DuaneB on Jul 03, 2012, 08:32 am
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 (http://rcarduino.blogspot.com)
Title: Re: SERVO LANGUAGE QUESTION
Post by: JimboZA on Jul 03, 2012, 08:32 am
One like  this  (http://en.wikipedia.org/wiki/Rotary_switch) I guess
Title: Re: Servo language question
Post by: bren21m on Jul 03, 2012, 06:49 pm
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
Title: Re: Servo language question
Post by: PeterH on Jul 03, 2012, 09:38 pm

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.
Title: Re: Servo language question
Post by: bren21m on Jul 04, 2012, 07:10 am
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
Title: Re: Servo language question
Post by: DuaneB on Jul 04, 2012, 07:24 am
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
Title: Re: Servo language question
Post by: PeterH on Jul 04, 2012, 11:19 am

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.
Title: Re: Servo language question
Post by: bren21m on Jul 18, 2012, 05:41 pm
would one like this be ok?

http://www.radioshack.com/product/index.jsp?productId=2062536#
Title: Re: Servo language question
Post by: bren21m on Jul 19, 2012, 02:15 am
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
Title: Re: Servo language question
Post by: bren21m on Jul 19, 2012, 07:41 am
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
Title: Re: Servo language question
Post by: zoomkat on Jul 19, 2012, 07:25 pm
Quote
im going to go with the potentiometer idea idea and i bought this one


That is not a pot, it is a switch.
Title: Re: Servo language question
Post by: PaulS on Jul 19, 2012, 07:35 pm
Quote
That is not a pot, it is a switch.

After enough pot, it could be... 8)
Title: Re: Servo language question
Post by: bren21m on Jul 20, 2012, 04:35 am
ohh, well is that how my switch workss?
Title: Re: Servo language question
Post by: bren21m on Jul 20, 2012, 06:04 am
i want the program on the arduino to say, when positiion 1 on the switch gets a signal in whatever pin it goes in on the arduino to make the delay this many milliseconds and so on. does anyone know what pins should be used for the switch i got?
Title: Re: Servo language question
Post by: zoomkat on Jul 20, 2012, 07:14 am
Two servo test code that can be modified to position a single servo in several different positions depending on the switch position.

Code: [Select]

//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }   
 
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
 
  /*else {
    servo1.write(90);
  }*/
}

Title: Re: Servo language question
Post by: bren21m on Jul 20, 2012, 07:29 am
that wouldn't adjust the delay for each switch postion would it? or i would have to chanfe it to do that?
Title: Re: Servo language question
Post by: bren21m on Aug 07, 2012, 03:33 am
would a video explaining help? im a beginner so if you guys already put it in black and white please show me. i have no idea where to start, im not impatient its just people are annoying me to finish it, so id like toget it done soon. thanks
Title: Re: Servo language question
Post by: zoomkat on Aug 07, 2012, 03:48 am
Quote
i have no idea where to start, im not impatient its just people are annoying me to finish it, so id like toget it done soon. thanks


Sounds like a school assignment. So what have you tried on your own since your last post on July 19?
Title: Re: Servo language question
Post by: bren21m on Aug 07, 2012, 07:09 pm
i tried to see if i could find anything like a diagram or somthing on google showing how to connect the switch to the arduino but i couldnt find anything. i also looked to see if anyone did Anything similar to what im doing but i couldnt find anything i also made this video a few day ago scince i dont have my gopro at the moment.

http://www.youtube.com/watch?v=cx04r9GtnbU (http://www.youtube.com/watch?v=cx04r9GtnbU)
i know it can be a whole lot better.  but thats just the way i am with everything. its also ok for my first video like that.
Title: Re: Servo language question
Post by: PeterH on Aug 08, 2012, 01:37 am

i also made this video a few day ago scince i dont have my gopro at the moment.


What the bleep has that video got to do with your question?
Title: Re: Servo language question
Post by: zoomkat on Aug 08, 2012, 02:57 am


i also made this video a few day ago scince i dont have my gopro at the moment.


What the bleep has that video got to do with your question?


It is what you do while waiting for somebody to do your homework for you.  :)
Title: Re: Servo language question
Post by: bren21m on Aug 08, 2012, 04:00 am
lol, no i made that video the other day because the camera im making this wiper for is getting fixed. and it not my home work, my dad is nagging my to make it and all these people want me to make them it. they think i can just turn it out. like no i have no idea where to start i used a code that came with the program. this is the camera i use the wiper on. its a gopro

http://www.youtube.com/watch?v=ChzVGV9FtX0&feature=relmfu (http://www.youtube.com/watch?v=ChzVGV9FtX0&feature=relmfu)

you can see the wiper go like every 8 or 10 seconds i forget what i set it too

if you think a video would help ill make one.
and im only 16 so i wouldnt have a class that would expect me to build somthing like this and it is summer.
Title: Re: Servo language question
Post by: bren21m on Aug 09, 2012, 03:26 am

Two servo test code that can be modified to position a single servo in several different positions depending on the switch position.

Code: [Select]

//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }   
 
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
 
  /*else {
    servo1.write(90);
  }*/
}






im pretty sure this would work its just i dont know how and where to insert it into my code to make it not change the position of the servo but change the delays and what pins on the arduino to connect the the pins on the switch
also can i just solder the servo positive to the positive on the switch to one pin and put that pin in the 5v on the arduino or would i need to get one of those breadboards that are like white plastic
Title: Re: Servo language question
Post by: bren21m on Jan 12, 2013, 12:40 am
i made this video a while back then just stopped caring about gettingthe wiper done because race season was over, but now its possible we are going to have a race in mid febuary so i figured i would get it done now, i hope this clearly explains my question




http://www.youtube.com/watch?v=2IrCplYeUZw&feature=youtu.be (http://www.youtube.com/watch?v=2IrCplYeUZw&feature=youtu.be)


i know i sound like a dweeb, i was sick.