Pages: [1]   Go Down
Author Topic: new to arduino, basic servo/sensor question  (Read 527 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to hook up a sensor to an arduino to control a servo between the angles of 0 and 85. This code seems to be registering light gradually (which is what I want), but I'm not sure how to change it from 0 to 85. Any help would be much appreciated!! Thanks.

Code:
#include <Servo.h>
Servo servo1;
int sensorPin = 0; // light sensor is on analog in 0
int sensorValue01 = 3;

void setup() {
 servo1.attach(8);
}

void loop() {
 sensorValue01 = analogRead (sensorPin); //value of the light sensor, normally between 0 and 1023
int angle = sensorValue01 / 6; // to obtain angle between 0 and 170
angle = angle + 5; // to obtain value between 5 and 175
       servo1.write(angle); //move the servo
     Servo::refresh(); // necessary with this library
      delay(50); // to cool the jitter…
}

Moderator: code tags added.
« Last Edit: December 13, 2011, 07:00:51 am by AWOL » Logged

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

sorry! i have no idea how that emoticon got in there?! it should be attach to pin 8.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

refresh hasn't been necessary for quite some time.

The emoticon is there because you didn't post the code in a code box, using the # icon on the editor's toolbar.
« Last Edit: December 13, 2011, 06:59:50 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

okay, I deleted refresh and it still seems to be working. How do I change the values to go from 0 degrees to 85?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12577
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What value are you actually reading from the sensor, and what value are you actually writing to the servo?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well naively, divide the reading from the sensor by 12 instead of 6, but as PeterH alludes, you really need to see what range the sensor returns in the varying light conditions you're measuring and do the math with that.
Logged

Pages: [1]   Go Up
Jump to: