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.
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,
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...
/*
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);
}
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.
This code doesn't do what it says -
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 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
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
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.
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 -
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 -
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;
}
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
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.
im going to go with the potentiometer idea idea and i bought this one
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.
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 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?
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