Loading...
Pages: [1]   Go Down
Author Topic: Servo and LDR  (Read 116 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi! I'm trying to make one servo to follow the light with two LDR's. I've been searching how to do it since I'm new with arduino and programming. I've done the code bellow with my knowledge but the servo don't move and I can't understand what am I doing wrong. Can anyone help me ? Thanks.

Code:
#include <Servo.h>
int pinoum = A0;
int pinodois = A1;
int valum = 0;
int valdois = 0;
Servo servo;
#define saida 2
float pos = 90;

void setup() {
  pinMode (saida, OUTPUT);
  Serial.begin(19200);
}

void loop() {
  valum = analogRead(pinoum);
  valdois = analogRead(pinodois);
  float volum = valum*(5.0/1023.0);
  float voldois = valdois*(5.0/1023.0);
  Serial.println(volum);
  Serial.println(voldois);
 
  if (volum>voldois){
    pos=pos+1;
    pos=constrain(pos,0,179);
    servo.write(pos);
    delay(1000);
  }
  else{
    if(valum<valdois){
      pos=pos-1;
      pos=constrain(pos,0,179);
      servo.write(pos);
      delay(1000);
    }
  }
 
  Serial.println(pos);
}
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 274
Posts: 25475
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What values do you get from
Code:
Serial.println(volum);
  Serial.println(voldois);

Look at the servo example, I can't see anywhere in your code that you tell the servo library what pin the servo is on.
http://arduino.cc/en/Reference/ServoAttach
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, now it's moving smiley

I get values between 0 and 5
Logged

Pages: [1]   Go Up
Print
 
Jump to: