Hi, I am new to using arduino, so I am unsure how to program them. I have played about with some programs to get partly what I want but have it a brick wall. Code below
}
void loop()
{
if (digitalRead(2) == HIGH && pos < 85) {
pos++;
myservo1.write(pos);
delay(4);
}
if (digitalRead(3) == HIGH && pos > 45) {
pos--;
myservo1.write(pos);
delay(4);
}
}
What I am doing is using a On-off-On switch to turn the servo left to right, but i would like to do this with mutli switches and servo's but can't get the code to work. Also I would like to have and LED light up when I switch left (possible red) and right (possible green) but can't work out how to code this.
I know that there is a lot of people that have a better brain than I have so I am asking for some help please.
Mistdragon:
At the moment it not attached to anything, but I will be attaching them ro a model railway layout.
Guessing for points/turnouts?
If so, then I wonder if you really need the pos++ and pos-- to gradually move from open to closed (or whatever the terminology is), but can't you just do a servo.write(22) and servo.write(64) or whatever numbers work?
You might also find it useful to look here to see about using the internal pullup resistors. It changes your logic, test for ==LOW not ==HIGH but is a pretty standard way of doing things. (You do need a pulldown or pullup (on or the other, pullup's more common, and builtin) to make sure your pin is definitely at one value or other and not floating in aimlessly.
The one thing that I am still wondering is how to get LEDs to work with the switch from left to right, can you think of a way to add LEDs to the system. I have the two servos working so it just the light now.
You want to make a RR crossing signal? I think that would be a perfect application of blink without delay.
Go google "blink without delay" and try it out.
The try adding a second LED attached to another pin.
Turn one on when you turn the other off and vice versa.
Then add it to this program.
Ask questions if you run into any problems.
Oh, and post a video once you get it all together.
Thanks for the answers but the thing is after reading them I tried to get the three servos working off different switches, by copying the code changing the number but this does not seem to work as it should. Here is the code, can someone look over and tell me where I have gone wrong.
#include<Servo.h>
Servo Point01;
Servo Point02;
Servo Point03;
int pos = 30;
void setup()
{
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
Point01.attach(9);
Point01.attach(10);
Point01.attach(11);
}
void loop()
{
if (digitalRead(2) == HIGH && pos <95) {
pos++;
Point01.write(pos);
delay(4);
}
if (digitalRead(3) == HIGH && pos >55) {
pos--;
Point01.write(pos);
delay(4);
}
if (digitalRead(4) == HIGH && pos <95) {
pos++;
Point02.write(pos);
delay(4);
}
if (digitalRead(5) == HIGH && pos >55) {
pos--;
Point02.write(pos);
delay(4);
}
if (digitalRead(6) == HIGH && pos <95) {
pos++;
Point03.write(pos);
delay(4);
}
if (digitalRead(7) == HIGH && pos >55) {
pos--;
Point03.write(pos);
delay(4);
}
}
If you see an error such as 'Point' was not declared in this scopeyou should immediately check :
1 - is Point declared anywhere in the program ? Maybe it is spelt wrongly/differently somewhere
2 - if it is declared then where ? What scope does the variable have