I can't seem to get this Servo to rotate, can anyone just have a look at that and tell me how terrible I am for not understanding something? The code is basic, but it doesn't work.
Much appreciated,
Ballamone
_2LDR1Ser.ino (603 Bytes)
I can't seem to get this Servo to rotate, can anyone just have a look at that and tell me how terrible I am for not understanding something? The code is basic, but it doesn't work.
Much appreciated,
Ballamone
_2LDR1Ser.ino (603 Bytes)
Why not just post the code, instead of forcing everyone to download it?
Attach your code using the </> icon on the left side of the posting menu.
Put your sketch between the code tags [code][/code]
First off, don't hang the servo on pin 0 which is used, along with pin 1, for Serial.
#include <Servo.h>
Servo myservo;
int val;
int val1;
void setup()
{
Serial.begin(9600);
myservo.attach(0);
myservo.write(90); // set servo to mid-point
}
void loop() {
{
val = analogRead(0);
val1 = analogRead(1);
if (val >> val1) {myservo.write(180);}
if (val << val1) {myservo.write(0);}
if (val == val1) {myservo.write(90);}
}
delay(150);
Serial.print("Current through port A1 = ");
Serial.println(val);
Serial.print(" ");
delay(150);
Serial.print("Current through port A2 = ");
Serial.println(val1);
Serial.print(" ");
delay(150);
}
Never knew how to do that - sorry haha
Get into the habit of using:
val = analogRead(0);
val1 = analogRead(1);
Use
val = analogRead(A0);
val1 = analogRead(A1);
Circuit schematic and image?
I don't know if there is an easier way of link photos but there ya go. Sorry about the messy wires. I realise there are two servos - i only coded from one to work my way up. The inputs are probably in the wrong number, but IRL i have them in the right ones.
Nice one,
Ballamone
I only just took it back off there, surely that couldn't be the problem though?
I don't see any power supply in that picture.
...R
Yeah it is connected to my pc, had to take it away from the computer though (too much clutter :P), didn't think any wires would be distinguishable then.
As the code stands it should turn the servo one way if one LDR is reading higher than the other, right?
Do not run your servo powered from the Arduino.
FYI
https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard
.
Yea I got a battery pack for the two servos, it is an old circuit diagram - mainly put it up to show resistors in series. On the topic of the breadboard, i had found that one side was faulty, so I had to make individual jumps. Is there an errors in the code though? Other than changing the pins?
The way i read it is that the servo will rotate in the direction of the designated value, that doesnt seem to be happening though.
The servo seems to favour turning clockwise, and doesn't really stop. Is there any way to make it stop within limits of the value?
There are two common types of servos.
One common type of servo uses the input to determine the position of the actuator.
Another common type of servo uses the input to determine the rotational speed (and direction?) of the actuator.
If your servo rotates more than 360 degrees, then it most likely the second type, which doesn't really help with "Is there any way to make it stop within limits of the value?"
Is there no way to prevent the 360 degree servo from turning? Like lets say I want it to stop turning when the difference between the values is less than 10? I thought the write(90) done that?
Usually they don't stop at exactly 90. Try 89 or 91 or 87 or 92 etc. Better still use Servo.writeMicroseconds() for much finer control. Servo.writeMicroseconds(1500); is approximately equal to Servo.write(90);
...R